From c71cc1fb3c1590fdcf8b568acdb8908f1bc58e5b Mon Sep 17 00:00:00 2001 From: SeSo Date: Tue, 31 Dec 2024 11:13:30 -0800 Subject: [PATCH] fix: populate anyOf only if we have options Signed-off-by: SeSo --- .../components/contacts/createContactSchema.ts | 7 +++++-- .../operationInformation/registrationPurpose.ts | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/bciers/apps/administration/app/components/contacts/createContactSchema.ts b/bciers/apps/administration/app/components/contacts/createContactSchema.ts index 87b87bcf83..9b21c32db7 100644 --- a/bciers/apps/administration/app/components/contacts/createContactSchema.ts +++ b/bciers/apps/administration/app/components/contacts/createContactSchema.ts @@ -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; } diff --git a/bciers/apps/registration/app/data/jsonSchema/operationInformation/registrationPurpose.ts b/bciers/apps/registration/app/data/jsonSchema/operationInformation/registrationPurpose.ts index efc471eb6b..3ec2e9e39b 100644 --- a/bciers/apps/registration/app/data/jsonSchema/operationInformation/registrationPurpose.ts +++ b/bciers/apps/registration/app/data/jsonSchema/operationInformation/registrationPurpose.ts @@ -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 @@ -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