Skip to content

Commit

Permalink
Add autofill logic for usageYear multiyear keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Onitoxan committed Nov 7, 2024
1 parent 20a2246 commit a42b027
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apps/hpc-ftsadmin/src/app/components/flow-form/flow-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
autofillOrganizations,
autofillPlan,
autofillProject,
autofillUsageYears,
} from '../../utils/fn-autofills';
import {
validateFlowForWarnings,
Expand Down Expand Up @@ -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={
Expand Down Expand Up @@ -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}
Expand Down
37 changes: 37 additions & 0 deletions apps/hpc-ftsadmin/src/app/utils/fn-autofills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
};

0 comments on commit a42b027

Please sign in to comment.