Skip to content

Commit

Permalink
fix(form-service): corrections to API docs
Browse files Browse the repository at this point in the history
Also form app state adjustments for redirecting from anonymous form routes.
Also check form definition for if PDF is generated.
  • Loading branch information
tzuge committed Aug 28, 2024
1 parent 474d9f3 commit e1722a0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
4 changes: 2 additions & 2 deletions apps/form-app/src/app/components/SubmittedForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const SubmittedForm: FunctionComponent<ApplicationStatusProps> = ({ defin
};

useEffect(() => {
if (pdfFileExists === null && form?.urn) {
if (definition.generatesPdf && pdfFileExists === null && form?.urn) {
dispatch(checkPdfFile(form.urn));
}

Expand All @@ -86,7 +86,7 @@ export const SubmittedForm: FunctionComponent<ApplicationStatusProps> = ({ defin
}, 10000);

return () => clearInterval(intervalId);
}, [dispatch, form, pdfFile, pdfFileExists]);
}, [dispatch, definition, form, pdfFile, pdfFileExists]);

const metadata = useSelector(metaDataSelector);

Expand Down
50 changes: 28 additions & 22 deletions apps/form-app/src/app/containers/AnonymousForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Container, Recaptcha } from '@core-services/app-common';
import { FunctionComponent } from 'react';
import { Navigate } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { DraftForm } from '../components/DraftForm';
import { LoadingIndicator } from '../components/LoadingIndicator';
import { SubmittedForm } from '../components/SubmittedForm';
import {
ValidationError,
Expand Down Expand Up @@ -32,29 +34,33 @@ export const AnonymousForm: FunctionComponent = () => {

return (
<div key={`anonymous-${definition?.id}`}>
<LoadingIndicator isLoading={!initialized} />
<Container vs={3} hs={1}>
{definition && (
<>
{form?.status === 'submitted' && <SubmittedForm definition={definition} form={form} data={data} />}
{!form && (
<DraftForm
definition={definition}
form={form}
data={data}
canSubmit={canSubmit}
showSubmit={showSubmit}
saving={busy.saving}
submitting={busy.submitting}
onChange={function ({ data, errors }: { data: unknown; errors?: ValidationError[] }) {
dispatch(updateForm({ data: data as Record<string, unknown>, files, errors }));
}}
onSubmit={function () {
dispatch(submitAnonymousForm());
}}
/>
)}
</>
)}
{initialized &&
(definition?.anonymousApply ? (
<>
{form?.status === 'submitted' && <SubmittedForm definition={definition} form={form} data={data} />}
{!form && (
<DraftForm
definition={definition}
form={form}
data={data}
canSubmit={canSubmit}
showSubmit={showSubmit}
saving={busy.saving}
submitting={busy.submitting}
onChange={function ({ data, errors }: { data: unknown; errors?: ValidationError[] }) {
dispatch(updateForm({ data: data as Record<string, unknown>, files, errors }));
}}
onSubmit={function () {
dispatch(submitAnonymousForm());
}}
/>
)}
</>
) : (
<Navigate to=".." />
))}
</Container>
<Recaptcha siteKey={recaptchaKey} />
</div>
Expand Down
8 changes: 7 additions & 1 deletion apps/form-app/src/app/state/form.slice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const definitionsToTest: FormState = {
uiSchema: { type: 'Categorization' },
applicantRoles: ['Admin'],
clerkRoles: [],
anonymousApply: false,
},
},
selected: null,
Expand Down Expand Up @@ -73,6 +74,7 @@ const loadedFormDefinition: FormDefinition = {
uiSchema: { type: 'Categorization' },
applicantRoles: ['Admin'],
clerkRoles: [],
anonymousApply: false,
};
const payload = {
form: {
Expand Down Expand Up @@ -104,7 +106,11 @@ describe('form slice unit tests', () => {
});

it('can fulfilled load form definition with data', () => {
const action = { type: loadDefinition.fulfilled, payload: 'TEST' };
const action = {
type: loadDefinition.fulfilled,
payload: loadedFormDefinition,
meta: { arg: loadedFormDefinition.id },
};
const definition = formReducer(definitionsToTest, action);
expect(definition.definitions.TEST.id).toEqual(loadedFormDefinition.id);
});
Expand Down
9 changes: 5 additions & 4 deletions apps/form-app/src/app/state/form.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface FormDefinition {
clerkRoles: string[];
registerData?: RegisterData;
anonymousApply: boolean;
generatesPdf?: boolean;
}

export interface Form {
Expand Down Expand Up @@ -328,8 +329,8 @@ export const updateForm = createAsyncThunk(
) => {
const { form, user } = getState() as AppState;

// Dispatch saving the draft if there is a logged in user.
if (user.user) {
// Dispatch saving the draft if there is a logged in user with a draft form.
if (user.user && form.form?.id) {
dispatch(formActions.setSaving(true));
dispatch(saveForm(form.form.id));
}
Expand Down Expand Up @@ -491,9 +492,9 @@ export const formSlice = createSlice({
.addCase(loadDefinition.pending, (state) => {
state.busy.loading = true;
})
.addCase(loadDefinition.fulfilled, (state, { payload }) => {
.addCase(loadDefinition.fulfilled, (state, { payload, meta }) => {
state.busy.loading = false;
state.definitions[payload.id] = payload;
state.definitions[meta.arg] = payload;

//Check form definition id case sensitivity, and use the definition id in the payload object,
//instead of using the value in querystring because if the case is not the same
Expand Down
1 change: 1 addition & 0 deletions apps/form-service/src/form/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function mapFormDefinition(entity: FormDefinitionEntity) {
dataSchema: entity.dataSchema,
uiSchema: entity.uiSchema,
dispositionStates: entity.dispositionStates,
generatesPdf: !!entity.submissionPdfTemplate
};
}

Expand Down
3 changes: 2 additions & 1 deletion apps/form-service/src/form/model/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { Form, FormStatus, SecurityClassificationType } from '../types';
import { FormSubmissionEntity } from './formSubmission';
import { PdfService } from '../pdf';

// Any form created by user with the intake app role is treated as anonymous.
// NOTE: True if form has an applicant and was not created by the applicant user (i.e. created by an intake application under a service account)
// Not True if the form does not include applicant at all, which is the case for 'simple' forms where form is directly submitted.
function isAnonymousApplicant(user: User, applicant?: Subscriber): boolean {
return !!(user?.roles.includes(FormServiceRoles.IntakeApp) && applicant && applicant.userId !== user.id);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/form-service/src/form/router/form.swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ components:
type: string
description:
type: string
generatesPdf:
type: boolean
Form:
type: object
properties:
Expand Down Expand Up @@ -245,7 +247,7 @@ components:
schema:
type: object
properties:
anonymousApplicant:
anonymousApplicantEquals:
type: boolean
definitionIdEquals:
type: string
Expand All @@ -260,6 +262,8 @@ components:
format: date-time
createdByIdEquals:
type: string
hashEquals:
type: string
responses:
200:
description: Successfully created a form.
Expand Down

0 comments on commit e1722a0

Please sign in to comment.