From 871b57ea3c89aae098116a1b05434935bd5e9b31 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Tue, 29 Mar 2022 17:06:11 -0700 Subject: [PATCH] client: client-side validation of new post page --- client/components/new-post/index.tsx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/components/new-post/index.tsx b/client/components/new-post/index.tsx index c761912e..f3240b96 100644 --- a/client/components/new-post/index.tsx +++ b/client/components/new-post/index.tsx @@ -16,6 +16,7 @@ const Post = () => { const { setToast } = useToasts() const router = useRouter(); const [title, setTitle] = useState() + const [docs, setDocs] = useState([{ title: '', content: '', @@ -61,6 +62,30 @@ const Post = () => { } setSubmitting(true) + let hasErrored = false + + if (!title) { + setToast({ + text: 'Please fill out the post title', + type: 'error' + }) + setSubmitting(false) + hasErrored = true + } + + for (const doc of docs) { + if (!doc.title) { + setToast({ + text: 'Please fill out all the document titles', + type: 'error' + }) + setSubmitting(false) + hasErrored = true + } + } + + if (hasErrored) return + await sendRequest('/server-api/posts/create', { title, files: docs,