From b74f6e68f20c0afe19f1aad68eead5de6164a7c1 Mon Sep 17 00:00:00 2001 From: Tim Arney Date: Mon, 9 Dec 2024 09:23:12 -0500 Subject: [PATCH] chore: move store setup to standalone function (#4799) move store setup to standalone function --- lib/store/initStore.ts | 49 ++++++++++++++++++++++++++++++++++ lib/store/useTemplateStore.tsx | 42 +++-------------------------- 2 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 lib/store/initStore.ts diff --git a/lib/store/initStore.ts b/lib/store/initStore.ts new file mode 100644 index 0000000000..c0a1a24ccc --- /dev/null +++ b/lib/store/initStore.ts @@ -0,0 +1,49 @@ +import { TemplateStoreProps, InitialTemplateStoreProps } from "./types"; +import { Language } from "../types/form-builder-types"; +import { defaultForm } from "./defaults"; +import { initializeGroups } from "@formBuilder/components/shared/right-panel/treeview/util/initializeGroups"; +import { orderGroups } from "@lib/utils/form-builder/orderUsingGroupsLayout"; + +export const initStore = (initProps?: Partial) => { + const DEFAULT_PROPS: TemplateStoreProps = { + id: "", + lang: (initProps?.locale as Language) || "en", + translationLanguagePriority: (initProps?.locale as Language) || "en", + focusInput: false, + hasHydrated: false, + form: defaultForm, + isPublished: false, + name: "", + securityAttribute: "Protected A", + formPurpose: "", + publishReason: "", + publishFormType: "", + publishDesc: "", + closingDate: initProps?.closingDate, + changeKey: String(new Date().getTime()), + allowGroupsFlag: initProps?.allowGroupsFlag || false, + }; + + // Ensure any required properties by Form Builder are defaulted by defaultForm + if (initProps?.form) { + initProps.form = { + ...defaultForm, + ...initProps?.form, + }; + + initProps.form = initializeGroups(initProps.form, initProps?.allowGroupsFlag || false); + + // Ensure order by groups layout + if (!initProps.form.groupsLayout) { + /* No need to order as the groups layout does not exist */ + initProps.form.groupsLayout = []; + } else { + initProps.form.groups = orderGroups(initProps.form.groups, initProps.form.groupsLayout); + } + } + + return { + ...DEFAULT_PROPS, + ...initProps, + }; +}; diff --git a/lib/store/useTemplateStore.tsx b/lib/store/useTemplateStore.tsx index b457600fcb..8a9d8ddef8 100644 --- a/lib/store/useTemplateStore.tsx +++ b/lib/store/useTemplateStore.tsx @@ -42,52 +42,16 @@ import { defaultField, defaultForm } from "./defaults"; import { storage } from "./storage"; import { clearTemplateStorage } from "./utils"; import { orderGroups } from "@lib/utils/form-builder/orderUsingGroupsLayout"; +import { initStore } from "./initStore"; const createTemplateStore = (initProps?: Partial) => { - const DEFAULT_PROPS: TemplateStoreProps = { - id: "", - lang: (initProps?.locale as Language) || "en", - translationLanguagePriority: (initProps?.locale as Language) || "en", - focusInput: false, - hasHydrated: false, - form: defaultForm, - isPublished: false, - name: "", - securityAttribute: "Protected A", - formPurpose: "", - publishReason: "", - publishFormType: "", - publishDesc: "", - closingDate: initProps?.closingDate, - changeKey: String(new Date().getTime()), - allowGroupsFlag: initProps?.allowGroupsFlag || false, - }; - - // Ensure any required properties by Form Builder are defaulted by defaultForm - if (initProps?.form) { - initProps.form = { - ...defaultForm, - ...initProps?.form, - }; - - initProps.form = initializeGroups(initProps.form, initProps?.allowGroupsFlag || false); - - // Ensure order by groups layout - if (!initProps.form.groupsLayout) { - /* No need to order as the groups layout does not exist */ - initProps.form.groupsLayout = []; - } else { - initProps.form.groups = orderGroups(initProps.form.groups, initProps.form.groupsLayout); - } - } - + const props = initStore(initProps); return createStore()( immer( subscribeWithSelector( persist( (set, get) => ({ - ...DEFAULT_PROPS, - ...initProps, + ...props, setChangeKey: (key: string) => { set((state) => { state.changeKey = key;