Skip to content

Commit

Permalink
chore: move store setup to standalone function (#4799)
Browse files Browse the repository at this point in the history
move store setup to standalone function
  • Loading branch information
timarney authored Dec 9, 2024
1 parent efe6d7d commit b74f6e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
49 changes: 49 additions & 0 deletions lib/store/initStore.ts
Original file line number Diff line number Diff line change
@@ -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<InitialTemplateStoreProps>) => {
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,
};
};
42 changes: 3 additions & 39 deletions lib/store/useTemplateStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<InitialTemplateStoreProps>) => {
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<TemplateStoreState>()(
immer(
subscribeWithSelector(
persist(
(set, get) => ({
...DEFAULT_PROPS,
...initProps,
...props,
setChangeKey: (key: string) => {
set((state) => {
state.changeKey = key;
Expand Down

0 comments on commit b74f6e6

Please sign in to comment.