Skip to content

Commit

Permalink
UIIN-2647: Show only local authorities when share local instance (#2327)
Browse files Browse the repository at this point in the history
* UIIN-2647: Show only local authorities when share local instance

* UIIN-2647: Update CHANGELOG.md

* UIIN-2647: Add brackets to query

* UIIN-2647: Fix test
  • Loading branch information
OleksandrHladchenko1 authored Nov 6, 2023
1 parent 6515018 commit 9e781df
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Optimistic locking message not working for instances in non-consortial tenant. Fixes UIIN-2628.
* Add immediate warning message when a local instance is shared. Refs UIIN-2617.
* ECS: Show info message when user in member tenant tries to view shared instance details without permission. Refs UIIN-2590.
* Show only local MARC Authorities when share local instance. Fixes UIIN-2647.

## [10.0.1](https://github.com/folio-org/ui-inventory/tree/v10.0.1) (2023-11-03)
[Full Changelog](https://github.com/folio-org/ui-inventory/compare/v10.0.0...v10.0.1)
Expand Down
56 changes: 28 additions & 28 deletions src/ViewInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import {
flowRight,
isEmpty,
pick,
} from 'lodash';

import {
Expand Down Expand Up @@ -38,9 +37,10 @@ import {
handleKeyCommand,
isInstanceShadowCopy,
isMARCSource,
getLinkedAuthorityIds,
} from './utils';
import {
AUTHORITY_LINKED_FIELDS,
CONSORTIUM_PREFIX,
HTTP_RESPONSE_STATUS_CODES,
indentifierTypeNames,
INSTANCE_SHARING_STATUSES,
Expand Down Expand Up @@ -170,6 +170,12 @@ class ViewInstance extends React.Component {
accumulate: true,
throwErrors: false,
},
authorities: {
type: 'okapi',
path: 'authority-storage/authorities',
accumulate: true,
throwErrors: false,
}
});

constructor(props) {
Expand Down Expand Up @@ -522,34 +528,25 @@ class ViewInstance extends React.Component {
}

checkIfHasLinkedAuthorities = (instance = {}) => {
const linkedAuthorities = pick(this.props.selectedInstance, AUTHORITY_LINKED_FIELDS);

const findLinkedAuthorities = authorities => {
const linkedAuthoritiesLength = AUTHORITY_LINKED_FIELDS.reduce((total, field) => {
const authoritiesAmount = authorities[field].filter(item => item.authorityId).length;
return total + authoritiesAmount;
}, 0);
const authorityIds = getLinkedAuthorityIds(instance).join(' or ');

return {
hasLinkedAuthorities: !!linkedAuthoritiesLength,
linkedAuthoritiesLength,
};
};

const {
hasLinkedAuthorities,
linkedAuthoritiesLength,
} = findLinkedAuthorities(linkedAuthorities);
this.props.mutator.authorities.GET({
params: {
query: `id==(${authorityIds})`,
}
}).then(({ authorities }) => {
const localAuthorities = authorities.filter(authority => !authority.source.startsWith(CONSORTIUM_PREFIX));

if (hasLinkedAuthorities) {
this.setState({
linkedAuthoritiesLength,
isShareLocalInstanceModalOpen: false,
isUnlinkAuthoritiesModalOpen: true,
});
} else {
this.handleShareLocalInstance(instance);
}
if (localAuthorities.length) {
this.setState({
linkedAuthoritiesLength: localAuthorities.length,
isShareLocalInstanceModalOpen: false,
isUnlinkAuthoritiesModalOpen: true,
});
} else {
this.handleShareLocalInstance(instance);
}
});
}

toggleCopyrightModal = () => {
Expand Down Expand Up @@ -1096,6 +1093,9 @@ ViewInstance.propTypes = {
POST: PropTypes.func.isRequired,
GET: PropTypes.func.isRequired,
}).isRequired,
authorities: PropTypes.shape({
GET: PropTypes.func.isRequired,
}).isRequired,
}),
onClose: PropTypes.func,
onCopy: PropTypes.func,
Expand Down
19 changes: 15 additions & 4 deletions src/ViewInstance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ ConfirmationModal.mockImplementation(({
open,
onCancel,
onConfirm,
heading,
}) => (open ? (
<div>
<span>Confirmation modal</span>
<span>{heading}</span>
<button
type="button"
onClick={onCancel}
Expand Down Expand Up @@ -195,6 +196,9 @@ const defaultProp = {
POST: jest.fn(),
GET: jest.fn(() => Promise.resolve({ sharingInstances: [{ status: 'COMPLETE' }] })),
},
authorities: {
GET: jest.fn(),
}
},
onClose: mockonClose,
onCopy: jest.fn(),
Expand Down Expand Up @@ -294,6 +298,7 @@ describe('ViewInstance', () => {

it('should show a correct Warning message banner when instance sharing is in progress', async () => {
defaultProp.mutator.shareInstance.POST.mockResolvedValue({});
defaultProp.mutator.authorities.GET.mockResolvedValueOnce({ authorities: [] });
checkIfUserInMemberTenant.mockClear().mockReturnValue(true);

renderViewInstance({ isShared: false });
Expand Down Expand Up @@ -627,7 +632,7 @@ describe('ViewInstance', () => {
fireEvent.click(screen.getByRole('button', { name: 'Actions' }));
fireEvent.click(screen.getByRole('button', { name: 'Share local instance' }));

expect(screen.getByText('Confirmation modal')).toBeInTheDocument();
expect(screen.getByText('Are you sure you want to share this instance?')).toBeInTheDocument();
});
});

Expand All @@ -654,14 +659,20 @@ describe('ViewInstance', () => {
fireEvent.click(screen.getByRole('button', { name: 'Share local instance' }));
fireEvent.click(screen.getByRole('button', { name: 'Confirm' }));

expect(screen.getByText('Confirmation modal')).toBeInTheDocument();
expect(screen.getByText('Are you sure you want to share this instance?')).toBeInTheDocument();
});

describe('when proceed sharing', () => {
it('should make POST request to share local instance', () => {
it('should make POST request to share local instance', async () => {
defaultProp.mutator.authorities.GET.mockResolvedValueOnce({ authorities: [{ source: 'MARC' }] });

fireEvent.click(screen.getByRole('button', { name: 'Actions' }));
fireEvent.click(screen.getByRole('button', { name: 'Share local instance' }));
fireEvent.click(screen.getByRole('button', { name: 'Confirm' }));

const localAuthoritiesModal = await screen.findByText('Linked to local authorities');
expect(localAuthoritiesModal).toBeInTheDocument();

fireEvent.click(screen.getByRole('button', { name: 'Confirm' }));

expect(defaultProp.mutator.shareInstance.POST).toHaveBeenCalled();
Expand Down
14 changes: 14 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
omit,
chunk,
flatten,
pick,
} from 'lodash';
import moment from 'moment';

Expand All @@ -41,6 +42,7 @@ import {
OKAPI_TENANT_HEADER,
CONTENT_TYPE_HEADER,
OKAPI_TOKEN_HEADER,
AUTHORITY_LINKED_FIELDS,
} from './constants';

export const areAllFieldsEmpty = fields => fields.every(item => (isArray(item)
Expand Down Expand Up @@ -850,3 +852,15 @@ export const switchAffiliation = async (stripes, tenantId, move) => {
move();
}
};

export const getLinkedAuthorityIds = instance => {
// Pick fields with authorityId
const linkedAuthorities = pick(instance, AUTHORITY_LINKED_FIELDS);

// Retrieve only authorityId
const authorityIdList = AUTHORITY_LINKED_FIELDS.map(field => {
return linkedAuthorities[field].filter(auth => !!auth.authorityId).map(auth => auth.authorityId);
});

return flatten(authorityIdList);
};

0 comments on commit 9e781df

Please sign in to comment.