diff --git a/.changeset/angry-apes-fail.md b/.changeset/angry-apes-fail.md new file mode 100644 index 0000000..e783079 --- /dev/null +++ b/.changeset/angry-apes-fail.md @@ -0,0 +1,5 @@ +--- +"@croz/nrich-form-configuration-core": minor +--- + +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..2ad5d2a 100644 --- a/libs/form-configuration/core/src/loader/fetch-form-configurations.ts +++ b/libs/form-configuration/core/src/loader/fetch-form-configurations.ts @@ -15,10 +15,18 @@ * */ +import _ from "lodash"; + import { FormConfiguration, FormConfigurationConfiguration, FormYupConfiguration } from "../api"; import { FormConfigurationValidationConverter } from "../converter"; import { useFormConfigurationStore } from "../store"; +const mergeFormYupConfigurationsWithoutDuplicates = (oldFormYupConfiguration: FormYupConfiguration[], newFormYupConfiguration: FormYupConfiguration[]) => { + const mergedFormYupConfigurations = [...oldFormYupConfiguration, ...newFormYupConfiguration]; + + return _.uniqBy(mergedFormYupConfigurations, "formId"); +}; + export const fetchFormConfigurations = async ({ url, requestOptionsResolver, additionalValidatorConverters }: FormConfigurationConfiguration): Promise => { const formConfigurationValidationConverter = new FormConfigurationValidationConverter(additionalValidatorConverters); const additionalOptions = requestOptionsResolver?.() || {}; @@ -40,7 +48,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..82892cf 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,7 +17,7 @@ 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(); @@ -49,4 +49,24 @@ describe("@croz/nrich-form-configuration-core/fetch-form-configurations", () => // cleanup formConfigurationState.set([]); }); + + it("should resolve form configurations and add them to store with ignoring duplicates", async () => { + // given + useFormConfigurationStore.getState().set([mockFormYupConfigurations[0]]); + + // when + const response = await fetchFormConfigurations({ url: "/test-url", additionalValidatorConverters: mockValidatorConverters }); + + // then + expect(response).toHaveLength(2); + + // and when + const formConfigurationState = useFormConfigurationStore.getState(); + + // then + expect(formConfigurationState.formYupConfigurations).toHaveLength(2); + + // cleanup + formConfigurationState.set([]); + }); });