Skip to content

Commit

Permalink
handle next element id
Browse files Browse the repository at this point in the history
  • Loading branch information
timarney committed Nov 25, 2024
1 parent 4792a62 commit 529cd27
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/store/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export const defaultForm = {
},
layout: [],
elements: [],
nextElementId: 1,
groups: {},
};
1 change: 1 addition & 0 deletions lib/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface TemplateStoreState extends TemplateStoreProps {
setChangeKey: (key: string) => void;
getGroupsEnabled: () => boolean;
setGroupsLayout: (layout: string[]) => void;
incrementNextElementId: () => void;
}

export interface InitialTemplateStoreProps extends TemplateStoreProps {
Expand Down
20 changes: 17 additions & 3 deletions lib/store/useTemplateStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
moveElementUp,
removeElementById,
removeById,
incrementElementId,
getSchemaFromState,
incrementSubElementId,
cleanInput,
Expand Down Expand Up @@ -80,6 +79,11 @@ const createTemplateStore = (initProps?: Partial<InitialTemplateStoreProps>) =>
} else {
initProps.form.groups = orderGroups(initProps.form.groups, initProps.form.groupsLayout);
}

// Handle legacy forms to ensure nextElementId is set correctly
if (initProps.form.nextElementId === 1 && initProps.form.elements.length == 1) {
initProps.form.nextElementId = initProps.form.elements.length + 1;
}
}

return createStore<TemplateStoreState>()(
Expand Down Expand Up @@ -212,11 +216,13 @@ const createTemplateStore = (initProps?: Partial<InitialTemplateStoreProps>) =>
});
},
add: async (elIndex = 0, type = FormElementTypes.radio, data, groupId) => {
const id = get().form.nextElementId;
get().incrementNextElementId();

return new Promise((resolve) => {
set((state) => {
const allowGroups = state.allowGroupsFlag;

const id = incrementElementId(state.form.elements);
const item = {
...defaultField,
...data,
Expand Down Expand Up @@ -365,8 +371,9 @@ const createTemplateStore = (initProps?: Partial<InitialTemplateStoreProps>) =>
},
duplicateElement: (itemId, groupId = "", copyEn = "", copyFr = "") => {
const elIndex = get().form.elements.findIndex((el) => el.id === itemId);
const id = get().form.nextElementId;
get().incrementNextElementId();
set((state) => {
const id = incrementElementId(state.form.elements);
// deep copy the element
const element = JSON.parse(JSON.stringify(state.form.elements[elIndex]));
element.id = id;
Expand Down Expand Up @@ -497,6 +504,13 @@ const createTemplateStore = (initProps?: Partial<InitialTemplateStoreProps>) =>
state.form.groupsLayout = layout;
});
},
incrementNextElementId: () => {
set((state) => {
// Increment nextElementId
const nextElementId = state.form.nextElementId;
state.form.nextElementId = nextElementId + 1;
});
},
}),
{
name: "form-storage",
Expand Down
1 change: 1 addition & 0 deletions lib/types/form-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export interface FormProperties {
groups?: GroupsType;
groupsLayout?: string[];
elements: FormElement[];
nextElementId: number;
brand?: BrandProperties;
formPurpose?: string;
[key: string]:
Expand Down
8 changes: 0 additions & 8 deletions lib/utils/form-builder/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
sortByLayout,
incrementElementId,
swap,
moveUp,
moveDown,
Expand Down Expand Up @@ -37,13 +36,6 @@ describe("Util", () => {
expect(sorted4).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]);
});

it("increments element id", () => {
expect(incrementElementId([])).toBe(1);
expect(incrementElementId([{ id: 3 }])).toBe(4);
expect(incrementElementId([{ id: 1 }, { id: 3 }, { id: 2 }])).toBe(4);
expect(incrementElementId([{ id: 6 }, { id: 8 }, { id: 4 }])).toBe(9);
});

it("increments subElement id", () => {
expect(incrementSubElementId([], 4)).toBe(401);
expect(incrementSubElementId([], 5)).toBe(501);
Expand Down
10 changes: 2 additions & 8 deletions lib/utils/form-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ export const swapElement = (arr: FormElement[], index1: number, index2: number)
});
};

export const incrementElementId = (elements: FormElement[]) => {
if (!elements || !elements.length) {
return 1;
}
const ids = elements.map((element) => element.id).sort((a, b) => a - b);
return ids[ids.length - 1] + 1;
};

export const incrementSubElementId = (subElements: FormElement[], elId: number) => {
if (!subElements || !subElements.length) {
return Number(elId.toString() + "01");
Expand Down Expand Up @@ -126,6 +118,7 @@ export const getSchemaFromState = (state: TemplateStoreState, allowGroups = fals
layout,
groups,
groupsLayout,
nextElementId,
},
} = state;

Expand All @@ -141,6 +134,7 @@ export const getSchemaFromState = (state: TemplateStoreState, allowGroups = fals
brand,
groups,
groupsLayout,
nextElementId,
};

// Force this is off until a enable in a follow-up PR
Expand Down

0 comments on commit 529cd27

Please sign in to comment.