-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "package download" pattern, which provides a submit button to dow…
…nload a PDF package.
- Loading branch information
1 parent
576aa19
commit dd7596c
Showing
20 changed files
with
1,787 additions
and
1,452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/design/src/Form/components/PackageDownload/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
|
||
import { type PackageDownloadProps } from '@atj/forms'; | ||
|
||
import { type PatternComponent } from '../../../Form/index.js'; | ||
import ActionBar from '../../../Form/ActionBar/index.js'; | ||
|
||
const PackageDownload: PatternComponent<PackageDownloadProps> = props => { | ||
return ( | ||
<> | ||
<p className="maxw-tablet">{props.text}</p> | ||
<ActionBar actions={props.actions} /> | ||
</> | ||
); | ||
}; | ||
export default PackageDownload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/design/src/FormManager/FormEdit/components/PackageDownloadPatternEdit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import classnames from 'classnames'; | ||
import React from 'react'; | ||
|
||
import { enLocale as message } from '@atj/common'; | ||
import { | ||
type PackageDownloadPattern, | ||
type PackageDownloadProps, | ||
type PatternId, | ||
} from '@atj/forms'; | ||
|
||
import PackageDownload from '../../../Form/components/PackageDownload/index.js'; | ||
import { PatternEditComponent } from '../types.js'; | ||
|
||
import { PatternEditActions } from './common/PatternEditActions.js'; | ||
import { PatternEditForm } from './common/PatternEditForm.js'; | ||
import { usePatternEditFormContext } from './common/hooks.js'; | ||
import { useFormManagerStore } from '../../store.js'; | ||
|
||
const PackageDownloadPatternEdit: PatternEditComponent< | ||
PackageDownloadProps | ||
> = ({ focus, previewProps }) => { | ||
return ( | ||
<> | ||
{focus ? ( | ||
<PatternEditForm | ||
pattern={focus.pattern} | ||
editComponent={<EditComponent patternId={focus.pattern.id} />} | ||
></PatternEditForm> | ||
) : ( | ||
<div className="padding-left-3 padding-bottom-3 padding-right-3"> | ||
<PackageDownload {...previewProps} /> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
const EditComponent = ({ patternId }: { patternId: PatternId }) => { | ||
const pattern = useFormManagerStore<PackageDownloadPattern>( | ||
state => state.session.form.patterns[patternId] | ||
); | ||
const { fieldId, getFieldState, register } = | ||
usePatternEditFormContext<PackageDownloadPattern>(patternId); | ||
const text = getFieldState('text'); | ||
|
||
return ( | ||
<div className="grid-row grid-gap-1"> | ||
<div className="tablet:grid-col-12"> | ||
<label | ||
className={classnames('usa-label', { | ||
'usa-label--error': text.error, | ||
})} | ||
htmlFor={fieldId('text')} | ||
> | ||
{message.patterns.packageDownload.fieldLabel} | ||
</label> | ||
{text.error ? ( | ||
<span className="usa-error-message" role="alert"> | ||
{text.error.message} | ||
</span> | ||
) : null} | ||
<textarea | ||
id={fieldId('text')} | ||
className="usa-textarea bg-primary-lighter text-bold" | ||
style={{ height: 'unset' }} | ||
rows={4} | ||
{...register('text')} | ||
defaultValue={pattern.data.text} | ||
autoFocus | ||
></textarea> | ||
</div> | ||
<PatternEditActions /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PackageDownloadPatternEdit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,351 changes: 1,351 additions & 0 deletions
1,351
packages/forms/src/documents/__tests__/test-documents.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { type PackageDownloadPattern } from '.'; | ||
import { PatternBuilder } from '../../pattern'; | ||
|
||
export class PackageDownload extends PatternBuilder<PackageDownloadPattern> { | ||
toPattern(): PackageDownloadPattern { | ||
return { | ||
id: this.id, | ||
type: 'page-set', | ||
data: this.data, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as z from 'zod'; | ||
|
||
import { type Pattern, type PatternConfig } from '../../pattern.js'; | ||
import { type PackageDownloadProps } from '../../components.js'; | ||
import { type ActionName, getActionString } from '../../submission.js'; | ||
import { safeZodParseFormErrors } from '../../util/zod.js'; | ||
|
||
const configSchema = z.object({ | ||
text: z.string().min(1), | ||
}); | ||
export type PackageDownloadPattern = Pattern<z.infer<typeof configSchema>>; | ||
|
||
export const packageDownloadConfig: PatternConfig<PackageDownloadPattern> = { | ||
displayName: 'Package download', | ||
iconPath: 'block-icon.svg', | ||
initial: { | ||
text: 'Description text...', | ||
}, | ||
parseConfigData: obj => safeZodParseFormErrors(configSchema, obj), | ||
getChildren() { | ||
return []; | ||
}, | ||
createPrompt(_, session, pattern, options) { | ||
const actionName: ActionName = getActionString({ | ||
handlerId: 'page-set', | ||
patternId: pattern.id, | ||
}); | ||
return { | ||
props: { | ||
_patternId: pattern.id, | ||
type: 'package-download' as const, | ||
actions: [ | ||
{ | ||
type: 'submit', | ||
submitAction: actionName, | ||
text: 'Download PDF', | ||
}, | ||
], | ||
text: pattern.data.text, | ||
} as PackageDownloadProps, | ||
children: [], | ||
}; | ||
}, | ||
}; |
105 changes: 105 additions & 0 deletions
105
packages/forms/src/patterns/package-download/submit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { failure, success } from '@atj/common'; | ||
|
||
import { Blueprint, getDocumentFieldData, type FormSession } from '../..'; | ||
import { defaultFormConfig } from '../../..'; | ||
|
||
import { downloadPackageHandler } from './submit'; | ||
import { PackageDownload } from './builder'; | ||
import { PageSet } from '../page-set/builder'; | ||
import { Page } from '../page/builder'; | ||
import { Input } from '../input/builder'; | ||
import { loadSamplePDF } from '../../documents/__tests__/sample-data'; | ||
|
||
describe('downloadPackageHandler', async () => { | ||
it('returns failure when form is not complete', async () => { | ||
const session: FormSession = { | ||
form: await createTestForm(), | ||
data: { errors: {}, values: {} }, | ||
route: { url: '#', params: {} }, | ||
}; | ||
const result = await downloadPackageHandler(defaultFormConfig, { | ||
pattern: new PackageDownload({ text: 'Download now!' }).toPattern(), | ||
session, | ||
data: {}, | ||
}); | ||
|
||
expect(result).toEqual(failure('Form is not complete')); | ||
}); | ||
|
||
it('returns attachments on complete form', async () => { | ||
const session: FormSession = { | ||
form: await createTestForm(), | ||
data: { | ||
errors: {}, | ||
values: { | ||
'input-1': '53555', | ||
}, | ||
}, | ||
route: { url: '#', params: {} }, | ||
}; | ||
const result = await downloadPackageHandler(defaultFormConfig, { | ||
pattern: new PackageDownload({ text: 'Download now!' }).toPattern(), | ||
session, | ||
data: {}, | ||
}); | ||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
success: true, | ||
data: { | ||
session, | ||
attachments: [ | ||
{ | ||
fileName: 'test.pdf', | ||
data: expect.any(Uint8Array), | ||
}, | ||
], | ||
}, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
const createTestForm = async (): Promise<Blueprint> => { | ||
const pdfBytes = await loadSamplePDF( | ||
'doj-pardon-marijuana/application_for_certificate_of_pardon_for_simple_marijuana_possession.pdf' | ||
); | ||
const input1 = new Input( | ||
{ label: 'Input 1', required: true, maxLength: 10 }, | ||
'input-1' | ||
); | ||
const page1 = new Page({ title: 'Page 1', patterns: [input1.id] }, 'page-1'); | ||
const pageSet = new PageSet({ pages: [page1.id] }, 'page-set'); | ||
return { | ||
summary: { | ||
title: 'Test Form', | ||
description: 'A test form', | ||
}, | ||
root: 'page-set', | ||
patterns: { | ||
'page-set': pageSet.toPattern(), | ||
'page-1': page1.toPattern(), | ||
'input-1': input1.toPattern(), | ||
}, | ||
outputs: [ | ||
{ | ||
path: 'test.pdf', | ||
data: new Uint8Array(pdfBytes), | ||
fields: { | ||
[input1.id]: { | ||
type: 'TextField', | ||
name: 'Zip Code', | ||
label: 'Zip Code', | ||
maxLength: 32, | ||
value: 'zipCode', | ||
required: true, | ||
}, | ||
}, | ||
formFields: { | ||
[input1.id]: 'Zip Code', | ||
}, | ||
}, | ||
], | ||
}; | ||
}; |
Oops, something went wrong.