Skip to content

Commit

Permalink
UISACQCOMP-220: ECS - expand ConsortiumFieldInventory component wit…
Browse files Browse the repository at this point in the history
…h `additionalAffiliationIds` prop to display affiliation name for User without affiliation in specific tenant
  • Loading branch information
alisher-epam committed Oct 9, 2024
1 parent 161e664 commit a3992ad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 18 additions & 2 deletions lib/ConsortiumFieldInventory/ConsortiumFieldInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import {
import { FieldInventory } from '../FieldInventory';
import {
useAffiliationsSelectionOptions,
useConsortiumTenants,
useCurrentUserTenants,
} from '../hooks';
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,
Expand All @@ -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);

Expand Down Expand Up @@ -113,6 +128,7 @@ export const ConsortiumFieldInventory = ({
};

ConsortiumFieldInventory.propTypes = {
additionalAffiliationIds: PropTypes.arrayOf(PropTypes.string),
affiliationLabel: PropTypes.node,
affiliationName: PropTypes.string.isRequired,
disabled: PropTypes.bool,
Expand Down
6 changes: 6 additions & 0 deletions lib/ConsortiumFieldInventory/ConsortiumFieldInventory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -45,7 +47,7 @@ export const useConsortiumTenants = (options = {}) => {
});

return ({
tenants: data?.tenants,
tenants: data?.tenants || DEFAULT_DATA,
totalRecords: data?.totalRecords,
isFetching,
isLoading,
Expand Down

0 comments on commit a3992ad

Please sign in to comment.