Skip to content

Commit

Permalink
fix unfound import issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ethangardner committed Dec 5, 2024
1 parent f2c2a45 commit e959ed3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
73 changes: 52 additions & 21 deletions packages/forms/src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type FieldsetPattern,
type PagePattern,
type PageSetPattern,
type RepeaterPattern,
type SequencePattern,
} from './patterns';
import { type Blueprint, type FormOutput, type FormSummary } from './types';
Expand Down Expand Up @@ -396,45 +397,75 @@ export const copyPattern = (
return { bp: updatedBp, pattern: newPattern };
};

export const addPatternToFieldset = (
export const addPatternToCompoundField = (
bp: Blueprint,
fieldsetPatternId: PatternId,
patternId: PatternId,
pattern: Pattern,
type: 'fieldset' | 'repeater',
index?: number
): Blueprint => {
const fieldsetPattern = bp.patterns[fieldsetPatternId] as FieldsetPattern;
if (fieldsetPattern.type !== 'fieldset') {
throw new Error('Pattern is not a page.');
const targetPattern = bp.patterns[patternId] as
| FieldsetPattern
| RepeaterPattern;
if (targetPattern.type !== type) {
throw new Error(`Pattern is not a ${type}.`);
}

let updatedPagePattern: PatternId[];

if (index !== undefined) {
updatedPagePattern = [
...fieldsetPattern.data.patterns.slice(0, index + 1),
pattern.id,
...fieldsetPattern.data.patterns.slice(index + 1),
];
} else {
updatedPagePattern = [...fieldsetPattern.data.patterns, pattern.id];
}
const updatedPatterns =
index !== undefined
? [
...targetPattern.data.patterns.slice(0, index + 1),
pattern.id,
...targetPattern.data.patterns.slice(index + 1),
]
: [...targetPattern.data.patterns, pattern.id];

return {
...bp,
patterns: {
...bp.patterns,
[fieldsetPattern.id]: {
...fieldsetPattern,
[targetPattern.id]: {
...targetPattern,
data: {
...fieldsetPattern.data,
patterns: updatedPagePattern,
...targetPattern.data,
patterns: updatedPatterns,
},
} satisfies FieldsetPattern,
} satisfies FieldsetPattern | RepeaterPattern,
[pattern.id]: pattern,
},
};
};

export const addPatternToFieldset = (
bp: Blueprint,
fieldsetPatternId: PatternId,
pattern: Pattern,
index?: number
): Blueprint => {
return addPatternToCompoundField(
bp,
fieldsetPatternId,
pattern,
'fieldset',
index
);
};

export const addPatternToRepeater = (
bp: Blueprint,
repeaterPatternId: PatternId,
pattern: Pattern,
index?: number
): Blueprint => {
return addPatternToCompoundField(
bp,
repeaterPatternId,
pattern,
'repeater',
index
);
};

export const addPageToPageSet = (
bp: Blueprint,
pattern: Pattern
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { type VoidResult } from '@atj/common';
import {
addPageToPageSet,
addPatternToFieldset,
addPatternToRepeater,
addPatternToPage,
addPatternToRepeater,
copyPattern,
createOnePageBlueprint,
movePatternBetweenPages,
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export {
type FormRoute,
type RouteData,
getRouteDataFromQueryString,
} from './route-data.js';
} from './route-data.js';
6 changes: 3 additions & 3 deletions packages/forms/src/services/submit-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ describe('submitForm', () => {
data: {
errors: {
'element-1': {
message: 'Required',
message: 'Invalid input',
type: 'custom',
},
'element-2': {
message: 'Required',
message: 'Invalid input',
type: 'custom',
},
},
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('multi-page form', () => {
data: {
errors: {
'element-1': {
message: 'Required',
message: 'Invalid input',
type: 'custom',
},
},
Expand Down

0 comments on commit e959ed3

Please sign in to comment.