From f1c65498c0801c4127b80fd112fccf2f21e308db Mon Sep 17 00:00:00 2001 From: ipavic Date: Mon, 2 Sep 2024 10:07:07 +0200 Subject: [PATCH] Add support for merging yup form configurations instead of overriding them in store --- .changeset/strange-steaks-raise.md | 5 +++++ .../core/src/loader/fetch-form-configurations.ts | 11 +++++++++-- .../test/loader/fetch-form-configurations.test.ts | 5 +++-- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .changeset/strange-steaks-raise.md diff --git a/.changeset/strange-steaks-raise.md b/.changeset/strange-steaks-raise.md new file mode 100644 index 0000000..cdbce4b --- /dev/null +++ b/.changeset/strange-steaks-raise.md @@ -0,0 +1,5 @@ +--- +"@croz/nrich-form-configuration-core": patch +--- + +Add support for merging yup form configurations instead of overriding them in store diff --git a/libs/form-configuration/core/src/loader/fetch-form-configurations.ts b/libs/form-configuration/core/src/loader/fetch-form-configurations.ts index e6d2f2c..7dcf328 100644 --- a/libs/form-configuration/core/src/loader/fetch-form-configurations.ts +++ b/libs/form-configuration/core/src/loader/fetch-form-configurations.ts @@ -19,6 +19,13 @@ import { FormConfiguration, FormConfigurationConfiguration, FormYupConfiguration import { FormConfigurationValidationConverter } from "../converter"; import { useFormConfigurationStore } from "../store"; +const mergeFormYupConfigurationsWithoutDuplicates = (oldFormYupConfiguration: FormYupConfiguration[], newFormYupConfiguration: FormYupConfiguration[]) => { + const mergedFormYupConfigurations = [...oldFormYupConfiguration, ...newFormYupConfiguration]; + + // next line removes all duplicates based on formId + return [...new Map(mergedFormYupConfigurations.map((item) => [item.formId, item])).values()]; +}; + export const fetchFormConfigurations = async ({ url, requestOptionsResolver, additionalValidatorConverters }: FormConfigurationConfiguration): Promise => { const formConfigurationValidationConverter = new FormConfigurationValidationConverter(additionalValidatorConverters); const additionalOptions = requestOptionsResolver?.() || {}; @@ -28,7 +35,7 @@ export const fetchFormConfigurations = async ({ url, requestOptionsResolver, add method: "POST", ...additionalOptions, }); - const body = await response.json() as FormConfiguration[]; + const body = (await response.json()) as FormConfiguration[]; const formYupConfigurations: FormYupConfiguration[] = []; @@ -40,7 +47,7 @@ export const fetchFormConfigurations = async ({ url, requestOptionsResolver, add yupSchema: formConfigurationValidationConverter.convertFormConfigurationToYupSchema(item.constrainedPropertyConfigurationList), }); }); - useFormConfigurationStore.getState().set(formYupConfigurations); + useFormConfigurationStore.getState().set(mergeFormYupConfigurationsWithoutDuplicates(useFormConfigurationStore.getState().formYupConfigurations, formYupConfigurations)); useFormConfigurationStore.getState().setFormConfigurationLoaded(true); } diff --git a/libs/form-configuration/core/test/loader/fetch-form-configurations.test.ts b/libs/form-configuration/core/test/loader/fetch-form-configurations.test.ts index afda789..a55cb94 100644 --- a/libs/form-configuration/core/test/loader/fetch-form-configurations.test.ts +++ b/libs/form-configuration/core/test/loader/fetch-form-configurations.test.ts @@ -17,12 +17,13 @@ import { fetchFormConfigurations } from "../../src/loader"; import { useFormConfigurationStore } from "../../src/store"; -import { mockFormConfigurations, mockValidatorConverters } from "../testutil/form-configuration-generating-util"; +import { mockFormConfigurations, mockFormYupConfigurations, mockValidatorConverters } from "../testutil/form-configuration-generating-util"; import { setupFormConfigurationServer } from "../testutil/setup-form-configuration-server"; const server = setupFormConfigurationServer(); beforeAll(() => { + useFormConfigurationStore.getState().set([mockFormYupConfigurations[0]]); server.listen(); }); @@ -30,7 +31,7 @@ afterEach(() => server.resetHandlers()); afterAll(() => server.close()); describe("@croz/nrich-form-configuration-core/fetch-form-configurations", () => { - it("should resolve form configurations", async () => { + it("should resolve form configurations and not add duplicates to store", async () => { // when const response = await fetchFormConfigurations({ url: "/test-url", additionalValidatorConverters: mockValidatorConverters });