From d8724c01e984254ef2934a3b0ebefcf3c73c59d7 Mon Sep 17 00:00:00 2001 From: mgtennant <100305096+mgtennant@users.noreply.github.com> Date: Mon, 27 May 2024 16:47:43 -0700 Subject: [PATCH 1/4] remove indexpage, debugging --- frontend/src/app/content/pages/IndexPage.tsx | 60 ------------------- .../src/app/content/pages/LandingPage.tsx | 4 ++ 2 files changed, 4 insertions(+), 60 deletions(-) delete mode 100644 frontend/src/app/content/pages/IndexPage.tsx diff --git a/frontend/src/app/content/pages/IndexPage.tsx b/frontend/src/app/content/pages/IndexPage.tsx deleted file mode 100644 index 315dc0a0..00000000 --- a/frontend/src/app/content/pages/IndexPage.tsx +++ /dev/null @@ -1,60 +0,0 @@ -// Unused page - -import { FC } from 'react'; -import Collapsible from '../../../app/components/common/Collapsible'; -import { DTRDisplayObject } from '../../../app/types/types'; -import TenureDetails from '../display/TenureDetails'; -import AreaDetails from '../display/AreaDetails'; -import DtidDetails from '../display/DtidDetails'; -import { Button } from 'react-bootstrap'; -// import { generateReportNew } from '../../common/report'; -import { useParams } from 'react-router'; - -export interface IndexPageProps { - data: DTRDisplayObject; -} - -const IndexPage: FC = ({ data }) => { - // const { dtid } = useParams<{ dtid: string }>(); - // const dtidNumber = dtid ? parseInt(dtid, 10) : null; - // const generateReportHandler = () => { - // if (dtidNumber) { - // // generateReportNew(dtidNumber, data!.fileNum, 'Land Use Report'); - // } - // }; - // return ( - // <> - //
Preview - Land Use Report (Draft)
- //
- //
- //
DTID:
- //
- // {data.dtid} - //
- //
- //
- //
Tenure File Number:
- //
- // {data.fileNum} - //
- //
- //
- //
Primary Contact Name:
- //
{data.primaryContactName}
- //
- // - // - // - // - // - // - // - // - // - // - // - // ); - return <>; -}; - -export default IndexPage; diff --git a/frontend/src/app/content/pages/LandingPage.tsx b/frontend/src/app/content/pages/LandingPage.tsx index 81b7e8c3..f7f6646a 100644 --- a/frontend/src/app/content/pages/LandingPage.tsx +++ b/frontend/src/app/content/pages/LandingPage.tsx @@ -226,9 +226,13 @@ const LandingPage: FC = () => { }; const validateProvisions = (): string | null => { + console.log('selectedProvisionIds'); + console.log(selectedProvisionIds); const unselectedMandatoryIds = mandatoryProvisionIds.filter( (mandatoryId) => !selectedProvisionIds.includes(mandatoryId) ); + console.log('unselectedMandatoryIds'); + console.log(unselectedMandatoryIds); const matchingProvisionIds = selectedProvisionIds.filter((id) => unselectedMandatoryIds.includes(id)); const matchingProvisions = provisions.filter((p) => matchingProvisionIds.includes(p.id)); const matchingGroupNumbers = [...new Set(matchingProvisions.map((provision) => provision.provision_group))]; From b8857650c9df0168ae1e1876b8179791945bafe1 Mon Sep 17 00:00:00 2001 From: mgtennant <100305096+mgtennant@users.noreply.github.com> Date: Tue, 28 May 2024 10:54:42 -0700 Subject: [PATCH 2/4] mandatory provision validation working --- backend/src/provision/provision.service.ts | 4 +- .../src/app/content/pages/LandingPage.tsx | 94 ++++++++++++------- 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/backend/src/provision/provision.service.ts b/backend/src/provision/provision.service.ts index f01751be..93cebbc3 100644 --- a/backend/src/provision/provision.service.ts +++ b/backend/src/provision/provision.service.ts @@ -240,9 +240,11 @@ export class ProvisionService { id: document_type_id, }, }, + relations: ['provision'], }); + console.log(docTypeProvisions); - return docTypeProvisions.map((provision) => provision.id); + return docTypeProvisions.map((provision) => provision.provision.id); } async getManageDocTypeProvisions(document_type_id: number) { diff --git a/frontend/src/app/content/pages/LandingPage.tsx b/frontend/src/app/content/pages/LandingPage.tsx index f7f6646a..7a43cc4c 100644 --- a/frontend/src/app/content/pages/LandingPage.tsx +++ b/frontend/src/app/content/pages/LandingPage.tsx @@ -43,6 +43,8 @@ const LandingPage: FC = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [showError, setShowError] = useState(false); + const [generateError, setGenerateError] = useState(null); + const [showGenerateError, setShowGenerateError] = useState(false); const [data, setData] = useState(null); const [mandatoryProvisionIds, setMandatoryProvisionIds] = useState([]); @@ -225,19 +227,33 @@ const LandingPage: FC = () => { setDtidInput(!isNaN(parseInt(e.target.value)) ? parseInt(e.target.value) : null); }; + /** + * Checks each provision group for mandatory provisions, then + * checks that those provision groups have at least one provision + * selected. If not, returns an error message. + * + * @returns the error message if any, otherwise null + */ const validateProvisions = (): string | null => { - console.log('selectedProvisionIds'); - console.log(selectedProvisionIds); - const unselectedMandatoryIds = mandatoryProvisionIds.filter( - (mandatoryId) => !selectedProvisionIds.includes(mandatoryId) - ); - console.log('unselectedMandatoryIds'); - console.log(unselectedMandatoryIds); - const matchingProvisionIds = selectedProvisionIds.filter((id) => unselectedMandatoryIds.includes(id)); - const matchingProvisions = provisions.filter((p) => matchingProvisionIds.includes(p.id)); - const matchingGroupNumbers = [...new Set(matchingProvisions.map((provision) => provision.provision_group))]; - if (matchingGroupNumbers.length > 0) { - return `There are unselected mandatory provisions the following groups: ${matchingGroupNumbers.join(', ')}`; + const mandatoryGroups: number[] = []; + for (let p of provisions) { + if (p.type === 'M' && !mandatoryGroups.includes(p.provision_group.provision_group)) { + mandatoryGroups.push(p.provision_group.provision_group); + } + } + const nonEmptyProvisionGroups: number[] = []; + for (let p of provisions) { + if ( + selectedProvisionIds.includes(p.provision_id) && + !nonEmptyProvisionGroups.includes(p.provision_group.provision_group) + ) { + nonEmptyProvisionGroups.push(p.provision_group.provision_group); + } + } + const emptyMandatoryGroups = mandatoryGroups.filter((id) => !nonEmptyProvisionGroups.includes(id)); + emptyMandatoryGroups.sort((a, b) => a - b); + if (emptyMandatoryGroups.length > 0) { + return `There are unselected mandatory provisions the following groups: ${emptyMandatoryGroups.join(', ')}`; } else { return null; } @@ -280,6 +296,8 @@ const LandingPage: FC = () => { setData(null); setIsOpen(false); setSelectedDocTypeId(null); + setShowError(false); + setShowGenerateError(false); }; const getReportData = () => { @@ -301,30 +319,29 @@ const LandingPage: FC = () => { return { variableJsonArray, provisionJsonArray }; }; - const handleDocumentSave = () => { - const saveData = async () => { - if (dtid && documentType) { - try { - setLoading(true); - const { variableSaveData, provisionSaveData } = getSaveData(); - await saveDocument(dtid, documentType.id, provisionSaveData, variableSaveData); - } catch (err) { - console.log('Error saving Document Data'); - console.log(err); - } finally { - setLoading(false); - } - } else { - console.log('No DTID was found.'); + const handleDocumentSave = async () => { + if (dtid && documentType) { + try { + setLoading(true); + const { variableSaveData, provisionSaveData } = getSaveData(); + await saveDocument(dtid, documentType.id, provisionSaveData, variableSaveData); + } catch (err) { + console.log('Error saving Document Data'); + console.log(err); + } finally { + setLoading(false); } - }; - saveData(); + } else { + console.log('No DTID was found.'); + } }; - const handleGenerateReport = () => { + const handleGenerateReport = async () => { try { + setShowGenerateError(false); + setLoading(true); + await handleDocumentSave(); setLoading(true); - handleDocumentSave(); if (dtid) { const errorMessage = validateProvisions(); if (!errorMessage) { @@ -339,11 +356,15 @@ const LandingPage: FC = () => { ); } } else { - alert(errorMessage); + setGenerateError(errorMessage); + setShowGenerateError(true); + // alert(errorMessage); } } } catch (err) { console.log(err); + setGenerateError('Something went wrong.'); + setShowGenerateError(true); // set error state } finally { setLoading(false); @@ -432,7 +453,14 @@ const LandingPage: FC = () => { ) : ( <> )} - + <> + {' '} + {showGenerateError && ( + + {generateError} + + )} +
<>