Skip to content

Commit

Permalink
saving provisions was inefficient and failing
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed May 27, 2024
1 parent ec2faf5 commit e207a7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { updateDocType } from '../../../common/manage-doc-types';
import { Button } from 'react-bootstrap';

interface EditDocTypeTableProps {
onUpdate: (documentType: Partial<DocType>) => void;
refreshDocTypes: () => void;
}

const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ onUpdate, refreshDocTypes }) => {
const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
const [isEditing, setIsEditing] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const dispatch = useDispatch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import React, { FC, useEffect, useState } from 'react';
import { DataTable } from '../common/DataTable';
import { ColumnDef, Row, createColumnHelper } from '@tanstack/react-table';
import {
ManageDocTypeProvision,
associateProvisionToDocType,
disassociateProvisionFromDocType,
} from '../../../common/manage-doc-types';
import { ManageDocTypeProvision } from '../../../common/manage-doc-types';
import { ProvisionGroup } from '../../../types/types';
import { DocumentProvisionSearchState } from '../../common/manage-doc-types/DocumentProvisionSearch';

interface ManageDocumentProvisionsTableProps {
documentTypeId: number;
provisions: ManageDocTypeProvision[] | undefined;
provisionGroups: ProvisionGroup[] | undefined;
searchState: DocumentProvisionSearchState;
onUpdate: (manageDocTypeProvisions: ManageDocTypeProvision[]) => void;
}

const ManageDocumentProvisionsTable: React.FC<ManageDocumentProvisionsTableProps> = ({
documentTypeId,
provisions,
provisionGroups,
searchState,
Expand Down
41 changes: 14 additions & 27 deletions frontend/src/app/content/pages/ManageDocumentsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useCallback, useEffect, useState } from 'react';
import { FC, useCallback, useEffect, useState } from 'react';
import { Button, Col, Row } from 'react-bootstrap';
import { DocType, ProvisionGroup } from '../../types/types';
import { getDocumentTypes, getGroupMaxByDocTypeId } from '../../common/report';
Expand All @@ -10,7 +10,6 @@ import {
addDocType,
getManageDocumentTypeProvisions,
removeDocType,
updateDocType,
updateManageDocTypeProvisions,
} from '../../common/manage-doc-types';
import EditProvisionGroupsModal from '../../components/modal/manage-doc-types/EditProvisionGroupsModal';
Expand All @@ -22,7 +21,6 @@ import DocumentProvisionSearch, {
import { useDispatch, useSelector } from 'react-redux';
import { RootState } from '../../store/store';
import { setDocType } from '../../store/reducers/docTypeSlice';
// import TestProvisionTable from '../../components/table/manage-doc-types/TestProvisionTable';

const ManageDocumentsPage: FC = () => {
const [loading, setLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -52,12 +50,7 @@ const ManageDocumentsPage: FC = () => {
});

const dispatch = useDispatch();
const { selectedDocType, updatedDocType } = useSelector((state: RootState) => state.docType);
const [docTypeEdits, setDocTypeEdits] = useState<DocType>(selectedDocType);

useEffect(() => {
setDocTypeEdits(selectedDocType);
}, [selectedDocType]);
const { selectedDocType } = useSelector((state: RootState) => state.docType);

useEffect(() => {
const getData = async () => {
Expand Down Expand Up @@ -153,13 +146,13 @@ const ManageDocumentsPage: FC = () => {
}
};

// called when a provision is altered in the ManageDocumentProvisionstable
const updateProvisionsState = (newProvisionsState: ManageDocTypeProvision[]) => {
setUpdatedProvisions(newProvisionsState);
};

const saveButtonHandler = async () => {
try {
// await saveDocType();
await saveProvisions();
} catch (err) {
console.log('Error when saving');
Expand All @@ -168,20 +161,20 @@ const ManageDocumentsPage: FC = () => {
}
};

const handleDocTypeEdit = useCallback((newValues: Partial<DocType>) => {
setDocTypeEdits((prev) => ({ ...prev, ...newValues }));
}, []);
const findUpdatedProvisions = (): ManageDocTypeProvision[] => {
const updated = updatedProvisions.filter((updatedProvision) => {
const originalProvision = provisions.find((p) => p.id === updatedProvision.id);
return originalProvision && JSON.stringify(updatedProvision) !== JSON.stringify(originalProvision);
});
return updated;
};

const saveProvisions = async () => {
try {
setLoading(true);
console.log('Saving Provisions...');
for (let prov of updatedProvisions) {
if (prov.associated) {
console.log(prov);
}
}
await updateManageDocTypeProvisions(selectedDocType.id, updatedProvisions);
const updated: ManageDocTypeProvision[] = findUpdatedProvisions();
console.log(updated);
await updateManageDocTypeProvisions(selectedDocType.id, updated);
} catch (err) {
console.log('Error updating provisions');
console.log(err);
Expand Down Expand Up @@ -231,7 +224,7 @@ const ManageDocumentsPage: FC = () => {
<hr />
{/** Doc Type info / edit single row table */}
{/** Document Type Name - Date Created - Created By - Last Updated Date - Last Updated By */}
<EditDocTypeTable refreshDocTypes={refreshDocTypes} onUpdate={handleDocTypeEdit} />
<EditDocTypeTable refreshDocTypes={refreshDocTypes} />
<hr />
<h1>Associate Provisions to {selectedDocType.name}</h1>
<hr />
Expand All @@ -249,17 +242,11 @@ const ManageDocumentsPage: FC = () => {

{/** Global Provisions table */}
<ManageDocumentProvisionsTable
documentTypeId={selectedDocType.id}
provisions={provisions}
provisionGroups={provisionGroups}
searchState={searchState}
onUpdate={updateProvisionsState}
/>
{/* <TestProvisionTable
provisions={provisions}
provisionGroups={provisionGroups}
onUpdate={updateProvisionsState}
/> */}
{/** ID - Type - Group - Seq - Max - Provision Name - Free Text - Category - Associated */}
<EditProvisionGroupsModal
provisionGroups={provisionGroups}
Expand Down

0 comments on commit e207a7d

Please sign in to comment.