-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create secret) add file upload support (#11)
- Loading branch information
Showing
20 changed files
with
492 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import React from "react"; | ||
import { makeStyles, Theme } from "@material-ui/core/styles"; | ||
import Tabs from "@material-ui/core/Tabs"; | ||
import Tab from "@material-ui/core/Tab"; | ||
import TabPanel from "./TabPanel"; | ||
import CreateSecretForm from "./CreateSecretForm"; | ||
import { FormikHelpers, FormikValues } from "formik"; | ||
import { Box, Divider, Typography } from "@material-ui/core"; | ||
|
||
type SecretType = "file" | "message"; | ||
|
||
function a11yProps(index: any) { | ||
return { | ||
id: `form-tab-${index}`, | ||
"aria-controls": `simple-tabpanel-${index}` | ||
}; | ||
} | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
root: { | ||
maxWidth: "500px", | ||
margin: "0 auto", | ||
backgroundColor: theme.palette.background.paper, | ||
gap: theme.spacing(4), | ||
flexDirection: "column", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
width: "100%", | ||
maxHeight: "80%", | ||
height: "80%", | ||
boxShadow: "rgba(0, 0, 0, 0.1) 0px 4px 12px", | ||
background: "#f5f7f8", | ||
borderRadius: "5px", | ||
padding: theme.spacing(2) | ||
}, | ||
container: {} | ||
})); | ||
|
||
type CreateSecretFormTabsProps = { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
onSubmit: (values: FormikValues, helpers: FormikHelpers<any>) => void; | ||
initialValues: FormikValues; | ||
loading: boolean; | ||
}; | ||
|
||
const CreateSecretFormTabs: React.FC<CreateSecretFormTabsProps> = props => { | ||
const classes = useStyles(); | ||
const [value, setValue] = React.useState<"file" | "message">("message"); | ||
|
||
const handleChange = (event: React.ChangeEvent<any>, newValue: SecretType) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Box textAlign="center" marginY="1rem"> | ||
<Typography variant="body1" style={{ fontWeight: "bold" }}> | ||
Which kind of secret do you want to share ? | ||
</Typography> | ||
</Box> | ||
<Divider style={{ margin: "0 3rem" }} /> | ||
<Tabs value={value} onChange={handleChange} centered> | ||
<Tab value="message" label="Secret message" {...a11yProps("message")} /> | ||
<Tab value="file" label="Secret file" {...a11yProps("file")} /> | ||
</Tabs> | ||
<TabPanel value={value} index="message"> | ||
<CreateSecretForm | ||
type={value} | ||
onSubmit={props.onSubmit} | ||
initialValues={props.initialValues} | ||
loading={props.loading} | ||
/> | ||
</TabPanel> | ||
<TabPanel value={value} index="file"> | ||
<CreateSecretForm | ||
type={value} | ||
onSubmit={props.onSubmit} | ||
initialValues={props.initialValues} | ||
loading={props.loading} | ||
/> | ||
</TabPanel> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CreateSecretFormTabs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Typography } from "@material-ui/core"; | ||
import clsx from "clsx"; | ||
import { makeStyles } from "@material-ui/styles"; | ||
import { ErrorMessage, FieldProps } from "formik"; | ||
import { useMemo } from "react"; | ||
import { useDropzone } from "react-dropzone"; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
error: { | ||
borderColor: "#f44336 !important" | ||
} | ||
})); | ||
|
||
const baseStyle = { | ||
flex: 1, | ||
display: "flex", | ||
alignItems: "center", | ||
padding: "20px", | ||
borderWidth: 2, | ||
borderRadius: 2, | ||
height: "175px", | ||
borderColor: "#eeeeee", | ||
borderStyle: "dashed", | ||
backgroundColor: "#fafafa", | ||
color: "#bdbdbd", | ||
outline: "none", | ||
transition: "border .24s ease-in-out" | ||
}; | ||
|
||
const activeStyle = { | ||
borderColor: "#2196f3" | ||
}; | ||
|
||
const acceptStyle = { | ||
borderColor: "#00e676" | ||
}; | ||
|
||
type StyledDropzoneProps = FieldProps; | ||
|
||
const FILE_SIZE = 64 * 1024; | ||
|
||
const StyledDropzone: React.FC<StyledDropzoneProps> = ({ form }) => { | ||
const classes = useStyles(); | ||
const { getRootProps, getInputProps, acceptedFiles, isDragActive, isDragAccept } = useDropzone({ | ||
maxSize: FILE_SIZE, | ||
onDrop: (_acceptedFiles: File[]) => { | ||
const file = _acceptedFiles[0]; | ||
form.setFieldValue("file", file); | ||
form.setFieldValue("filename", file.name); | ||
form.setFieldValue("is_base64", true); | ||
} | ||
}); | ||
|
||
const files = acceptedFiles.map(file => <Typography key={file.name}>{file.name}</Typography>); | ||
|
||
const style = useMemo( | ||
() => ({ | ||
...baseStyle, | ||
...(isDragActive ? activeStyle : {}), | ||
...(isDragAccept ? acceptStyle : {}) | ||
}), | ||
[isDragActive, isDragAccept] | ||
); | ||
|
||
return ( | ||
<div className="container"> | ||
<div | ||
{...getRootProps({ style })} | ||
className={clsx({ [classes.error]: form.touched["file"] && !!form.errors["file"] })} | ||
> | ||
<input {...getInputProps()} /> | ||
{isDragActive ? ( | ||
<p>Drop the files here ...</p> | ||
) : ( | ||
<>{!files.length ? "Drag'n drop your file here, or click to select the file" : files}</> | ||
)} | ||
</div> | ||
<small className="MuiFormHelperText-root MuiFormHelperText-contained Mui-error Mui-required"> | ||
<ErrorMessage name="file" /> | ||
</small> | ||
</div> | ||
); | ||
}; | ||
|
||
export default StyledDropzone; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from "react"; | ||
import AttachmentIcon from "@material-ui/icons/Attachment"; | ||
import { Box, Button, makeStyles, Theme, Typography } from "@material-ui/core"; | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
container: { | ||
position: "absolute", | ||
top: "50%", | ||
left: "50%", | ||
transform: "translate(-50%, -50%)", | ||
width: "100%", | ||
maxWidth: "500px", | ||
background: "#f7f5f5", | ||
padding: theme.spacing(2) | ||
} | ||
})); | ||
|
||
type ShowFileProps = { | ||
file?: File; | ||
uploadedAt?: Date; | ||
}; | ||
|
||
const ShowFile: React.FC<ShowFileProps> = ({ file }) => { | ||
const classes = useStyles(); | ||
|
||
const handleDownloadClick = () => { | ||
if (file) { | ||
const url = window.URL.createObjectURL(new Blob([file])); | ||
const link = document.createElement("a"); | ||
link.href = url; | ||
link.target = "_blank"; | ||
link.setAttribute("download", file.name); | ||
document.body.appendChild(link); | ||
link.click(); | ||
link.parentNode && link.parentNode.removeChild(link); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={classes.container}> | ||
<Box display="flex" alignItems="center" justifyContent="space-between"> | ||
<div> | ||
<AttachmentIcon fontSize="large" /> | ||
</div> | ||
<div> | ||
<Typography variant="caption">Name</Typography> | ||
<Typography>{file?.name}</Typography> | ||
</div> | ||
<div> | ||
<Typography variant="caption">Size</Typography> | ||
<Typography>{`${file?.size && (file?.size / 1024).toFixed(2)} Mb`}</Typography> | ||
</div> | ||
<div> | ||
<Button variant="contained" color="primary" onClick={handleDownloadClick} fullWidth> | ||
Download | ||
</Button> | ||
</div> | ||
</Box> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ShowFile; |
Oops, something went wrong.