Skip to content

Commit

Permalink
feat: add component dropzone
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Mar 30, 2022
1 parent 5a1dd4d commit ffa4bd1
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/components/Dropzone/Dropzone.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
.dropzone {
height: 300px;
border-radius: 10px;

display: flex;
justify-content: center;
align-items: center;
outline: 0;
}

.dropzone img {
width: 100%;
height: 100%;
object-fit: cover;
}

.dropzone p {
height: calc(100% - 60px);
border-radius: 10px;

display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #333;
}

.dropzone p svg {
color: #4ecb79;
width: 24px;
height: 24px;
margin-bottom: 8px;
z-index: 999;
}

.preview {
display: flex;
width: 85%;
object-fit: cover;
border-radius: 50%;
}

.profile {
display: flex;
width: 100%;
height: 25vh;
align-items: flex-end;
justify-content: center;
}

.upload {
display: flex;
justify-content: center;
align-items: center;
background: var(--orange);
height: 40px;
width: 40px;
z-index: 1;
position: absolute;
margin-left: 125px;
border-radius: 10px;
}
66 changes: 66 additions & 0 deletions src/components/Dropzone/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useCallback, useState } from 'react';
import { useDropzone } from 'react-dropzone';
import { FiCamera } from 'react-icons/fi';

import Avvvatars from 'avvvatars-react';
import { DropzoneProps } from 'types/IComponents';

import { useAuth } from 'hooks/useAuth';

import styles from './Dropzone.module.scss';

const Dropzone: React.FC<DropzoneProps> = ({ onFileUploaded }) => {
const { user } = useAuth();
const [selectedFileUrl, setSelectedFileUrl] = useState('');

const onDrop = useCallback(
(acceptedFiles) => {
const file = acceptedFiles[0];

const fileUrl = URL.createObjectURL(file);

setSelectedFileUrl(fileUrl);
onFileUploaded(file);
},
[onFileUploaded],
);
const { getRootProps, getInputProps } = useDropzone({
onDrop,
accept: 'image/*',
});

return (
<div className={styles.dropzone} {...getRootProps()}>
<input {...getInputProps()} accept="image/*" />

{selectedFileUrl ? (
<p>
<img src={selectedFileUrl} alt="Point thumbnail" className={styles.preview} />
</p>
) : (
<>
<div className={styles.profile}>
<p>
{user?.user_metadata.picture || user?.user_metadata.avatar_url ? (
<>
<img
src={user?.user_metadata.picture || user?.user_metadata.avatar_url}
alt="Perfil"
className={styles.preview}
/>
</>
) : (
<Avvvatars value={user?.user_metadata.name || ''} size={100} />
)}
</p>
<div className={styles.upload}>
<FiCamera />
</div>
</div>
</>
)}
</div>
);
};

export default Dropzone;

0 comments on commit ffa4bd1

Please sign in to comment.