-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/sustech-cs304/team-project-…
- Loading branch information
Showing
6 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React, {useState} from 'react'; | ||
import axios from 'axios'; | ||
|
||
const ImageUploader = ({addPoster}) => { | ||
const authToken = localStorage.getItem('authToken'); | ||
const [name, setName] = useState(''); | ||
const [avatar, setAvatar] = useState(null); | ||
const [imageUrl, setImageUrl] = useState(''); | ||
|
||
const handleNameChange = (e) => setName(e.target.value); | ||
const handleAvatarChange = (e) => setAvatar(e.target.files[0]); | ||
|
||
const uploadImage = async (file) => { | ||
try { | ||
const formData = new FormData(); | ||
formData.append('image', file); | ||
|
||
const response = await axios.post( | ||
'http://10.27.41.93:5000/api/images', | ||
formData, | ||
{ | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'multipart/form-data', | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
}, | ||
{ | ||
withCredentials: true, | ||
}, | ||
); | ||
console.log(response.data.url); | ||
return response.data.url; | ||
} catch (error) { | ||
console.error('Error uploading image:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
if (!authToken) { | ||
console.error('No auth token available'); | ||
return; | ||
} | ||
if (!avatar) { | ||
console.error('No file selected'); | ||
return; | ||
} | ||
try { | ||
const avatarUrl = await uploadImage(avatar); | ||
setImageUrl(avatarUrl); | ||
addPoster({avatarUrl}); | ||
} catch (error) { | ||
console.error('Error uploading avatar:', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
type='text' | ||
value={name} | ||
onChange={handleNameChange} | ||
placeholder='Name' | ||
required | ||
/> | ||
<input | ||
type='file' | ||
onChange={handleAvatarChange} | ||
accept='image/*' | ||
required | ||
/> | ||
<button type='submit'>Add Poster</button> | ||
</form> | ||
{imageUrl && ( | ||
<div> | ||
<p>Uploaded Image:</p> | ||
<img | ||
src={imageUrl} | ||
alt='Uploaded Avatar' | ||
style={{maxWidth: '300px'}} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ImageUploader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters