-
Notifications
You must be signed in to change notification settings - Fork 2
Profile picture uploading #41 #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 16 commits
6150600
634eba9
5786abc
9d9145b
fdee411
8c262d3
81e35e0
10de14f
a48754d
475d68d
9f4e76f
704cd98
92df7b6
3771316
d81e373
a74e172
13f946a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { AuthApiData } from '../../interface/AuthApiData'; | ||
|
|
||
| const uploadImage = async (Image: File): Promise<AuthApiData> => { | ||
| const data = new FormData(); | ||
| data.append('uploads', Image); | ||
|
|
||
| const fetchOptions = { | ||
| method: 'POST', | ||
| body: data, | ||
| }; | ||
|
|
||
| return await fetch(`/imageUpload`, fetchOptions) | ||
| .then((res) => res.json()) | ||
| .catch((err) => { | ||
| console.log('UPLOADIMAGE-ERROR: ', err); | ||
| }); | ||
| }; | ||
|
|
||
| export default uploadImage; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import React from 'react'; | ||
| import { ChangeEvent, FormEvent, useState } from 'react'; | ||
| import useStyles from './useStyles'; | ||
| import AuthMenu from '../../components/AuthMenu/AuthMenu'; | ||
| import Grid from '@material-ui/core/Grid'; | ||
|
|
@@ -8,9 +10,37 @@ import Button from '@material-ui/core/Button'; | |
| import IconButton from '@material-ui/core/IconButton'; | ||
| import Box from '@material-ui/core/Box'; | ||
| import DeleteIcon from '@material-ui/icons/Delete'; | ||
| import uploadImage from '../../helpers/APICalls/uploadImage'; | ||
|
|
||
| export default function ProfilePicture(): JSX.Element { | ||
| const classes = useStyles(); | ||
| const [fileInput, setFileInput] = useState<File>(); | ||
| const [previewImage, setPreviewImage] = useState(''); | ||
| const componentRef = React.useRef<HTMLFormElement>(null); | ||
|
|
||
| const handleSubmit = (event: FormEvent<HTMLFormElement>) => { | ||
| event.preventDefault(); | ||
| if (!fileInput) return; | ||
| uploadImage(fileInput); | ||
| }; | ||
|
|
||
| const handleChange = async (event: ChangeEvent<HTMLInputElement>) => { | ||
| const target = event.target as HTMLInputElement; | ||
| const image: File = (target.files as FileList)[0]; | ||
| profileImage(image); | ||
| await setFileInput(image); | ||
| componentRef.current?.requestSubmit(); | ||
| }; | ||
|
|
||
| const profileImage = (image: File) => { | ||
| const reader = new FileReader(); | ||
| reader.readAsDataURL(image); | ||
| reader.onloadend = () => { | ||
| const result = reader.result as string; | ||
| setPreviewImage(result); | ||
| }; | ||
| }; | ||
|
|
||
| return ( | ||
| <Box className={classes.outsideContainer}> | ||
| <AuthMenu /> | ||
|
|
@@ -34,8 +64,8 @@ export default function ProfilePicture(): JSX.Element { | |
| Profile Picture | ||
| </Typography> | ||
| </Grid> | ||
| <Grid item container className={classes.profileImageContainer}> | ||
| <img src="" className={classes.profileImage}></img> | ||
| <Grid item container className={classes.previewImageContainer}> | ||
| {previewImage && <img src={previewImage} className={classes.previewImage}></img>} | ||
| </Grid> | ||
| <Grid item container className={classes.tipContainer}> | ||
| <Typography variant="h6" className={classes.tip}> | ||
|
|
@@ -44,11 +74,31 @@ export default function ProfilePicture(): JSX.Element { | |
| </Typography> | ||
| </Grid> | ||
| <Grid item container className={classes.uploadButtonContainer}> | ||
| <Button variant="text" className={classes.uploadButton}> | ||
| Upload a file from your device | ||
| </Button> | ||
| <form | ||
| method="POST" | ||
| action="/imageUpload" | ||
| id="upload-Image-Form" | ||
| ref={componentRef} | ||
| onSubmit={handleSubmit} | ||
| encType="multipart/form-data" | ||
| > | ||
| <input | ||
|
||
| className={classes.uploadInput} | ||
| id="fileInput" | ||
| name="uploads" | ||
| type="file" | ||
| onChange={handleChange} | ||
| /> | ||
| <label htmlFor="fileInput" className={classes.label}> | ||
|
||
| <Button variant="contained" component="span" className={classes.uploadButton}> | ||
| Upload a file from your device | ||
| </Button> | ||
| </label> | ||
| <button type="submit" className={classes.submitButton}></button> | ||
|
||
| </form> | ||
| </Grid> | ||
| <Grid item container className={classes.deleteButtonContainer}> | ||
| {/*this is going to remove from the cloud*/} | ||
|
||
| <IconButton className={classes.deleteButton}> | ||
| <DeleteIcon className={classes.deleteButtonIcon} /> | ||
| Delete photo | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ const useStyles = makeStyles(() => ({ | |
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| zIndex: -1, | ||
| // zIndex: -1, | ||
|
||
| overflow: 'hidden', | ||
| }, | ||
| innerContentContainer: { | ||
|
|
@@ -37,7 +37,7 @@ const useStyles = makeStyles(() => ({ | |
| color: 'black', | ||
| margin: '2rem', | ||
| }, | ||
| profileImageContainer: { | ||
| previewImageContainer: { | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
|
|
@@ -46,11 +46,11 @@ const useStyles = makeStyles(() => ({ | |
| paddingBottom: '2rem', | ||
| border: 'none', | ||
| }, | ||
| profileImage: { | ||
| previewImage: { | ||
| position: 'relative', | ||
| display: 'flex', | ||
| height: '10rem', | ||
| width: '10rem', | ||
| height: '15rem', | ||
| width: '15rem', | ||
| border: '0.15rem solid', | ||
| borderRadius: '50%', | ||
| }, | ||
|
|
@@ -75,20 +75,27 @@ const useStyles = makeStyles(() => ({ | |
| }, | ||
| uploadButton: { | ||
| border: '0.15rem solid', | ||
| borderRadius: '0.3rem', | ||
| borderColor: 'red', | ||
| color: 'red', | ||
| fontWeight: 700, | ||
| fontSize: '1rem', | ||
| fontFamily: 'sans-serif', | ||
| height: '4rem', | ||
| width: '30%', | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| backgroundColor: 'transparent', | ||
| '&:hover': { | ||
| backgroundColor: 'transparent', | ||
| }, | ||
| }, | ||
| label: {}, | ||
| submitButton: { | ||
| display: 'none', | ||
| }, | ||
| uploadInput: { | ||
| display: 'none', | ||
| }, | ||
| deleteButtonContainer: { | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
console.errorfor this log