From c436579d79c534025bc6a86baadbcabe6a162772 Mon Sep 17 00:00:00 2001 From: Jim Moffet Date: Thu, 6 Jun 2024 09:42:05 -0700 Subject: [PATCH] Missing radio groups (#167) * checkboxes working, radios partially working * remove comments and debug vars * remove superfluous linebreak * log error * comment --- packages/forms/src/documents/pdf/generate.ts | 20 +++++++++-- .../forms/src/documents/pdf/parsing-api.ts | 33 +++++++++++++++---- packages/forms/src/documents/types.ts | 2 +- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/packages/forms/src/documents/pdf/generate.ts b/packages/forms/src/documents/pdf/generate.ts index 947be908..dabfa55a 100644 --- a/packages/forms/src/documents/pdf/generate.ts +++ b/packages/forms/src/documents/pdf/generate.ts @@ -11,11 +11,12 @@ export const createFormOutputFieldData = ( const results = {} as Record; Object.entries(output.fields).forEach(([patternId, docField]) => { if (docField.type === 'not-supported') { + console.error(`unsupported field: ${patternId}: ${docField}`); return; } const outputFieldId = output.formFields[patternId]; if (outputFieldId === '') { - console.error(`empty outputFieldId for patternId: ${patternId}`); + console.error(`empty outputFieldId for field: ${patternId}: ${docField}`); return; } results[outputFieldId] = { @@ -71,8 +72,21 @@ const setFormFieldData = ( const field = form.getDropdown(fieldName); field.select(fieldValue); } else if (fieldType === 'RadioGroup') { - const field = form.getRadioGroup(fieldName); - field.select(fieldValue); + // TODO: remove this when we have a better way to handle radio groups + try { + 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(); + } else { + field.uncheck(); + } + } } else if (fieldType === 'Paragraph') { // do nothing } else { diff --git a/packages/forms/src/documents/pdf/parsing-api.ts b/packages/forms/src/documents/pdf/parsing-api.ts index 2e8141cb..3ab8b019 100644 --- a/packages/forms/src/documents/pdf/parsing-api.ts +++ b/packages/forms/src/documents/pdf/parsing-api.ts @@ -216,7 +216,7 @@ export const processApiResponse = async (json: any): Promise => { } if (element.component_type === 'checkbox') { - const checkbox = processPatternData( + const checkboxPattern = processPatternData( defaultFormConfig, parsedPdf, 'checkbox', @@ -225,19 +225,27 @@ export const processApiResponse = async (json: any): Promise => { defaultChecked: element.default_checked, } ); - if (checkbox) { - rootSequence.push(checkbox.id); + if (checkboxPattern) { + rootSequence.push(checkboxPattern.id); + parsedPdf.outputs[checkboxPattern.id] = { + type: 'CheckBox', + name: element.id, + label: element.label, + value: false, + required: true, + }; } continue; } if (element.component_type === 'radio_group') { - const radioGroup = processPatternData( + const radioGroupPattern = processPatternData( defaultFormConfig, parsedPdf, 'radio-group', { label: element.legend, + // outputId: element.id, options: element.options.map(option => ({ id: option.id, label: option.label, @@ -246,8 +254,21 @@ export const processApiResponse = async (json: any): Promise => { })), } ); - if (radioGroup) { - rootSequence.push(radioGroup.id); + if (radioGroupPattern) { + rootSequence.push(radioGroupPattern.id); + parsedPdf.outputs[radioGroupPattern.id] = { + type: 'RadioGroup', + name: element.id, + label: element.legend, + options: element.options.map(option => ({ + id: option.id, + label: option.label, + name: option.name, + defaultChecked: option.default_checked, + })), + value: '', + required: true, + }; } continue; } diff --git a/packages/forms/src/documents/types.ts b/packages/forms/src/documents/types.ts index 658aa192..350665c1 100644 --- a/packages/forms/src/documents/types.ts +++ b/packages/forms/src/documents/types.ts @@ -31,7 +31,7 @@ export type DocumentFieldValue = | { type: 'RadioGroup'; name: string; - options: string[]; + options: any[]; label: string; value: string; required: boolean;