Skip to content

Commit

Permalink
Merge pull request #67 from croz-ltd/feature_fixAddingFormYupConfigur…
Browse files Browse the repository at this point in the history
…ationsToStore

Add support for merging yup form configurations instead of overriding them in store
  • Loading branch information
jtomic-croz authored Sep 9, 2024
2 parents 387a21c + e1bb4ce commit 8a6f70f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-apes-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@croz/nrich-form-configuration-core": minor
---

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 @@ -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<FormConfiguration[]> => {
const formConfigurationValidationConverter = new FormConfigurationValidationConverter(additionalValidatorConverters);
const additionalOptions = requestOptionsResolver?.() || {};
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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([]);
});
});

0 comments on commit 8a6f70f

Please sign in to comment.