Skip to content

Commit

Permalink
UIIN-3194: Detail view of created Instance record is not loaded after…
Browse files Browse the repository at this point in the history
… saving
  • Loading branch information
mariia-aloshyna committed Jan 24, 2025
1 parent ebc566d commit cfcffdc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* ECS: Disable opening item details if a user is not affiliated with item's member tenant. Fixes UIIN-3187.
* Display failure message during `Update Ownership` action when Item contains Local reference data. Fixes UIIN-3195.
* Correctly depend on `inflected`. Refs UIIN-3203.
* Detail view of created Instance record is not loaded after saving. Fixes UIIN-3194.

## [12.0.10](https://github.com/folio-org/ui-inventory/tree/v12.0.10) (2025-01-20)
[Full Changelog](https://github.com/folio-org/ui-inventory/compare/v12.0.9...v12.0.10)
Expand Down
24 changes: 19 additions & 5 deletions src/common/hooks/useInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ import { useStripes } from '@folio/stripes/core';
import useSearchInstanceByIdQuery from './useSearchInstanceByIdQuery';
import useInstanceQuery from './useInstanceQuery';

import { isUserInConsortiumMode } from '../../utils';

const useInstance = (id) => {
const stripes = useStripes();
const centralTenantId = stripes.user.user?.consortium?.centralTenantId;
const isUserInConsortium = isUserInConsortiumMode(stripes);

let isShared = false;
let instanceTenantId = stripes?.okapi.tenant;

// search instance by id (only in consortium mode) to get information about tenant and shared status
const {
refetch: refetchSearch,
isLoading: isSearchInstanceByIdLoading,
instance: _instance,
} = useSearchInstanceByIdQuery(id);
} = useSearchInstanceByIdQuery(id, { enabled: Boolean(isUserInConsortium) });

if (isUserInConsortium) {
const centralTenantId = stripes.user.user?.consortium?.centralTenantId;

const isShared = _instance?.shared;
const instanceTenantId = isShared ? centralTenantId : _instance?.tenantId;
isShared = _instance?.shared;
instanceTenantId = isShared ? centralTenantId : _instance?.tenantId;
}

const {
refetch: refetchInstance,
Expand Down Expand Up @@ -47,7 +57,11 @@ const useInstance = (id) => {
[isSearchInstanceByIdLoading, isInstanceLoading],
);
const refetch = useCallback(() => {
refetchSearch().then(() => refetchInstance());
if (isUserInConsortium) {
refetchSearch().then(refetchInstance);
} else {
refetchInstance();
}
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import { useQuery } from 'react-query';

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

const useSearchInstanceByIdQuery = (instanceId) => {
const NO_RECORDS_FOUND_ERROR = 'No records found';

const useSearchInstanceByIdQuery = (instanceId, { enabled = true } = {}) => {
const ky = useOkapiKy();
const [namespace] = useNamespace({ key: 'search-instance' });

const queryFn = async () => {
const response = await ky.get(`search/instances?query=id==${instanceId}`).json();

if (response.totalRecords === 0) {
throw new Error(NO_RECORDS_FOUND_ERROR); // this triggers the retry mechanism
}
return response;
};

const { refetch, isLoading, data = {} } = useQuery(
{
queryKey: [namespace, instanceId],
queryFn: () => ky.get(`search/instances?query=id==${instanceId}`).json(),
enabled: Boolean(instanceId),
queryFn,
enabled: Boolean(enabled && instanceId),
retry: (failureCount, error) => {
if (failureCount >= 3) return false; // stop after 3 attempts
if (error.message === NO_RECORDS_FOUND_ERROR) return true; // retry if no records
return false; // stop retrying on other errors
},
retryDelay: () => 3000,
},
);

Expand Down

0 comments on commit cfcffdc

Please sign in to comment.