Skip to content

Commit

Permalink
working radio button mapping, ignore empty non-required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmoffet committed Oct 11, 2024
1 parent 1e0ceeb commit 23c8873
Show file tree
Hide file tree
Showing 5 changed files with 10,707 additions and 15,552 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
"tsup": "^8.2.4",
"turbo": "^2.0.14",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"vitest": "^2.1.2",
"vitest-mock-extended": "^2.0.0"
},
"dependencies": {
"playwright": "^1.47.2"
}
}
}
28 changes: 20 additions & 8 deletions packages/forms/src/documents/pdf/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PDFDocument, type PDFForm } from 'pdf-lib';
import {
PDFDocument,
PDFName,
createPDFAcroFields,
type PDFForm,
} from 'pdf-lib';

import { Result } from '@atj/common';
import { type FormOutput } from '../../index.js';
Expand Down Expand Up @@ -119,14 +124,21 @@ const setFormFieldData = (
const field = form.getRadioGroup(fieldName);
field.select(fieldValue);
} catch (error: any) {
console.error(
`error setting radio field: ${fieldName}: ${error.message}`
);
const field = form.getCheckBox(fieldName);
if (fieldValue) {
field.check();
// This logic should work even if pdf-lib misidentifies the field type
// TODO: radioParent should contain the name, not the id
const [radioParent, radioChild] = fieldValue.split('.');
if (radioChild) {
// TODO: resolve import failure when spaces are present in name, id
const radioChildWithSpace = radioChild.replace('_', ' ');
const field = form.getField(fieldName);
const acroField = field.acroField;
acroField.dict.set(PDFName.of('V'), PDFName.of(radioChildWithSpace));
const kids = createPDFAcroFields(acroField.Kids()).map(_ => _[0]);
kids.forEach(kid => {
kid.dict.set(PDFName.of('AS'), PDFName.of(radioChildWithSpace));
});
} else {
field.uncheck();
// do nothing
}
}
} else if (fieldType === 'Paragraph' || fieldType === 'RichText') {
Expand Down
7 changes: 3 additions & 4 deletions packages/forms/src/documents/pdf/parsing-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const RadioGroupOption = z.object({
});

const RadioGroup = z.object({
// id: z.string(),
id: z.string(),
component_type: z.literal('radio_group'),
legend: z.string(),
options: RadioGroupOption.array(),
Expand Down Expand Up @@ -110,7 +110,7 @@ export type FetchPdfApiResponse = (

export const fetchPdfApiResponse: FetchPdfApiResponse = async (
rawData: Uint8Array,
url: string = 'https://10x-atj-doc-automation-staging.app.cloud.gov/api/v2/parse' // 'http://localhost:5000/api/v2/parse'
url: string = 'http://localhost:5000/api/v2/parse' // 'https://10x-atj-doc-automation-staging.app.cloud.gov/api/v2/parse'
) => {
const base64 = await uint8ArrayToBase64(rawData);
const response = await fetch(url, {
Expand Down Expand Up @@ -238,7 +238,7 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
pagePatterns[element.page] = (pagePatterns[element.page] || []).concat(
radioGroupPattern.id
);
/*

parsedPdf.outputs[radioGroupPattern.id] = {
type: 'RadioGroup',
name: element.id,
Expand All @@ -252,7 +252,6 @@ export const processApiResponse = async (json: any): Promise<ParsedPdf> => {
value: '',
required: true,
};
*/
}
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/forms/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ export const validatePattern = (
}
const parseResult = patternConfig.parseUserInput(pattern, value);
if (!parseResult.success) {
if ('required' in pattern.data) {
if (pattern.data.required === false && !value) {
console.log('Parse result failed for blank input that is not required');
return {
success: true,
data: value,
};
}
}
return {
success: false,
error: parseResult.error,
Expand Down
Loading

0 comments on commit 23c8873

Please sign in to comment.