Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIOR-1159 Use a provided tenantId when loading an instance in a PO line form #1511

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* When order line contains bad product IDs - the save & close does not work. Refs UIOR-1141.
* Upgrade `ui-plugin-find-po-line` and `ui-plugin-find-organization` plugins to `5` version. Refs UIOR-1154.
* POL - Display only active account numbers in dropdown list. Refs UIOR-1142.
* Use a provided `tenantId` when loading an instance in a PO line form. Refs UIOR-1159.

## [4.0.2](https://github.com/folio-org/ui-orders/tree/v4.0.2) (2023-03-17)
[Full Changelog](https://github.com/folio-org/ui-orders/compare/v4.0.1...v4.0.2)
Expand Down
1 change: 1 addition & 0 deletions src/common/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ export const UNOPEN_ORDER_ABANDONED_HOLDINGS_TYPES = {
};

export const FIELD_ARRAY_ITEM_IDENTIFIER_KEY = '__key__';
export const OKAPI_TENANT_HEADER = 'X-Okapi-Tenant';
1 change: 1 addition & 0 deletions src/common/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export * from './useOrderLinesAbandonedHoldingsCheck';
export * from './useOrderTemplate';
export * from './usePOLineRelatedItems';
export * from './useReexport';
export * from './useTenantKy';
export * from './useTitleMutation';
export * from './useVendor';
20 changes: 15 additions & 5 deletions src/common/hooks/useInstance/useInstance.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { useQuery } from 'react-query';

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

export const useInstance = (instanceId) => {
const ky = useOkapiKy();
import { useTenantKy } from '../useTenantKy';

const DEFAULT_DATA = {};
const DEFAULT_OPTIONS = {};

export const useInstance = (instanceId, options = DEFAULT_OPTIONS) => {
const { tenantId } = options;

const ky = useTenantKy({ tenantId });
const [namespace] = useNamespace({ key: 'instance' });

const { isLoading, data: instance = {} } = useQuery(
[namespace, instanceId],
const {
isLoading,
data: instance = DEFAULT_DATA,
} = useQuery(
[namespace, instanceId, tenantId],
() => ky.get(`inventory/instances/${instanceId}`).json(),
{ enabled: Boolean(instanceId) },
);
Expand Down
1 change: 1 addition & 0 deletions src/common/hooks/useTenantKy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useTenantKy } from './useTenantKy';
19 changes: 19 additions & 0 deletions src/common/hooks/useTenantKy/useTenantKy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useOkapiKy } from '@folio/stripes/core';

import { OKAPI_TENANT_HEADER } from '../../constants';

export const useTenantKy = ({ tenantId } = {}) => {
const ky = useOkapiKy();

return tenantId
? ky.extend({
hooks: {
beforeRequest: [
request => {
request.headers.set(OKAPI_TENANT_HEADER, tenantId);
},
],
},
})
: ky;
};
41 changes: 41 additions & 0 deletions src/common/hooks/useTenantKy/useTenantKy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';
import { useOkapiKy } from '@folio/stripes/core';

import { OKAPI_TENANT_HEADER } from '../../constants';
import { useTenantKy } from './useTenantKy';

const reqMock = {
headers: {
set: jest.fn(),
},
};
const kyMock = {
extend: jest.fn(({ hooks: { beforeRequest } }) => {
beforeRequest[0](reqMock);

return kyMock;
}),
};

describe('useTenantKy', () => {
beforeEach(() => {
reqMock.headers.set.mockClear();
kyMock.extend.mockClear();
useOkapiKy.mockClear().mockReturnValue(kyMock);
});

it('should set provided okapi tenant header and return \'ky\' client', async () => {
const tenantId = 'college';
const { result } = renderHook(() => useTenantKy({ tenantId }));

expect(result.current).toBe(kyMock);
expect(reqMock.headers.set).toHaveBeenCalledWith(OKAPI_TENANT_HEADER, tenantId);
});

it('should use current tenant in the headers if there is no provided tenant ID', async () => {
const { result } = renderHook(() => useTenantKy());

expect(result.current).toBe(kyMock);
expect(reqMock.headers.set).not.toHaveBeenCalled();
});
});
7 changes: 6 additions & 1 deletion src/components/LayerCollection/LayerPOLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@
const { isLoading: isLinesLimitLoading, linesLimit } = useLinesLimit(!(lineId || poLine));
const [isCreateAnotherChecked, setCreateAnotherChecked] = useState(locationState?.isCreateAnotherChecked);
const { isFetching: isConfigsFetching, integrationConfigs } = useIntegrationConfigs({ organizationId: vendor?.id });
const { instance, isLoading: isInstanceLoading } = useInstance(locationState?.instanceId);
const { instance, isLoading: isInstanceLoading } = useInstance(
locationState?.instanceId,
{
tenantId: locationState?.instanceTenantId,
},
);
const { mutateTitle } = useTitleMutation();

// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -190,7 +195,7 @@
});
}
},
[openLineLimitExceededModal, sendCallout, toggleDeletePieces],

Check warning on line 198 in src/components/LayerCollection/LayerPOLine.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has a missing dependency: 'intl'. Either include it or remove the dependency array

Check warning on line 198 in src/components/LayerCollection/LayerPOLine.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has a missing dependency: 'intl'. Either include it or remove the dependency array
);

const openOrder = useCallback(
Expand Down
Loading