-
Notifications
You must be signed in to change notification settings - Fork 5
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
Avatar image component #140
Open
mAbadsa
wants to merge
9
commits into
main
Choose a base branch
from
138-avatar-image-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dd40c3e
Create User Avatar Component to uppload user image
mAbadsa ebdfcb8
Merge branch 'main' of https://github.com/GSG-G9/house-hunting-app in…
mAbadsa 8a13cac
Merge branch 'main' of https://github.com/GSG-G9/house-hunting-app in…
mAbadsa 46f70a5
Add the data as key in the sending object
mAbadsa 49a6fbf
Solve conflict
mAbadsa 9b008ea
Add refresh state to rerender the component
mAbadsa f59943f
Merge branch 'main' of https://github.com/GSG-G9/house-hunting-app in…
mAbadsa f3a4e6e
Fix spelling bud and add accept attribute to input element
mAbadsa a4163a3
Merge branch 'main' of https://github.com/GSG-G9/house-hunting-app in…
mAbadsa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Axios from 'axios'; | ||
import { Typography } from '@material-ui/core'; | ||
import { Alert } from '@material-ui/lab'; | ||
|
||
import Button from '../../../../Components/Button'; | ||
import useStyles from './style'; | ||
|
||
function UserAvatar({ setRefresh, setOpen }) { | ||
const classes = useStyles(); | ||
const [previewSource, setPreviewSource] = useState(); | ||
// eslint-disable-next-line no-unused-vars | ||
const [inputFileState, setInputFileState] = useState(''); | ||
const [selectedFile, setSelectedFile] = useState(); | ||
const [errorMsg, setErrorMsg] = useState(''); | ||
|
||
const previewFile = (file) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.onloadend = () => { | ||
setPreviewSource(reader.result); | ||
}; | ||
}; | ||
|
||
const handleUploadFile = (e) => { | ||
const file = e.target.files[0]; | ||
previewFile(file); | ||
setSelectedFile(file); | ||
}; | ||
|
||
const uploadImage = async (base64EncodedImage) => { | ||
try { | ||
await Axios.patch('/api/v1/upload', { data: base64EncodedImage }); | ||
setInputFileState(''); | ||
setPreviewSource(''); | ||
setRefresh(true); | ||
setOpen(false); | ||
setErrorMsg('Image uploaded successfully'); | ||
} catch (err) { | ||
setErrorMsg('Something went wrong!'); | ||
} | ||
}; | ||
|
||
const handleSubmitImage = (e) => { | ||
e.preventDefault(); | ||
if (!selectedFile) return; | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(selectedFile); | ||
reader.onloadend = () => { | ||
uploadImage(reader.result); | ||
}; | ||
reader.onerror = () => { | ||
setErrorMsg('Something went wrong!'); | ||
}; | ||
}; | ||
|
||
return ( | ||
<div className={classes.modal}> | ||
<Typography variant="h6">Choose an image</Typography> | ||
{errorMsg && <Alert variant="error">{errorMsg}</Alert>} | ||
<div className={classes.formContainer}> | ||
<form className={classes.avtarForm} onSubmit={handleSubmitImage}> | ||
<label htmlFor="file-input-select"> | ||
Choose file | ||
<input | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest that you add the |
||
accept="image/*" | ||
type="file" | ||
name="image" | ||
id="file-input-select" | ||
value={inputFileState} | ||
onChange={handleUploadFile} | ||
className={classes.selectFileBtn} | ||
/> | ||
</label> | ||
<Button | ||
type="submit" | ||
variant="contained" | ||
color="secondary" | ||
className={classes.uploadBtn} | ||
> | ||
Upload | ||
</Button> | ||
</form> | ||
<div className={classes.imagePreview}> | ||
{previewSource && <img src={previewSource} alt="avatar" />} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
UserAvatar.propTypes = { | ||
setRefresh: PropTypes.func.isRequired, | ||
setOpen: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default UserAvatar; |
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,71 @@ | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
modal: { | ||
position: 'absolute', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
width: 400, | ||
backgroundColor: '#F5F5F5', | ||
border: '1px solid #c7c7c7', | ||
borderRadius: '4px', | ||
padding: theme.spacing(2, 4, 3), | ||
left: 'calc(50% - 200px)', | ||
top: 'calc(50% - 200px)', | ||
'& h6': { | ||
color: theme.palette.primary.dark, | ||
fontWeight: 'bold', | ||
}, | ||
}, | ||
formContainer: { | ||
marginTop: '1rem', | ||
width: '100%', | ||
display: 'flex', | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
}, | ||
avtarForm: { | ||
width: '40%', | ||
'& label': { | ||
display: 'inline-block', | ||
width: '85px', | ||
height: '35px', | ||
color: theme.palette.primary.dark, | ||
backgroundColor: '#F5F5F5', | ||
border: `1px solid ${theme.palette.primary.dark}`, | ||
borderRadius: '3px', | ||
padding: '0.5em', | ||
textAlign: 'center', | ||
cursor: 'pointer', | ||
transition: 'all 0.25s', | ||
'&:hover': { | ||
color: '#F5F5F5', | ||
backgroundColor: theme.palette.primary.dark, | ||
border: '1px solid #F5F5F5', | ||
transition: 'all 0.25s', | ||
}, | ||
}, | ||
}, | ||
selectFileBtn: { | ||
visibility: 'hidden', | ||
}, | ||
imagePreview: { | ||
width: '60%', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
minHeight: '100px', | ||
backgroundColor: '#c7c7c7', | ||
borderRadius: '5px', | ||
'& img': { | ||
width: '150px', | ||
}, | ||
}, | ||
uploadBtn: { | ||
color: '#f5f5f5', | ||
marginTop: '1em', | ||
}, | ||
})); | ||
|
||
export default useStyles; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is awesome