Skip to content

Commit

Permalink
UIMARCAUTH-284: Number of links must not be taken from another tenant. (
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Melnyshyn authored Sep 27, 2023
1 parent 67a737b commit 8a346b9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useAuthorityExport';
export * from './useExportReport';
export * from './useAuthoritiesDelete';
export * from './useAuthorityLinksCount';
1 change: 1 addition & 0 deletions src/queries/useAuthorityLinksCount/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useAuthorityLinksCount } from './useAuthorityLinksCount';
19 changes: 19 additions & 0 deletions src/queries/useAuthorityLinksCount/useAuthorityLinksCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useMutation } from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';

const useAuthorityLinksCount = () => {
const ky = useOkapiKy();

const { data, mutateAsync, isLoading } = useMutation({
mutationFn: ids => ky.post('links/authorities/bulk/count', { json: { ids } }).json(),
});

return {
fetchLinksCount: mutateAsync,
linksCount: data?.links?.[0]?.totalLinks,
isLoading,
};
};

export default useAuthorityLinksCount;
44 changes: 44 additions & 0 deletions src/queries/useAuthorityLinksCount/useAuthorityLinksCount.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
QueryClient,
QueryClientProvider,
} from 'react-query';
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';

import '../../../test/jest/__mock__';

import { useOkapiKy } from '@folio/stripes/core';

import useAuthorityLinksCount from './useAuthorityLinksCount';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
}));

const queryClient = new QueryClient();

const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

const mockPost = jest.fn().mockReturnValue({
json: jest.fn().mockResolvedValue({ links: [] }),
});

describe('Given useAuthorityLinksCount', () => {
beforeEach(() => {
useOkapiKy.mockClear().mockReturnValue({
post: mockPost,
});
});

it('should fetch links count', async () => {
const { result } = renderHook(() => useAuthorityLinksCount(), { wrapper });

await result.current.fetchLinksCount(['fakeId']);

expect(mockPost).toHaveBeenCalledWith('links/authorities/bulk/count', { json: { ids: ['fakeId'] } });
});
});
21 changes: 15 additions & 6 deletions src/views/AuthorityView/AuthorityView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {
useContext,
useCallback,
useState,
useEffect,
} from 'react';
import PropTypes from 'prop-types';
import {
useHistory,
useLocation,
useRouteMatch,
} from 'react-router';
import {
useIntl,
Expand Down Expand Up @@ -42,12 +44,14 @@ import {
import PrintPopup from '@folio/quick-marc/src/QuickMarcView/PrintPopup';
import { KeyShortCutsWrapper } from '../../components';

import useAuthorityDelete from '../../queries/useAuthoritiesDelete/useAuthorityDelete';
import {
useAuthorityDelete,
useAuthorityLinksCount,
} from '../../queries';
import { isConsortiaEnv } from '../../utils';

const propTypes = {
authority: PropTypes.shape({
allData: PropTypes.arrayOf(PropTypes.object).isRequired,
data: PropTypes.shape({
authRefType: PropTypes.string,
headingRef: PropTypes.string,
Expand All @@ -71,6 +75,7 @@ const AuthorityView = ({
}) => {
const [deleteModalOpen, setDeleteModalOpen] = useState(false);
const intl = useIntl();
const { params: { id } } = useRouteMatch();
const history = useHistory();
const location = useLocation();
const stripes = useStripes();
Expand All @@ -92,17 +97,21 @@ const AuthorityView = ({
enabled: Boolean(isShared && checkIfUserInMemberTenant(stripes)),
});

const { linksCount, fetchLinksCount, isLoading: isLinksCountLoading } = useAuthorityLinksCount();
const { authorityMappingRules } = useAuthorityMappingRules({ tenantId, enabled: Boolean(authority.data) });

const [, setSelectedAuthorityRecordContext] = useContext(SelectedAuthorityRecordContext);

const callout = useContext(CalloutContext);
const linkedRecord = authority?.allData.find(authorityRecord => authorityRecord.numberOfTitles);

const [isShownPrintPopup, setIsShownPrintPopup] = useState(false);
const openPrintPopup = () => setIsShownPrintPopup(true);
const closePrintPopup = () => setIsShownPrintPopup(false);

useEffect(() => {
fetchLinksCount([id]);
}, [fetchLinksCount, id]);

const hasCentralTenantPerm = perm => {
return centralTenantPermissions.some(({ permissionName }) => permissionName === perm);
};
Expand Down Expand Up @@ -157,7 +166,7 @@ const AuthorityView = ({
},
});

if (marcSource.isLoading || authority.isLoading || isCentralTenantPermissionsLoading) {
if (marcSource.isLoading || authority.isLoading || isCentralTenantPermissionsLoading || isLinksCountLoading) {
return <LoadingPane id="marc-view-pane" />;
}

Expand Down Expand Up @@ -297,13 +306,13 @@ const AuthorityView = ({
id: 'ui-marc-authorities.delete.label',
})}
message={
linkedRecord
linksCount
? (
<FormattedMessage
id="ui-marc-authorities.delete.linkedRecord.description"
values={{
headingRef: authority.data.headingRef,
count: linkedRecord.numberOfTitles,
count: linksCount,
}}
/>
)
Expand Down
27 changes: 17 additions & 10 deletions src/views/AuthorityView/AuthorityView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useUserTenantPermissions } from '@folio/stripes-authority-components';
import Harness from '../../../test/jest/helpers/harness';
import AuthorityView from './AuthorityView';
import { openEditShortcut } from '../../../test/utilities';
import { useAuthorityLinksCount } from '../../queries';

const mockHistoryPush = jest.fn();
const mockSetSelectedAuthorityRecordContext = jest.fn();
Expand Down Expand Up @@ -50,6 +51,11 @@ jest.mock('@folio/stripes-authority-components', () => ({
useTenantKy: jest.fn(),
}));

jest.mock('../../queries', () => ({
...jest.requireActual('../../queries'),
useAuthorityLinksCount: jest.fn(),
}));

const marcSource = {
data: {
parsedRecord: {
Expand Down Expand Up @@ -105,7 +111,6 @@ const marcSource = {
};

const authority = {
allData: [],
data: {
id: 'authority-id',
headingRef: 'heading-ref',
Expand All @@ -132,6 +137,11 @@ describe('Given AuthorityView', () => {
userPermissions: [],
isFetching: false,
});
useAuthorityLinksCount.mockReturnValue({
linksCount: undefined,
fetchLinksCount: jest.fn().mockResolvedValue(),
isLoading: false,
});
});

afterEach(() => {
Expand All @@ -146,7 +156,6 @@ describe('Given AuthorityView', () => {
isLoading: true,
},
authority: {
allData: [],
data: {},
isLoading: true,
},
Expand Down Expand Up @@ -302,16 +311,14 @@ describe('Given AuthorityView', () => {

describe('and the record is linked to a bib record', () => {
it('should display the correct message', () => {
const { getByText } = renderAuthorityView({
authority: {
...authority,
allData: [{
...authority.data,
numberOfTitles: 1,
}],
},
useAuthorityLinksCount.mockReturnValue({
linksCount: 1,
fetchLinksCount: jest.fn().mockResolvedValue(),
isLoading: false,
});

const { getByText } = renderAuthorityView();

fireEvent.click(getByText('ui-marc-authorities.authority-record.delete'));

expect(getByText('ui-marc-authorities.delete.linkedRecord.description')).toBeVisible();
Expand Down

0 comments on commit 8a346b9

Please sign in to comment.