|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | +import AvatarEditor from 'react-avatar-editor'; |
| 3 | + |
| 4 | +import noAvatarImage from '../../../assets/no-avatar.jpg'; |
| 5 | +import firebaseImage from '../../../assets/firebase.png'; |
| 6 | + |
| 7 | +import './setProfileImage.scss'; |
| 8 | +import { useDispatch, useMappedState } from 'redux-react-hook'; |
| 9 | +import { action_setAvatar } from '../../../store/profile/setAvatar'; |
| 10 | +import { isAvatarImageUploading } from '../../../store/profile/selectors'; |
| 11 | + |
| 12 | +const NoAvatar = () => { |
| 13 | + return ( |
| 14 | + <div className="image-container"> |
| 15 | + <img style={{ width: '200px' }} alt="profile" src={noAvatarImage} /> |
| 16 | + </div> |
| 17 | + ); |
| 18 | +}; |
| 19 | + |
| 20 | +export const SetProfileImage = () => { |
| 21 | + const dispatch = useDispatch(); |
| 22 | + |
| 23 | + const isLoading = useMappedState(isAvatarImageUploading); |
| 24 | + |
| 25 | + const [imageRef, setImageRef] = useState<AvatarEditor | null>(null); |
| 26 | + const [avatar, setAvatar] = useState<File>(); |
| 27 | + const uploadImageInputRef = useRef<HTMLInputElement>(null); |
| 28 | + |
| 29 | + const setEditorRef = (editor: AvatarEditor | null) => setImageRef(editor); |
| 30 | + |
| 31 | + if (isLoading) { |
| 32 | + return ( |
| 33 | + <div className="set-profile-image"> |
| 34 | + <img src={firebaseImage} /> |
| 35 | + <span>Processing...</span> |
| 36 | + </div> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="set-profile-image"> |
| 42 | + <img src={firebaseImage} /> |
| 43 | + <span>Upload your photo</span> |
| 44 | + {avatar ? ( |
| 45 | + <AvatarEditor |
| 46 | + style={{ |
| 47 | + margin: '20px', |
| 48 | + marginTop: '0px', |
| 49 | + width: '250px', |
| 50 | + height: '378px', |
| 51 | + }} |
| 52 | + ref={setEditorRef} |
| 53 | + image={URL.createObjectURL(avatar)} |
| 54 | + border={0} |
| 55 | + width={852} |
| 56 | + height={1280} |
| 57 | + borderRadius={0} |
| 58 | + color={[255, 255, 255, 0.6]} |
| 59 | + scale={1} |
| 60 | + /> |
| 61 | + ) : ( |
| 62 | + <NoAvatar /> |
| 63 | + )} |
| 64 | + <button onClick={() => uploadImageInputRef.current?.click()}>Select photo</button> |
| 65 | + <input |
| 66 | + ref={uploadImageInputRef} |
| 67 | + type="file" |
| 68 | + accept=".jpg, .jpeg" |
| 69 | + onChange={(e) => { |
| 70 | + const { files } = e.target; |
| 71 | + if (files) { |
| 72 | + const file = files[0]; |
| 73 | + if (file) { |
| 74 | + setAvatar(file); |
| 75 | + } |
| 76 | + } |
| 77 | + }} |
| 78 | + style={{ display: 'none' }} |
| 79 | + /> |
| 80 | + <button |
| 81 | + type="submit" |
| 82 | + disabled={false} |
| 83 | + onClick={() => { |
| 84 | + if (imageRef && avatar) { |
| 85 | + const canvasScaledImage: HTMLCanvasElement = imageRef.getImageScaledToCanvas(); |
| 86 | + canvasScaledImage.toBlob( |
| 87 | + (imageBlob: Blob | null) => { |
| 88 | + dispatch(action_setAvatar(imageBlob)); |
| 89 | + }, |
| 90 | + 'image/jpeg', |
| 91 | + 0.9, |
| 92 | + ); |
| 93 | + } |
| 94 | + }} |
| 95 | + > |
| 96 | + <span>Save</span> |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + ); |
| 100 | +}; |
0 commit comments