Skip to content

Commit

Permalink
Add support for merging yup form configurations instead of overriding…
Browse files Browse the repository at this point in the history
… them in store
  • Loading branch information
ipavic-croz committed Sep 2, 2024
1 parent 387a21c commit 4b2e39c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-steaks-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@croz/nrich-form-configuration-core": patch
---

Add support for merging yup form configurations instead of overriding them in store
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormConfiguration[]> => {
const formConfigurationValidationConverter = new FormConfigurationValidationConverter(additionalValidatorConverters);
const additionalOptions = requestOptionsResolver?.() || {};
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

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();
});

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 });

Expand Down

0 comments on commit 4b2e39c

Please sign in to comment.