From a3992ad3b5e586773c5655b7f4d18631845ade34 Mon Sep 17 00:00:00 2001 From: Alisher Musurmonov Date: Wed, 9 Oct 2024 11:04:14 +0500 Subject: [PATCH] UISACQCOMP-220: ECS - expand `ConsortiumFieldInventory` component with `additionalAffiliationIds` prop to display affiliation name for User without affiliation in specific tenant --- CHANGELOG.md | 1 + .../ConsortiumFieldInventory.js | 20 +++++++++++++++++-- .../ConsortiumFieldInventory.test.js | 6 ++++++ .../useConsortiumTenants.js | 4 +++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f1b9b1..c3086e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * ECS - Add `isLoading` prop for `ConsortiumFieldInventory` component. Refs UISACQCOMP-215. * Add "Amount must be a positive number" validation for "Set exchange rate" field. Refs UISACQCOMP-218. * Create common utilities for managing response errors. Refs UISACQCOMP-219. +* ECS - expand `ConsortiumFieldInventory` component with `additionalAffiliationIds` prop to display affiliation name for User without affiliation in specific tenant. Refs UISACQCOMP-220. ## [5.1.2](https://github.com/folio-org/stripes-acq-components/tree/v5.1.2) (2024-09-13) [Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v5.1.1...v5.1.2) diff --git a/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js b/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js index a802b064..60b599c0 100644 --- a/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js +++ b/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js @@ -19,6 +19,7 @@ import { import { FieldInventory } from '../FieldInventory'; import { useAffiliationsSelectionOptions, + useConsortiumTenants, useCurrentUserTenants, } from '../hooks'; import { FieldAffiliation } from '../FieldAffiliation'; @@ -26,6 +27,8 @@ import { FieldAffiliation } from '../FieldAffiliation'; import css from './ConsortiumFieldInventory.css'; export const ConsortiumFieldInventory = ({ + // Additional affiliations to be displayed in the dropdown if the current user is not affiliated with them which is defined by UIOR-1311 + additionalAffiliationIds = [], affiliationLabel, affiliationName, disabled = false, @@ -43,14 +46,26 @@ export const ConsortiumFieldInventory = ({ const { batch, change } = useForm(); const { values } = useFormState(); const currUserTenants = useCurrentUserTenants(); + const { tenants } = useConsortiumTenants(); const affiliations = useMemo(() => { - return currUserTenants?.map(({ id, name, isPrimary }) => ({ + let tenantList = currUserTenants; + const isAdditionalAffiliationsMissing = additionalAffiliationIds.some((id) => { + return !currUserTenants?.find(({ id: tenantId }) => tenantId === id); + }); + + if (additionalAffiliationIds.length && isAdditionalAffiliationsMissing) { + const additionalAffiliations = tenants?.filter(({ id }) => additionalAffiliationIds.includes(id)); + + tenantList = [...additionalAffiliations, ...currUserTenants]; + } + + return tenantList?.map(({ id, name, isPrimary }) => ({ tenantId: id, tenantName: name, isPrimary, })); - }, [currUserTenants]); + }, [additionalAffiliationIds, currUserTenants, tenants]); const { dataOptions } = useAffiliationsSelectionOptions(affiliations); @@ -113,6 +128,7 @@ export const ConsortiumFieldInventory = ({ }; ConsortiumFieldInventory.propTypes = { + additionalAffiliationIds: PropTypes.arrayOf(PropTypes.string), affiliationLabel: PropTypes.node, affiliationName: PropTypes.string.isRequired, disabled: PropTypes.bool, diff --git a/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js b/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js index badeb4ac..06e71efb 100644 --- a/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js +++ b/lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js @@ -64,4 +64,10 @@ describe('ConsortiumFieldInventory', () => { expect(onAffiliationChange).toHaveBeenCalled(); }); + + it('should render affiliations with additionalAffiliationIds', () => { + renderConsortiumFieldInventory({ additionalAffiliationIds: [tenants[1].id] }); + + expect(screen.getByText(tenants[1].name)).toBeInTheDocument(); + }); }); diff --git a/lib/hooks/consortia/useConsortiumTenants/useConsortiumTenants.js b/lib/hooks/consortia/useConsortiumTenants/useConsortiumTenants.js index 380ac6e0..26ffbbcd 100644 --- a/lib/hooks/consortia/useConsortiumTenants/useConsortiumTenants.js +++ b/lib/hooks/consortia/useConsortiumTenants/useConsortiumTenants.js @@ -11,6 +11,8 @@ import { CONSORTIA_CONSORTIUM_TENANTS_API, } from '../../../constants'; +const DEFAULT_DATA = []; + export const useConsortiumTenants = (options = {}) => { const stripes = useStripes(); const [namespace] = useNamespace({ key: 'consortium-tenants' }); @@ -45,7 +47,7 @@ export const useConsortiumTenants = (options = {}) => { }); return ({ - tenants: data?.tenants, + tenants: data?.tenants || DEFAULT_DATA, totalRecords: data?.totalRecords, isFetching, isLoading,