From 2fbcb41cdd34cced35c68d070df3c5e0dc984a11 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sun, 20 Mar 2022 21:18:38 -0700 Subject: [PATCH] client: clean-up drag and drop code and set post title if unset --- .../new-post/drag-and-drop/index.tsx | 21 +++++++------------ client/components/new-post/index.tsx | 21 +++++++++++++++++-- client/components/new-post/title/index.tsx | 11 +++++----- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/client/components/new-post/drag-and-drop/index.tsx b/client/components/new-post/drag-and-drop/index.tsx index eaaab5b6..08973db7 100644 --- a/client/components/new-post/drag-and-drop/index.tsx +++ b/client/components/new-post/drag-and-drop/index.tsx @@ -94,12 +94,12 @@ const allowedFileExtensions = [ 'webmanifest', ] -function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch>, docs: Document[] }) { +function FileDropzone({ setDocs }: { setDocs: ((docs: Document[]) => void) }) { const { palette } = useTheme() const { setToast } = useToasts() - const onDrop = useCallback(async (acceptedFiles) => { - const newDocs = await Promise.all(acceptedFiles.map((file: File) => { - return new Promise((resolve, reject) => { + const onDrop = async (acceptedFiles: File[]) => { + const newDocs = await Promise.all(acceptedFiles.map((file) => { + return new Promise((resolve, reject) => { const reader = new FileReader() reader.onabort = () => setToast({ text: 'File reading was aborted', type: 'error' }) @@ -116,15 +116,8 @@ function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch [...oldDocs, ...newDocs]) - }, [setDocs, setToast, docs]) + setDocs(newDocs) + } const validator = (file: File) => { // TODO: make this configurable @@ -170,7 +163,7 @@ function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch {fileRejections.length > 0 &&
    {/* */} - There was a problem with some of your files. + There was a problem with one or more of your files. {fileRejectionItems}
} diff --git a/client/components/new-post/index.tsx b/client/components/new-post/index.tsx index 12c8a30b..6e41a42f 100644 --- a/client/components/new-post/index.tsx +++ b/client/components/new-post/index.tsx @@ -63,10 +63,27 @@ const Post = () => { setDocs(docs.map((doc) => doc.id === id ? { ...doc, content } : doc)) }, [docs]) + const uploadDocs = (files: Document[]) => { + // if no title is set and the only document is empty, + const isFirstDocEmpty = docs.length === 1 && docs[0].title === '' && docs[0].content === '' + const shouldSetTitle = !title && isFirstDocEmpty + console.log(shouldSetTitle, title, isFirstDocEmpty) + if (shouldSetTitle) { + if (files.length === 1) { + setTitle(files[0].title) + } else if (files.length > 1) { + setTitle('Uploaded files') + } + } + + if (isFirstDocEmpty) setDocs(files) + else setDocs([...docs, ...files]) + } + return (
- - <FileDropzone docs={docs} setDocs={setDocs} /> + <Title title={title} handleChange={(e) => setTitle(e.target.value)} /> + <FileDropzone setDocs={uploadDocs} /> { docs.map(({ id }) => { const doc = docs.find((doc) => doc.id === id) diff --git a/client/components/new-post/title/index.tsx b/client/components/new-post/title/index.tsx index 3ad765ab..dae79ac5 100644 --- a/client/components/new-post/title/index.tsx +++ b/client/components/new-post/title/index.tsx @@ -1,5 +1,5 @@ +import { ChangeEvent, memo } from 'react' import { Text, Input } from '@geist-ui/core' -import { memo } from 'react' import ShiftBy from '@components/shift-by' import styles from '../post.module.css' @@ -14,18 +14,19 @@ const titlePlaceholders = [ ] type props = { - setTitle: (title: string) => void + handleChange?: (event: ChangeEvent<HTMLInputElement>) => void title?: string } -const Title = ({ setTitle, title }: props) => { +const Title = ({ handleChange, title }: props) => { + return (<div className={styles.title}> <Text h1 width={"150px"} className={styles.drift}>Drift</Text> <ShiftBy y={-3}> <Input placeholder={titlePlaceholders[Math.floor(Math.random() * titlePlaceholders.length)]} value={title || ""} - onChange={(event) => setTitle(event.target.value)} + onChange={handleChange} height={"55px"} font={1.5} label="Post title" @@ -35,4 +36,4 @@ const Title = ({ setTitle, title }: props) => { </div>) } -export default memo(Title) \ No newline at end of file +export default Title \ No newline at end of file