Skip to content

Commit

Permalink
UIIN-2452: Switch user affiliation using validateUser
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrHladchenko1 committed Oct 17, 2023
1 parent 2398063 commit a603d03
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 104 deletions.
19 changes: 11 additions & 8 deletions src/Holding/CreateHolding/CreateHolding.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
stripesConnect,
stripesShape,
} from '@folio/stripes/core';
import {
LoadingView,
} from '@folio/stripes/components';
import { LoadingView } from '@folio/stripes/components';

import {
useInstance,
Expand All @@ -22,24 +20,29 @@ import HoldingsForm from '../../edit/holdings/HoldingsForm';
import { switchAffiliation } from '../../utils';

const CreateHolding = ({
history,
location,
instanceId,
referenceData,
stripes,
mutator,
tenantFrom,
}) => {
const callout = useCallout();
const { instance, isLoading: isInstanceLoading } = useInstance(instanceId);
const sourceId = referenceData.holdingsSourcesByName?.FOLIO?.id;

const goBack = useCallback(() => {
window.location.href = `/inventory/view/${instanceId}${location.search}`;
history.push({
pathname: `/inventory/view/${instanceId}`,
search: location.search,
});
}, [location.search, instanceId]);

const onCancel = useCallback(() => {
switchAffiliation(stripes.okapi, tenantFrom, goBack).then();
}, [stripes.okapi, tenantFrom, goBack]);
const { location: { state: { tenantFrom } } } = history;

switchAffiliation(stripes, tenantFrom, goBack).then();
}, [stripes, goBack]);

const onSubmit = useCallback((newHolding) => {
return mutator.holding.POST(newHolding)
Expand Down Expand Up @@ -94,11 +97,11 @@ CreateHolding.manifest = Object.freeze({

CreateHolding.propTypes = {
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
instanceId: PropTypes.string.isRequired,
mutator: PropTypes.object.isRequired,
referenceData: PropTypes.object.isRequired,
stripes: stripesShape.isRequired,
tenantFrom: PropTypes.string,
};

export default withRouter(stripesConnect(CreateHolding));
4 changes: 2 additions & 2 deletions src/Instance/HoldingsList/Holding/HoldingButtonsGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const HoldingButtonsGroup = ({
<Button
id={`clickable-view-holdings-${holding.id}`}
data-test-view-holdings
onClick={() => switchAffiliation(stripes.okapi, tenantId, onViewHolding)}
onClick={() => switchAffiliation(stripes, tenantId, onViewHolding)}
disabled={isViewHoldingsDisabled}
>
<FormattedMessage id="ui-inventory.viewHoldings" />
Expand All @@ -58,7 +58,7 @@ const HoldingButtonsGroup = ({
<Button
id={`clickable-new-item-${holding.id}`}
data-test-add-item
onClick={() => switchAffiliation(stripes.okapi, tenantId, onAddItem)}
onClick={() => switchAffiliation(stripes, tenantId, onAddItem)}
buttonStyle="primary paneHeaderNewButton"
disabled={isAddItemDisabled}
>
Expand Down
6 changes: 3 additions & 3 deletions src/Instance/HoldingsList/Holding/HoldingButtonsGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import HoldingButtonsGroup from './HoldingButtonsGroup';

jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'),
updateAffiliation: jest.fn(),
switchAffiliation: jest.fn(),
}));

const mockItemCount = 3;
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('HoldingButtonsGroup', () => {
});

describe('when user click on View holdings button', () => {
it('should call function to update user\'s affiliation', () => {
it('should call function to switch user\'s affiliation', () => {
const { getByRole } = renderHoldingButtonsGroup();

fireEvent.click(getByRole('button', { name: 'View holdings' }));
Expand All @@ -90,7 +90,7 @@ describe('HoldingButtonsGroup', () => {
});

describe('when user click on Add item button', () => {
it('should call function to update user\'s affiliation', () => {
it('should call function to switch user\'s affiliation', () => {
const { getByRole } = renderHoldingButtonsGroup();

fireEvent.click(getByRole('button', { name: 'Add item' }));
Expand Down
23 changes: 21 additions & 2 deletions src/Instance/HoldingsList/Holding/HoldingContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { withRouter } from 'react-router';
import { Draggable } from 'react-beautiful-dnd';
import { FormattedMessage } from 'react-intl';

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

import Holding from './Holding';

const dragStyles = {
Expand Down Expand Up @@ -100,6 +102,7 @@ DraggableHolding.propTypes = {

const HoldingContainer = ({
location,
history,
isViewHoldingsDisabled,
isAddItemDisabled,
isBarcodeAsHotlink,
Expand All @@ -111,12 +114,28 @@ const HoldingContainer = ({
tenantId,
...rest
}) => {
const stripes = useStripes();

const onViewHolding = useCallback(() => {
window.location.href = `/inventory/view/${instance.id}/${holding.id}${location.search}`;
history.push({
pathname: `/inventory/view/${instance.id}/${holding.id}`,
search: location.search,
state: {
tenantTo: tenantId,
tenantFrom: stripes.okapi.tenant,
},
});
}, [location.search, instance.id, holding.id]);

const onAddItem = useCallback(() => {
window.location.href = `/inventory/create/${instance.id}/${holding.id}/item${location.search}`;
history.push({
pathname: `/inventory/create/${instance.id}/${holding.id}/item`,
search: location.search,
state: {
tenantTo: tenantId,
tenantFrom: stripes.okapi.tenant,
},
});
}, [location.search, instance.id, holding.id]);

return isDraggable ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useCallback } from 'react';
import { useIntl } from 'react-intl';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { useHistory } from 'react-router-dom';

import {
IfPermission,
Expand All @@ -23,11 +24,19 @@ const InstanceNewHolding = ({
}) => {
const intl = useIntl();
const stripes = useStripes();
const history = useHistory();

const label = intl.formatMessage({ id: 'ui-inventory.addHoldings' });

const onNewHolding = useCallback(() => {
window.location.href = `/inventory/create/${instance?.id}/holding${location.search}`;
history.push({
pathname: `/inventory/create/${instance?.id}/holding`,
search: location.search,
state: {
tenantTo: tenantId,
tenantFrom: stripes.okapi.tenant,
}
});
}, [location.search, instance.id]);

return (
Expand All @@ -39,7 +48,7 @@ const InstanceNewHolding = ({
aria-label={label}
buttonStyle="primary"
fullWidth
onClick={() => switchAffiliation(stripes.okapi, tenantId, onNewHolding)}
onClick={() => switchAffiliation(stripes, tenantId, onNewHolding)}
disabled={disabled}
>
{label}
Expand Down
13 changes: 11 additions & 2 deletions src/Instance/ItemsList/ItemBarcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
} from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { useHistory } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import queryString from 'query-string';
Expand All @@ -30,13 +31,21 @@ const ItemBarcode = ({
isBarcodeAsHotlink,
tenantId,
}) => {
const history = useHistory();
const stripes = useStripes();
const { search } = location;
const queryBarcode = queryString.parse(search)?.query;
const isQueryByBarcode = queryString.parse(search)?.qindex === QUERY_INDEXES.BARCODE;

const onViewItem = useCallback(() => {
window.location.href = `/inventory/view/${instanceId}/${holdingId}/${item.id}${search}`;
history.push({
pathname: `/inventory/view/${instanceId}/${holdingId}/${item.id}`,
search,
state: {
tenantTo: tenantId,
tenantFrom: stripes.okapi.tenant,
},
});
}, [instanceId, holdingId, item.id, search]);

const callout = useContext(CalloutContext);
Expand Down Expand Up @@ -67,7 +76,7 @@ const ItemBarcode = ({
<Button
buttonStyle="link"
buttonClass={css.linkWithoutBorder}
onClick={() => switchAffiliation(stripes.okapi, tenantId, onViewItem)}
onClick={() => switchAffiliation(stripes, tenantId, onViewItem)}
>
{itemBarcode}
</Button>
Expand Down
32 changes: 19 additions & 13 deletions src/Item/CreateItem/CreateItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import React, {
useMemo,
} from 'react';
import PropTypes from 'prop-types';
import { useLocation } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';

import { useStripes } from '@folio/stripes/core';
import {
LoadingView,
} from '@folio/stripes/components';
import { LoadingView } from '@folio/stripes/components';

import {
useInstanceQuery,
Expand All @@ -24,10 +22,17 @@ const CreateItem = ({
referenceData,
instanceId,
holdingId,
tenantTo,
tenantFrom,
}) => {
const location = useLocation();
const {
push,
location: {
search,
state: {
tenantTo,
tenantFrom,
},
},
} = useHistory();

const { isLoading: isInstanceLoading, instance } = useInstanceQuery(instanceId, { tenantId: tenantTo });
const { isLoading: isHoldingLoading, holding } = useHolding(holdingId, { tenantId: tenantTo });
Expand All @@ -40,12 +45,15 @@ const CreateItem = ({
}), [holding.id]);

const goBack = useCallback(() => {
window.location.href = `/inventory/view/${instanceId}${location.search}`;
}, [instanceId, location.search]);
push({
pathname: `/inventory/view/${instanceId}`,
search,
});
}, [instanceId, search]);

const onCancel = useCallback(() => {
switchAffiliation(stripes.okapi, tenantFrom, goBack).then();
}, [stripes.okapi, tenantFrom, goBack]);
switchAffiliation(stripes, tenantFrom, goBack).then();
}, [stripes, tenantFrom]);

const onSuccess = useCallback(async (response) => {
const { hrid } = await response.json();
Expand Down Expand Up @@ -91,8 +99,6 @@ CreateItem.propTypes = {
instanceId: PropTypes.string.isRequired,
holdingId: PropTypes.string.isRequired,
referenceData: PropTypes.object.isRequired,
tenantTo: PropTypes.string,
tenantFrom: PropTypes.string,
};

export default CreateItem;
17 changes: 14 additions & 3 deletions src/Item/CreateItem/CreateItem.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '../../../test/jest/__mock__';

import { MemoryRouter } from 'react-router-dom';
import { Router } from 'react-router-dom';
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
import { QueryClient, QueryClientProvider } from 'react-query';
import { createMemoryHistory } from 'history';

import { instance } from '../../../test/fixtures/instance';
import {
Expand All @@ -19,6 +20,16 @@ jest.mock('../../common/hooks', () => ({
useHolding: jest.fn().mockReturnValue({ holding: {}, isLoading: false }),
}));

const history = createMemoryHistory();
history.location = {
pathname: '/testPathName',
search: '?filters=test1',
state: {
tenantTo: 'testTenantToId',
tenantFrom: 'testTenantFromId',
}
};

const defaultProps = {
instanceId: instance.id,
holdingId: 'holdingId',
Expand All @@ -28,11 +39,11 @@ const defaultProps = {
const queryClient = new QueryClient();

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

const renderCreateItem = (props = {}) => render(
Expand Down
Loading

0 comments on commit a603d03

Please sign in to comment.