From 69ab8ffe9e413170b0cd61581890089271bb41d2 Mon Sep 17 00:00:00 2001 From: Daniel Naab Date: Tue, 27 Feb 2024 22:45:59 -0600 Subject: [PATCH] Add output document to the mock form --- apps/spotlight/src/context.ts | 3 +- packages/documents/src/document.ts | 10 +++++- packages/documents/src/pdf/mock-api.ts | 42 ++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/apps/spotlight/src/context.ts b/apps/spotlight/src/context.ts index 4c0c8c12..4b943864 100644 --- a/apps/spotlight/src/context.ts +++ b/apps/spotlight/src/context.ts @@ -13,6 +13,7 @@ export type AppContext = { github: GithubRepository; formConfig: FormConfig; formService: FormService; + uswdsRoot: `${string}/`; }; let _context: AppContext | null = null; @@ -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, diff --git a/packages/documents/src/document.ts b/packages/documents/src/document.ts index 8776b1f6..bcc0ae25 100644 --- a/packages/documents/src/document.ts +++ b/packages/documents/src/document.ts @@ -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, @@ -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, diff --git a/packages/documents/src/pdf/mock-api.ts b/packages/documents/src/pdf/mock-api.ts index 146960ac..5a2c013b 100644 --- a/packages/documents/src/pdf/mock-api.ts +++ b/packages/documents/src/pdf/mock-api.ts @@ -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' }; @@ -81,15 +81,45 @@ const ExtractedObject = z.object({ raw_fields: z.discriminatedUnion('type', [RawTxField, RawBtnField]).array(), }); -type ExtractedJsonType = z.infer; +type ExtractedObject = z.infer; -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); + 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[] => {