Skip to content

Commit

Permalink
fix: populate anyOf only if we have options
Browse files Browse the repository at this point in the history
Signed-off-by: SeSo <[email protected]>
  • Loading branch information
Sepehr-Sobhani committed Jan 3, 2025
1 parent 91f80e9 commit c71cc1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ export const createContactSchema = (
// For now, we show the `existing_bciers_user` toggle and `selected_user` combobox only when creating a new contact. We should not show `places_assigned` when creating
if (isCreating) {
delete localSchema.properties.section1.properties.places_assigned;
const userOperatorUserOptions = userOperatorUsers?.map((user) => ({
const userOperatorUserOptions = userOperatorUsers.map((user) => ({
type: "string",
title: user.full_name,
enum: [user.id],
value: user.id,
}));

if (Array.isArray(userOperatorUserOptions)) {
if (
Array.isArray(userOperatorUserOptions) &&
userOperatorUserOptions.length > 0
) {
localSchema.properties.section1.allOf[0].then.properties.selected_user.anyOf =
userOperatorUserOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export const createRegistrationPurposeSchema = async () => {
const regulatedProducts: { id: number; name: string }[] =
await getRegulatedProducts();
const operations = await getCurrentUsersOperations();
// Using empty array for anyOf will cause the field to not show up and raise an error
let operationsAnyOf;
if (Array.isArray(operations) && operations.length > 0) {
operationsAnyOf = operations.map(
(operation: { id: UUID; name: string }) => ({
const: operation.id,
title: operation.name,
}),
);
}

const registrationPurposes = await getRegistrationPurposes();

// create the schema with the fetched values
Expand Down Expand Up @@ -51,10 +62,7 @@ export const createRegistrationPurposeSchema = async () => {
operation: {
type: "string",
title: "Select your operation:",
anyOf: operations.map((operation: { id: UUID; name: string }) => ({
const: operation.id,
title: operation.name,
})),
anyOf: operationsAnyOf,
},
operation_add: {
//Not an actual field in the db - this is just to make the form look like the wireframes
Expand Down

0 comments on commit c71cc1f

Please sign in to comment.