Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing landing page #212

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/src/document_type/document_type.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class DocumentTypeController {

@Post('remove-provision-group')
removeProvisionGroup(@Body() data: { provision_group_id: number }) {
console.log('provision_group_id: ' + data.provision_group_id);
return this.documentTypeService.removeProvisionGroup(data.provision_group_id);
}
}
3 changes: 3 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import * as bodyParser from 'body-parser';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(bodyParser.json({ limit: '1mb' }));
app.use(bodyParser.urlencoded({ limit: '1mb', extended: true }));
app.enableCors();
const config = new DocumentBuilder()
.setTitle('Users example')
Expand Down
5 changes: 2 additions & 3 deletions backend/src/provision/provision.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ export class ProvisionService {
id: document_type_id,
},
},
relations: ['provision'],
});

return docTypeProvisions.map((provision) => provision.id);
return docTypeProvisions.map((provision) => provision.provision.id);
}

async getManageDocTypeProvisions(document_type_id: number) {
Expand Down Expand Up @@ -374,7 +374,6 @@ export class ProvisionService {
return existingProv;
});
await this.documentTypeProvisionRepository.save(updatedProvisions);
console.log('Provisions updated successfully!');
}

/**
Expand Down
60 changes: 0 additions & 60 deletions frontend/src/app/content/pages/IndexPage.tsx

This file was deleted.

92 changes: 62 additions & 30 deletions frontend/src/app/content/pages/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const LandingPage: FC = () => {
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const [showError, setShowError] = useState<boolean>(false);
const [generateError, setGenerateError] = useState<string | null>(null);
const [showGenerateError, setShowGenerateError] = useState<boolean>(false);

const [data, setData] = useState<DTRDisplayObject | null>(null);
const [mandatoryProvisionIds, setMandatoryProvisionIds] = useState<number[]>([]);
Expand Down Expand Up @@ -225,15 +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 => {
const unselectedMandatoryIds = mandatoryProvisionIds.filter(
(mandatoryId) => !selectedProvisionIds.includes(mandatoryId)
);
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;
}
Expand Down Expand Up @@ -276,6 +296,8 @@ const LandingPage: FC = () => {
setData(null);
setIsOpen(false);
setSelectedDocTypeId(null);
setShowError(false);
setShowGenerateError(false);
};

const getReportData = () => {
Expand All @@ -297,36 +319,35 @@ 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) {
if (data && documentType && documentType.id) {
const { variableJsonArray, provisionJsonArray } = getReportData();
generateReport(
await generateReport(
dtid,
data && data.fileNum ? data.fileNum : '',
documentType.id,
Expand All @@ -335,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);
Expand Down Expand Up @@ -428,7 +453,14 @@ const LandingPage: FC = () => {
) : (
<></>
)}

<>
{' '}
{showGenerateError && (
<Alert variant="danger" className="mb-3 d-inline-block" style={{ width: 'auto', maxWidth: '100%' }}>
{generateError}
</Alert>
)}
</>
<div style={{ display: 'flex', gap: '10px', minHeight: '55px' }}>
<>
<Button
Expand Down
Loading