ente/src/pages/gallery/components/CollectionDropZone.tsx

85 lines
2.1 KiB
TypeScript
Raw Normal View History

import React from 'react';
import Dropzone from 'react-dropzone';
import styled from 'styled-components';
2021-01-08 03:52:51 +00:00
import UploadService from 'services/uploadService';
import { fetchData } from 'services/fileService';
2021-01-13 12:48:55 +00:00
export const getColor = (props) => {
2021-01-08 03:52:51 +00:00
if (props.isDragAccept) {
return '#00e676';
}
if (props.isDragReject) {
return '#ff1744';
}
if (props.isDragActive) {
return '#2196f3';
}
};
2021-01-13 12:48:55 +00:00
export const enableBorder = (props) => (props.isDragActive ? 'dashed' : 'none');
2021-01-13 12:48:55 +00:00
export const DropDiv = styled.div`
2021-01-14 12:34:55 +00:00
width:200px;
margin:5px;
height:230px;
border-width: 2px;
border-radius: 2px;
border-color: ${(props) => getColor(props)};
border-style: ${(props) => enableBorder(props)};
outline: none;
transition: border 0.24s ease-in-out;
`;
const CollectionDropZone = ({
2021-01-08 03:52:51 +00:00
children,
closeModal,
refetchData,
2021-01-08 03:52:51 +00:00
collectionLatestFile,
2021-01-12 06:59:37 +00:00
setProgressView,
token,
2021-01-12 06:59:37 +00:00
progressBarProps
}) => {
2021-01-08 03:52:51 +00:00
const upload = async (acceptedFiles) => {
closeModal();
2021-01-12 08:00:40 +00:00
progressBarProps.setPercentComplete(0);
2021-01-12 06:59:37 +00:00
setProgressView(true);
2021-01-12 08:00:40 +00:00
2021-01-12 06:59:37 +00:00
await UploadService.uploadFiles(acceptedFiles, collectionLatestFile, token, progressBarProps);
refetchData();
2021-01-12 08:00:40 +00:00
setProgressView(false);
2021-01-08 03:52:51 +00:00
}
return (
<Dropzone
onDropAccepted={upload}
onDropRejected={closeModal}
2021-01-13 12:48:55 +00:00
noDragEventsBubbling
2021-01-12 13:43:41 +00:00
accept="image/*, video/*, application/json "
2021-01-08 03:52:51 +00:00
>
{({
getRootProps,
getInputProps,
isDragActive,
isDragAccept,
isDragReject,
}) => {
return (
<DropDiv
{...getRootProps({
isDragActive,
isDragAccept,
isDragReject,
})}
>
<input {...getInputProps()} />
{children}
</DropDiv>
);
}}
</Dropzone>
);
};
export default CollectionDropZone;