diff --git a/src/components/DocumentForm/DocumentForm.js b/src/components/DocumentForm/DocumentForm.js index 2abf80a4..33181f4e 100644 --- a/src/components/DocumentForm/DocumentForm.js +++ b/src/components/DocumentForm/DocumentForm.js @@ -56,6 +56,7 @@ const DocumentForm = ({ mode, data, setErrors, + setPageLoading, }) => { const router = useRouter(); const [showModal, setShowModal] = useState(false); @@ -314,6 +315,7 @@ const DocumentForm = ({ { const submitFunction = mode === 'edit' ? editDocument : createDocument; + setPageLoading(true); setTimeout(() => { submitFunction(values) .then(() => { @@ -328,6 +330,7 @@ const DocumentForm = ({ }) .catch((err) => { setErrors([err.message]); + setPageLoading(false); }); actions.setSubmitting(false); }, 1000); @@ -563,6 +566,7 @@ const DocumentForm = ({ handleCloseModal={handleCloseModal} show={showModal} onClick={(event) => { + setPageLoading(true); event.target.setAttribute('disabled', 'true'); deleteDocumentById(data.id).then(() => { router.push({ @@ -574,6 +578,7 @@ const DocumentForm = ({ }); }).catch((err) => { setErrors([err.message]); + setPageLoading(false); }); handleCloseModal(); }} diff --git a/src/pages/documents/[slug]/edit.jsx b/src/pages/documents/[slug]/edit.jsx index e9f3b187..1778dc29 100644 --- a/src/pages/documents/[slug]/edit.jsx +++ b/src/pages/documents/[slug]/edit.jsx @@ -1,5 +1,5 @@ import { useSession } from 'next-auth/client'; -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Card, Col, } from 'react-bootstrap'; @@ -10,24 +10,37 @@ import DocumentForm from '../../../components/DocumentForm'; import { prefetchDocumentBySlug } from '../../../utils/docUtil'; const EditDocument = ({ document, alerts, statefulSession }) => { - const [session] = useSession(); + const [session, loading] = useSession(); + const [pageLoading, setPageLoading] = useState(true); + + useEffect(() => { + if (loading === false) { + setPageLoading(false); + } + }, [loading]); const [errors, setErrors] = useState(alerts || []); return ( - {!session && ( + {((!session && loading) || pageLoading) && ( )} - {session && document && ( + {session && document && !loading && !pageLoading && ( <> Edit document - + )} - {session && !document && ( + {session && !document && !loading && !pageLoading && ( <> Document not found Sorry, this document could not be found. diff --git a/src/pages/documents/new.jsx b/src/pages/documents/new.jsx index 91877531..bf18225b 100644 --- a/src/pages/documents/new.jsx +++ b/src/pages/documents/new.jsx @@ -1,5 +1,5 @@ import { useSession } from 'next-auth/client'; -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { Card, Col, } from 'react-bootstrap'; @@ -8,20 +8,33 @@ import LoadingSpinner from '../../components/LoadingSpinner'; import DocumentForm from '../../components/DocumentForm'; const NewDocument = ({ statefulSession }) => { - const [session] = useSession(); + const [session, loading] = useSession(); + const [pageLoading, setPageLoading] = useState(true); + + useEffect(() => { + if (loading === false) { + setPageLoading(false); + } + }, [loading]); + const [errors, setErrors] = useState([]); return ( - {!session && ( + {((!session && loading) || pageLoading) && ( )} - {session && ( + {session && !loading && !pageLoading && ( <> Create a new document - + )}