Skip to content

Commit

Permalink
Add output document to the mock form
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Feb 28, 2024
1 parent 1f22743 commit 69ab8ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
3 changes: 2 additions & 1 deletion apps/spotlight/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type AppContext = {
github: GithubRepository;
formConfig: FormConfig;
formService: FormService;
uswdsRoot: `${string}/`;
};

let _context: AppContext | null = null;
Expand All @@ -24,7 +25,7 @@ export const getAppContext = (): AppContext => {
return _context;
};

const createAppContext = (env: any) => {
const createAppContext = (env: any): AppContext => {
return {
github: env.GITHUB,
baseUrl: env.BASE_URL,
Expand Down
10 changes: 9 additions & 1 deletion packages/documents/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const addDocument = async (
const cachedPdf = await getSuggestedFormElementsFromCache(fileDetails.data);

if (cachedPdf) {
const updatedForm = addFormElements(
const withElements = addFormElements(
form,
[
...cachedPdf.elements,
Expand All @@ -39,6 +39,14 @@ export const addDocument = async (
],
'root'
);
const updatedForm = addFormOutput(withElements, {
data: fileDetails.data,
path: fileDetails.name,
fields: cachedPdf.outputs,
formFields: Object.fromEntries(
Object.keys(fields).map(field => [field, field])
),
});
// TODO: add form outputs
return {
newFields: fields,
Expand Down
42 changes: 36 additions & 6 deletions packages/documents/src/pdf/mock-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod';

import { type FormElement } from '@atj/forms';
import { type DocumentFieldMap, type FormElement } from '@atj/forms';
import { type InputElement } from '@atj/forms/src/config/elements/input';

import json from './al_name_change.json' assert { type: 'json' };
Expand Down Expand Up @@ -81,15 +81,45 @@ const ExtractedObject = z.object({
raw_fields: z.discriminatedUnion('type', [RawTxField, RawBtnField]).array(),
});

type ExtractedJsonType = z.infer<typeof ExtractedObject>;
type ExtractedObject = z.infer<typeof ExtractedObject>;

export type ParsedPdf = { elements: FormElement[] };
export type ParsedPdf = {
elements: FormElement[];
outputs: DocumentFieldMap; // to populate FormOutput
};

export const parseAlabamaNameChangeForm = (): ParsedPdf => {
const parsedPDF: ExtractedJsonType = ExtractedObject.parse(json);
return {
elements: parsedPDF.elements.flatMap(getElementInputs),
const extracted: ExtractedObject = ExtractedObject.parse(json);
const parsedPdf: ParsedPdf = {
elements: [],
outputs: {},
};
for (const element of extracted.elements) {
for (const input of element.inputs) {
if (input.input_type === 'Tx') {
const id = input.input_params.output_id.toLowerCase();
parsedPdf.elements.push({
type: 'input',
id,
default: {} as unknown as any,
data: {
text: input.input_params.instructions,
instructions: input.input_params.instructions,
},
required: true,
} satisfies FormElement<InputElement>);
parsedPdf.outputs[id] = {
type: 'TextField',
name: input.input_params.text,
label: input.input_params.instructions,
value: '',
maxLength: 1024,
required: input.input_params.required,
};
}
}
}
return parsedPdf;
};

const getElementInputs = (element: ExtractedElement): FormElement[] => {
Expand Down

0 comments on commit 69ab8ff

Please sign in to comment.