Skip to content

Commit

Permalink
move generate report logic to useeffect
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed Nov 15, 2024
1 parent 22e6726 commit 0d5517a
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions frontend/src/app/content/pages/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const LandingPage: FC = () => {
const searchState = useSelector((state: RootState) => state.search);
const [isOpen, setIsOpen] = useState(false);

const [generateReportState, setGenerateReportState] = useState(false);

// Old route compatibility, set dtid from url, set doc type to lur if none given
useEffect(() => {
if (dtidFromUrl && initializeDtid) {
Expand Down Expand Up @@ -365,21 +367,12 @@ const LandingPage: FC = () => {
try {
setShowGenerateError(false);
setLoading(true);
await handleDocumentSave();
setLoading(true);
if (dtid) {
if (dtid && documentType) {
await handleDocumentSave();

const errorMessage = validateProvisions();
if (!errorMessage) {
if (data && documentType && documentType.id) {
const { variableJsonArray, provisionJsonArray } = getReportData();
await generateReport(
dtid,
data && data.fileNum ? data.fileNum : '',
documentType.id,
provisionJsonArray,
variableJsonArray
);
}
setGenerateReportState(true);
} else {
setGenerateError(errorMessage);
setShowGenerateError(true);
Expand All @@ -395,6 +388,31 @@ const LandingPage: FC = () => {
}
};

// generateReportHandler sets generateReportState to true, which triggers the useEffect hook below to generate the report
useEffect(() => {
const genReport = async () => {
try {
if (generateReportState) {
if (data && documentType && documentType.id && dtid) {
const { variableJsonArray, provisionJsonArray } = getReportData();
await generateReport(
dtid,
data && data.fileNum ? data.fileNum : '',
documentType.id,
provisionJsonArray,
variableJsonArray
);
}
}
} catch (err) {
console.log(err);
} finally {
setGenerateReportState(false);
}
};
genReport();
}, [generateReportState]);

return (
<>
<div className="h1">{documentType ? `Preview - ${documentType.name}` : 'Document Preview'}</div>
Expand Down

0 comments on commit 0d5517a

Please sign in to comment.