From a42b0272906c9fc98f0dbd2adf330e4f2825bf0d Mon Sep 17 00:00:00 2001 From: Onitoxan Date: Wed, 6 Nov 2024 15:07:12 +0100 Subject: [PATCH] Add autofill logic for usageYear multiyear keyword --- .../app/components/flow-form/flow-form.tsx | 19 ++++++++++ .../src/app/utils/fn-autofills.ts | 37 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/apps/hpc-ftsadmin/src/app/components/flow-form/flow-form.tsx b/apps/hpc-ftsadmin/src/app/components/flow-form/flow-form.tsx index 33d1e25d0..36fe73ba3 100644 --- a/apps/hpc-ftsadmin/src/app/components/flow-form/flow-form.tsx +++ b/apps/hpc-ftsadmin/src/app/components/flow-form/flow-form.tsx @@ -53,6 +53,7 @@ import { autofillOrganizations, autofillPlan, autofillProject, + autofillUsageYears, } from '../../utils/fn-autofills'; import { validateFlowForWarnings, @@ -623,6 +624,15 @@ export const FlowForm = (props: FlowFormProps) => { fieldName="fundingSourceUsageYears" label="Usage Year(s)" fnPromise={() => fnUsageYears(env)} + onChange={(newValue) => + autofillUsageYears({ + fieldName: 'fundingSourceUsageYears', + setFieldValue, + values, + env, + newValue, + }) + } isAutocompleteAPI={false} disabled={isDisabled || !!values.parentFlow} pendingValues={ @@ -1085,6 +1095,15 @@ export const FlowForm = (props: FlowFormProps) => { fieldName="fundingDestinationUsageYears" label="Usage Year(s)" fnPromise={() => fnUsageYears(env)} + onChange={(newValue) => + autofillUsageYears({ + fieldName: 'fundingDestinationUsageYears', + setFieldValue, + values, + env, + newValue, + }) + } isAutocompleteAPI={false} disabled={isDisabled} pendingValues={pendingValues?.fundingDestinationUsageYears} diff --git a/apps/hpc-ftsadmin/src/app/utils/fn-autofills.ts b/apps/hpc-ftsadmin/src/app/utils/fn-autofills.ts index 42d8c6534..7379821f4 100644 --- a/apps/hpc-ftsadmin/src/app/utils/fn-autofills.ts +++ b/apps/hpc-ftsadmin/src/app/utils/fn-autofills.ts @@ -456,3 +456,40 @@ export const autofillGlobalClusters = async ({ globalClusters ); }; + +export const autofillUsageYears = async ({ + fieldName, + setFieldValue, + values, + env, + newValue, +}: AutofillProps) => { + setFieldValue(fieldName, newValue); + + // usageYears field is multi select + if ( + !newValue || + typeof newValue === 'string' || + !Array.isArray(newValue) || + newValue.length < 2 || + values.keywords.some((keyword) => keyword.displayLabel === 'Multiyear') + ) { + return; + } + const multiyear = await env.model.categories + .getKeywords() + .then((keywords) => + keywords.find((keyword) => keyword.name === 'Multiyear') + ); + + if (!multiyear) { + return; + } + setFieldValue('keywords', [ + ...values.keywords, + { + displayLabel: multiyear.name, + value: multiyear.id, + } satisfies FormObjectValue, + ]); +};