-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
292 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React, { useRef, useState, useEffect, useCallback } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import NoStyleButton from 'Components/Form/NoStyleButton'; | ||
|
||
import Cropper from 'react-cropper'; | ||
import 'cropperjs/dist/cropper.css'; | ||
|
||
import Button from '@material-ui/core/Button'; | ||
|
||
function ImageCropper(props) { | ||
const { imgHandler } = props; | ||
const cropperRef = useRef(null); | ||
const user = useSelector((state) => state.authReducer.user); | ||
|
||
const [pictureImg, setPictureImg] = useState(null); | ||
const [pictureImgUrl, setPictureImgUrl] = useState(null); | ||
|
||
const handlePictureImg = async (e) => { | ||
if (e.target.files[0] !== undefined) { | ||
setPictureImg(e.target.files[0]); | ||
setPictureImgUrl(URL.createObjectURL(e.target.files[0])); | ||
} | ||
}; | ||
|
||
const handlePostUpload = useCallback(async () => { | ||
if (user && cropperRef.current) { | ||
const imgElement = cropperRef.current; | ||
const { cropper } = imgElement; | ||
if (pictureImg && cropper) { | ||
const { name } = pictureImg; | ||
const canvas = cropper.getCroppedCanvas(); | ||
if (canvas) { | ||
canvas.toBlob( | ||
async (croppedImg) => { | ||
const image = new FormData(); | ||
image.append('img', croppedImg, name); | ||
imgHandler(image); | ||
}, | ||
undefined, | ||
1, | ||
); | ||
} | ||
} else { | ||
// TO DO :: notify uploading image ! | ||
} | ||
} else { | ||
// TO DO :: implement login modal ! | ||
} | ||
}, [imgHandler, pictureImgUrl, user, pictureImg]); | ||
|
||
const removePicture = () => { | ||
setPictureImg(null); | ||
setPictureImgUrl(null); | ||
}; | ||
|
||
useEffect(() => { | ||
handlePostUpload(); | ||
}, [pictureImg, handlePostUpload]); | ||
|
||
return ( | ||
<> | ||
<div className="imageCropper"> | ||
<div className="imageCropper__creator"> | ||
<div className="imageCropper__creator-previewPic"> | ||
{pictureImg && ( | ||
<> | ||
<Cropper | ||
className="cropper" | ||
alt="imageCrop" | ||
style={{ zIndex: 2 }} | ||
src={pictureImgUrl} | ||
ref={cropperRef} | ||
crop={handlePostUpload} | ||
/> | ||
<NoStyleButton | ||
className="imageCropper__creator-previewPic__remove" | ||
onClick={removePicture} | ||
> | ||
X | ||
</NoStyleButton> | ||
</> | ||
)} | ||
{!pictureImg && ( | ||
<div className="imageCropper__creator-previewPic__empty"> | ||
<div className="imageCropper__creator-previewPic__empty-uploadPic"> | ||
<Button | ||
size="small" | ||
fullWidth | ||
disableRipple | ||
disableFocusRipple | ||
> | ||
<label htmlFor="art-upload"> | ||
upload image | ||
<input | ||
accept="image/*" | ||
type="file" | ||
name="img" | ||
id="art-upload" | ||
style={{ display: 'none' }} | ||
onChange={handlePictureImg} | ||
/> | ||
</label> | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
ImageCropper.propTypes = { | ||
imgHandler: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default ImageCropper; |
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,59 @@ | ||
/* | ||
ImageCropper Component Styling | ||
*/ | ||
|
||
.imageCropper { | ||
width: 100%; | ||
border-radius: 18px; | ||
button:focus { | ||
outline: none; | ||
} | ||
&__creator { | ||
margin-bottom: 1rem; | ||
&-previewPic { | ||
position: relative; | ||
display: flex; | ||
margin: auto; | ||
border: 2px dotted #ccd0d5; | ||
background-color: rgba(255, 244, 201, 0.3); | ||
|
||
&__remove { | ||
margin: 0.5rem; | ||
z-index: 1; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
color: $main-color; | ||
font-weight: bolder; | ||
font-size: 1.5rem; | ||
} | ||
|
||
&__empty { | ||
position: relative; | ||
height: 400px; | ||
width: 100%; | ||
background-color: $background-color; | ||
opacity: 50%; | ||
&-uploadPic { | ||
z-index: 1; | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
label { | ||
margin: 0; | ||
cursor: pointer; | ||
font-family: 'GothamRound'; | ||
color: $main-color; | ||
} | ||
} | ||
} | ||
|
||
.cropper { | ||
margin: auto; | ||
max-width: 80%; | ||
max-height: 400px; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.