Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form builder interface and organization #83

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/doj-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@astrojs/node": "^8.2.3",
"@astrojs/react": "^3.0.9",
"@atj/design": "workspace:*",
"@atj/documents": "workspace:*",
"@atj/form-service": "workspace:*",
"@atj/forms": "workspace:*",
"astro": "^4.4.11",
Expand Down
1 change: 0 additions & 1 deletion apps/spotlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"dependencies": {
"@astrojs/react": "^3.0.9",
"@atj/design": "workspace:*",
"@atj/documents": "workspace:*",
"@atj/form-service": "workspace:*",
"@atj/forms": "workspace:*",
"astro": "^4.3.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/spotlight/public/sample-documents
1 change: 0 additions & 1 deletion packages/design/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
},
"dependencies": {
"@atj/common": "workspace:*",
"@atj/documents": "workspace:*",
"@atj/form-service": "workspace:*",
"@atj/forms": "workspace:*",
"@dnd-kit/core": "^6.1.0",
Expand Down
13 changes: 4 additions & 9 deletions packages/design/src/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ const usePrompt = (
}
};
const updatePrompt = (data: Record<string, string>) => {
const result = applyPromptResponse(
config,
session,
{
action: 'submit',
data,
},
{ validate: true }
);
const result = applyPromptResponse(config, session, {
action: 'submit',
data,
});
if (!result.success) {
console.warn('Error applying prompt response...', result.error);
return;
Expand Down
8 changes: 3 additions & 5 deletions packages/design/src/FormManager/DocumentImporter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import React, { PropsWithChildren, useReducer } from 'react';
import { useNavigate } from 'react-router-dom';

import {
type DocumentFieldMap,
type FormDefinition,
SAMPLE_DOCUMENTS,
addDocument,
addDocumentFieldsToForm,
} from '@atj/documents';
import { type FormService } from '@atj/form-service';
import {
type DocumentFieldMap,
type FormDefinition,
createFormSession,
} from '@atj/forms';
import { type FormService } from '@atj/form-service';

import Form, { FormUIContext } from '../../Form';
import { onFileInputChangeGetFile } from '../FormList/PDFFileSelect/file-input';
Expand Down
10 changes: 5 additions & 5 deletions packages/design/src/FormManager/FormEdit/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type FormElementMap,
type Pattern,
getFormElement,
updateFormElement,
FormBuilder,
FormElement,
} from '@atj/forms';
import { type FormEditUIContext } from './types';
Expand Down Expand Up @@ -64,14 +64,14 @@ const createFormEditStore = ({
console.warn('No selected element');
return;
}
const updatedForm = updateFormElement(
const builder = new FormBuilder(state.form);
const success = builder.updateFormElement(
state.context.config,
state.form,
state.selectedElement,
formData
);
if (updatedForm) {
set({ form: updatedForm, selectedElement: undefined });
if (success) {
set({ form: builder.form, selectedElement: undefined });
}
},
}));
83 changes: 24 additions & 59 deletions packages/design/src/FormManager/FormList/PDFFileSelect/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { useNavigate } from 'react-router-dom';

import { type Result } from '@atj/common';
import { addDocument } from '@atj/documents';
import { FormElement, createForm } from '@atj/forms';
import { FormBuilder } from '@atj/forms';
import { type FormService } from '@atj/form-service';
import { FormSummary } from '@atj/forms/src/config/elements/form-summary';

export const useDocumentImporter = (
formService: FormService,
Expand All @@ -15,10 +12,19 @@ export const useDocumentImporter = (
return {
actions: {
stepOneSelectPdfByUrl: async (url: string) => {
const result = await stepOneSelectPdfByUrl(
{ formService, baseUrl },
url
);
const data = await fetchUint8Array(`${baseUrl}${url}`);

const builder = new FormBuilder();
builder.setFormSummary({
title: url,
description: '',
});
await builder.addDocument({
name: url,
data,
});

const result = formService.addForm(builder.form);
if (result.success) {
navigate(`/${result.data}/edit`);
}
Expand All @@ -27,10 +33,13 @@ export const useDocumentImporter = (
name: string;
data: Uint8Array;
}) => {
const result = await stepOneSelectPdfByUpload(
{ formService },
fileDetails
);
const builder = new FormBuilder();
builder.setFormSummary({
title: fileDetails.name,
description: '',
});
await builder.addDocument(fileDetails);
const result = await formService.addForm(builder.form);
if (result.success) {
navigate(`/${result.data}/edit`);
}
Expand All @@ -39,52 +48,8 @@ export const useDocumentImporter = (
};
};

export const stepOneSelectPdfByUrl = async (
ctx: { formService: FormService; baseUrl: string },
url: string
): Promise<Result<string>> => {
const completeUrl = `${ctx.baseUrl}${url}`;
const response = await fetch(completeUrl);
const fetchUint8Array = async (url: string) => {
const response = await fetch(url);
const blob = await response.blob();
const data = new Uint8Array(await blob.arrayBuffer());

const emptyForm = createForm({
title: url,
description: '',
});
const { updatedForm } = await addDocument(emptyForm, {
name: url,
data,
});
return ctx.formService.addForm(updatedForm);
};

export const stepOneSelectPdfByUpload = async (
ctx: { formService: FormService },
fileDetails: {
name: string;
data: Uint8Array;
}
): Promise<Result<string>> => {
const emptyForm = createForm(
{
title: fileDetails.name,
description: '',
},
{
root: 'root',
elements: [
{
type: 'form-summary',
id: 'form-summary',
data: {
title: fileDetails.name,
description: '',
},
} as FormElement<FormSummary>,
],
}
);
const { updatedForm } = await addDocument(emptyForm, fileDetails);
return ctx.formService.addForm(updatedForm);
return new Uint8Array(await blob.arrayBuffer());
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { SAMPLE_DOCUMENTS } from '@atj/documents';
import { SAMPLE_DOCUMENTS } from '@atj/forms';
import { FormService } from '@atj/form-service';

import { onFileInputChangeGetFile } from './file-input';
Expand Down
3 changes: 2 additions & 1 deletion packages/design/src/experiments/document-assembler.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { generateDummyPDF } from '@atj/documents';

import { generateDummyPDF } from '@atj/forms';

export const downloadPdfBytes = (bytes: Uint8Array) => {
const base64 = btoa(String.fromCharCode(...bytes));
Expand Down
2 changes: 1 addition & 1 deletion packages/docassemble/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test:integration": "vitest run --coverage"
},
"dependencies": {
"@atj/documents": "workspace:*"
"@atj/forms": "workspace:*"
},
"devDependencies": {
"@vitest/coverage-v8": "^0.34.6",
Expand Down
1 change: 0 additions & 1 deletion packages/documents/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/documents/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions packages/documents/package.json

This file was deleted.

18 changes: 0 additions & 18 deletions packages/documents/src/index.ts

This file was deleted.

Loading
Loading