|
@@ -1,20 +1,14 @@
|
|
-import React, { useRef, useState } from 'react';
|
|
|
|
|
|
+import React, { useState } from 'react';
|
|
import style from './DragAndDropPopup.module.scss';
|
|
import style from './DragAndDropPopup.module.scss';
|
|
import { ReactComponent as IconAddFile } from '../../assets/img/iconAddFile.svg';
|
|
import { ReactComponent as IconAddFile } from '../../assets/img/iconAddFile.svg';
|
|
import { usePopup } from '../../hooks/usePopup';
|
|
import { usePopup } from '../../hooks/usePopup';
|
|
import { CropperPopup } from '../CropperPopup/CropperPopup';
|
|
import { CropperPopup } from '../CropperPopup/CropperPopup';
|
|
import { getImageUrlFromFile } from '../../utils/getImageUrlFromFile';
|
|
import { getImageUrlFromFile } from '../../utils/getImageUrlFromFile';
|
|
-import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
-import { changeAvatar } from '../../store/user/actions/changeAvatar';
|
|
|
|
-import { uploadAvatar } from '../../store/user/actions/uploadAvatar';
|
|
|
|
|
|
|
|
export const DragAndDropPopup = ({ isOpen, close }) => {
|
|
export const DragAndDropPopup = ({ isOpen, close }) => {
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
const [isDragging, setIsDragging] = useState(false);
|
|
- const [imageSrc, setImageSrc] = useState('');
|
|
|
|
|
|
+ const [imageUrl, setImageUrl] = useState('');
|
|
const cropper = usePopup();
|
|
const cropper = usePopup();
|
|
- const formRef = useRef();
|
|
|
|
- const dispatch = useDispatch();
|
|
|
|
- const user = useSelector((state) => state.user);
|
|
|
|
|
|
|
|
const handleDragEnter = (e) => {
|
|
const handleDragEnter = (e) => {
|
|
e.preventDefault();
|
|
e.preventDefault();
|
|
@@ -25,19 +19,21 @@ export const DragAndDropPopup = ({ isOpen, close }) => {
|
|
setIsDragging(false);
|
|
setIsDragging(false);
|
|
};
|
|
};
|
|
|
|
|
|
- const handleDrop = async (event) => {
|
|
|
|
|
|
+ const getUncroppedAvatar = async (event) => {
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
setIsDragging(false);
|
|
setIsDragging(false);
|
|
- const image = await getImageUrlFromFile(event.dataTransfer.files[0]);
|
|
|
|
- setImageSrc(image);
|
|
|
|
- cropper.open();
|
|
|
|
- };
|
|
|
|
|
|
|
|
- const handleUpload = async (event) => {
|
|
|
|
- const currentFile = event.target.files[0];
|
|
|
|
|
|
+ let file;
|
|
|
|
+ if (event._reactName === 'onChange') {
|
|
|
|
+ file = event.target.files[0];
|
|
|
|
+ }
|
|
|
|
+ if (event._reactName === 'onDrop') {
|
|
|
|
+ file = event.dataTransfer.files[0];
|
|
|
|
+ }
|
|
|
|
|
|
- dispatch(uploadAvatar(currentFile));
|
|
|
|
- // cropper.open();
|
|
|
|
|
|
+ const image = await getImageUrlFromFile(file);
|
|
|
|
+ setImageUrl(image);
|
|
|
|
+ cropper.open();
|
|
};
|
|
};
|
|
|
|
|
|
const handleClose = () => {
|
|
const handleClose = () => {
|
|
@@ -53,16 +49,16 @@ export const DragAndDropPopup = ({ isOpen, close }) => {
|
|
<CropperPopup
|
|
<CropperPopup
|
|
isOpen={cropper.isPopupVisible}
|
|
isOpen={cropper.isPopupVisible}
|
|
close={cropper.close}
|
|
close={cropper.close}
|
|
- imageSrc={imageSrc}
|
|
|
|
|
|
+ closeParent={close}
|
|
|
|
+ imageUrl={imageUrl}
|
|
/>
|
|
/>
|
|
|
|
|
|
- <form
|
|
|
|
- ref={formRef}
|
|
|
|
|
|
+ <div
|
|
className={style.container}
|
|
className={style.container}
|
|
onDragEnter={handleDragEnter}
|
|
onDragEnter={handleDragEnter}
|
|
onDragOver={(e) => e.preventDefault()}
|
|
onDragOver={(e) => e.preventDefault()}
|
|
onDragLeave={handleDragLeave}
|
|
onDragLeave={handleDragLeave}
|
|
- onDrop={handleDrop}
|
|
|
|
|
|
+ onDrop={getUncroppedAvatar}
|
|
>
|
|
>
|
|
<div className={style.header}>
|
|
<div className={style.header}>
|
|
<img
|
|
<img
|
|
@@ -71,19 +67,21 @@ export const DragAndDropPopup = ({ isOpen, close }) => {
|
|
/>
|
|
/>
|
|
<span onClick={close}>x</span>
|
|
<span onClick={close}>x</span>
|
|
</div>
|
|
</div>
|
|
|
|
+
|
|
<label htmlFor="upload" className={style.dropArea}>
|
|
<label htmlFor="upload" className={style.dropArea}>
|
|
<IconAddFile />
|
|
<IconAddFile />
|
|
<span>Select Files to Upload</span>
|
|
<span>Select Files to Upload</span>
|
|
<span>or Drag and Drop, Copy and Paste Files</span>
|
|
<span>or Drag and Drop, Copy and Paste Files</span>
|
|
|
|
+
|
|
<input
|
|
<input
|
|
type="file"
|
|
type="file"
|
|
id="upload"
|
|
id="upload"
|
|
- onChange={handleUpload}
|
|
|
|
|
|
+ onChange={getUncroppedAvatar}
|
|
hidden
|
|
hidden
|
|
accept="image/*,.png,.jpg.,.web"
|
|
accept="image/*,.png,.jpg.,.web"
|
|
/>
|
|
/>
|
|
</label>
|
|
</label>
|
|
- </form>
|
|
|
|
|
|
+ </div>
|
|
</>
|
|
</>
|
|
);
|
|
);
|
|
};
|
|
};
|