client: clean-up drag and drop code and set post title if unset

This commit is contained in:
Max Leiter 2022-03-20 21:18:38 -07:00
parent 921f219c5a
commit 2fbcb41cdd
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
3 changed files with 32 additions and 21 deletions

View file

@ -94,12 +94,12 @@ const allowedFileExtensions = [
'webmanifest', 'webmanifest',
] ]
function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch<React.SetStateAction<Document[]>>, docs: Document[] }) { function FileDropzone({ setDocs }: { setDocs: ((docs: Document[]) => void) }) {
const { palette } = useTheme() const { palette } = useTheme()
const { setToast } = useToasts() const { setToast } = useToasts()
const onDrop = useCallback(async (acceptedFiles) => { const onDrop = async (acceptedFiles: File[]) => {
const newDocs = await Promise.all(acceptedFiles.map((file: File) => { const newDocs = await Promise.all(acceptedFiles.map((file) => {
return new Promise((resolve, reject) => { return new Promise<Document>((resolve, reject) => {
const reader = new FileReader() const reader = new FileReader()
reader.onabort = () => setToast({ text: 'File reading was aborted', type: 'error' }) reader.onabort = () => setToast({ text: 'File reading was aborted', type: 'error' })
@ -116,15 +116,8 @@ function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch<React.SetStat
}) })
})) }))
if (docs.length === 1) { setDocs(newDocs)
if (docs[0].content === '') { }
setDocs(newDocs)
return
}
}
setDocs((oldDocs) => [...oldDocs, ...newDocs])
}, [setDocs, setToast, docs])
const validator = (file: File) => { const validator = (file: File) => {
// TODO: make this configurable // TODO: make this configurable
@ -170,7 +163,7 @@ function FileDropzone({ setDocs, docs }: { setDocs: React.Dispatch<React.SetStat
</div> </div>
{fileRejections.length > 0 && <ul className={styles.error}> {fileRejections.length > 0 && <ul className={styles.error}>
{/* <Button style={{ float: 'right' }} type="abort" onClick={() => fileRejections.splice(0, fileRejections.length)} auto iconRight={<XCircle />}></Button> */} {/* <Button style={{ float: 'right' }} type="abort" onClick={() => fileRejections.splice(0, fileRejections.length)} auto iconRight={<XCircle />}></Button> */}
<Text h5>There was a problem with some of your files.</Text> <Text h5>There was a problem with one or more of your files.</Text>
{fileRejectionItems} {fileRejectionItems}
</ul>} </ul>}
</div> </div>

View file

@ -63,10 +63,27 @@ const Post = () => {
setDocs(docs.map((doc) => doc.id === id ? { ...doc, content } : doc)) setDocs(docs.map((doc) => doc.id === id ? { ...doc, content } : doc))
}, [docs]) }, [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 ( return (
<div> <div>
<Title title={title} setTitle={setTitle} /> <Title title={title} handleChange={(e) => setTitle(e.target.value)} />
<FileDropzone docs={docs} setDocs={setDocs} /> <FileDropzone setDocs={uploadDocs} />
{ {
docs.map(({ id }) => { docs.map(({ id }) => {
const doc = docs.find((doc) => doc.id === id) const doc = docs.find((doc) => doc.id === id)

View file

@ -1,5 +1,5 @@
import { ChangeEvent, memo } from 'react'
import { Text, Input } from '@geist-ui/core' import { Text, Input } from '@geist-ui/core'
import { memo } from 'react'
import ShiftBy from '@components/shift-by' import ShiftBy from '@components/shift-by'
import styles from '../post.module.css' import styles from '../post.module.css'
@ -14,18 +14,19 @@ const titlePlaceholders = [
] ]
type props = { type props = {
setTitle: (title: string) => void handleChange?: (event: ChangeEvent<HTMLInputElement>) => void
title?: string title?: string
} }
const Title = ({ setTitle, title }: props) => { const Title = ({ handleChange, title }: props) => {
return (<div className={styles.title}> return (<div className={styles.title}>
<Text h1 width={"150px"} className={styles.drift}>Drift</Text> <Text h1 width={"150px"} className={styles.drift}>Drift</Text>
<ShiftBy y={-3}> <ShiftBy y={-3}>
<Input <Input
placeholder={titlePlaceholders[Math.floor(Math.random() * titlePlaceholders.length)]} placeholder={titlePlaceholders[Math.floor(Math.random() * titlePlaceholders.length)]}
value={title || ""} value={title || ""}
onChange={(event) => setTitle(event.target.value)} onChange={handleChange}
height={"55px"} height={"55px"}
font={1.5} font={1.5}
label="Post title" label="Post title"
@ -35,4 +36,4 @@ const Title = ({ setTitle, title }: props) => {
</div>) </div>)
} }
export default memo(Title) export default Title