feat: get upload component up to spec

This commit is contained in:
José Oliveira 2021-10-20 22:50:38 +01:00
parent bedb75b767
commit 10b8838cf0
7 changed files with 130 additions and 34 deletions

View file

@ -1,30 +1,68 @@
import { makeStyles } from '@material-ui/core/styles'
import { useFormikContext } from 'formik'
import * as R from 'ramda'
import React, { useState, useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
import { Info3 } from 'src/components/typography'
import { offColor, subheaderColor } from 'src/styling/variables'
const useStyles = makeStyles({
input: {
width: 200
box: {
boxSizing: 'border-box',
marginTop: 31,
width: 450,
height: 120,
borderStyle: 'dashed',
borderColor: offColor,
borderRadius: 4,
borderWidth: 1,
backgroundColor: subheaderColor,
display: 'flex',
justifyContent: 'center'
},
boxContent: {
marginTop: 30
},
board: {
width: 450,
height: 120
}
})
const Upload = ({ type }) => {
const classes = useStyles()
const { values } = useFormikContext()
console.log(values)
const IMAGE = 'image'
const [data, setData] = useState({})
const message =
type === IMAGE
? 'Drag and drop an image or click to select a file'
: 'Drag and drop or click to select a file'
const onDrop = useCallback(acceptedData => {
setData({ preview: URL.createObjectURL(R.head(acceptedData)) })
}, [])
const { getRootProps, getInputProps } = useDropzone({ onDrop })
return (
<>
<div className={classes.board}>
<input
type={type}
name={type}
onClick={() => {
console.log(values)
}}
className={classes.input}
/>
<div {...getRootProps()} className={classes.board}>
<input {...getInputProps()} />
{R.isEmpty(data) && (
<div className={classes.box}>
<div className={classes.boxContent}>
<Info3>{message}</Info3>
</div>
</div>
)}
{!R.isEmpty(data) && type === IMAGE && (
<div key={data.name}>
<img src={data.preview} className={classes.box}></img>
</div>
)}
</div>
<div className={classes.picture}></div>
</>
)
}