From 729dcc92b637fa1ce16f6d4f82a78754ba3e2891 Mon Sep 17 00:00:00 2001 From: vashjs Date: Fri, 2 Feb 2024 10:43:02 +0100 Subject: [PATCH] UIBULKED-403 enable query-related call only on Query tab --- .../BulkEditListFilters.js | 19 +++++++------------ src/hooks/api/useQueryPlugin.js | 6 +----- src/hooks/api/useRecordTypes.js | 7 ++++--- src/hooks/api/useRecordTypes.test.js | 2 +- 4 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/components/BulkEditList/BulkEditListFilters/BulkEditListFilters.js b/src/components/BulkEditList/BulkEditListFilters/BulkEditListFilters.js index 2277c66c..737a6a3e 100644 --- a/src/components/BulkEditList/BulkEditListFilters/BulkEditListFilters.js +++ b/src/components/BulkEditList/BulkEditListFilters/BulkEditListFilters.js @@ -67,7 +67,6 @@ export const BulkEditListFilters = ({ const initialCapabilities = search.get('capabilities'); const initialFileName = search.get('fileName'); const initialStep = search.get('step'); - const initialRecordType = search.get('recordTypes'); const logFilters = Object.values(FILTERS).map((el) => search.getAll(el)); const isQuery = criteria === CRITERIA.QUERY; @@ -86,7 +85,6 @@ export const BulkEditListFilters = ({ criteria: CRITERIA.LOGS, fileName: initialFileName, step: initialStep, - recordTypes: initialRecordType, }; const [ @@ -109,7 +107,9 @@ export const BulkEditListFilters = ({ const { fileUpload, isLoading } = useUpload(); const { bulkOperationStart } = useBulkOperationStart(); - const { recordTypes } = useRecordTypes(); + const { recordTypes } = useRecordTypes({ enabled: isQuery }); + + const recordTypeId = recordTypes?.find(type => type.label === getRecordType(initialCapabilities))?.id; const { entityTypeDataSource, @@ -117,7 +117,7 @@ export const BulkEditListFilters = ({ testQueryDataSource, getParamsSource, cancelQueryDataSource, - } = useQueryPlugin(initialRecordType); + } = useQueryPlugin(recordTypeId); const handleChange = (value, field) => setFilters(prev => ({ ...prev, [field]: value, @@ -132,8 +132,6 @@ export const BulkEditListFilters = ({ recordIdentifier: '', })); - const selected = recordTypes?.find(type => type.label === getRecordType(value))?.id; - history.replace({ pathname: '/bulk-edit', search: buildSearch({ @@ -141,7 +139,6 @@ export const BulkEditListFilters = ({ identifier: null, step: null, fileName: null, - recordTypes: selected }, location.search), }); @@ -151,12 +148,10 @@ export const BulkEditListFilters = ({ }; const handleCriteriaChange = (value) => { - const newFilterValue = { capabilities: '', recordTypes: '', criteria: value }; - - setFilters(prev => ({ ...prev, ...newFilterValue })); + setFilters(prev => ({ ...prev, recordIdentifier: '', criteria: value })); history.replace({ - search: buildSearch(newFilterValue, location.search), + search: buildSearch({ identifier: '', criteria: value }, location.search), }); }; @@ -332,7 +327,7 @@ export const BulkEditListFilters = ({ componentType="builder" type="query-builder" disabled={isQueryBuilderDisabled} - key={capabilities} + key={recordTypeId} entityTypeDataSource={entityTypeDataSource} testQueryDataSource={testQueryDataSource} getParamsSource={getParamsSource} diff --git a/src/hooks/api/useQueryPlugin.js b/src/hooks/api/useQueryPlugin.js index 6e63911b..28bf928a 100644 --- a/src/hooks/api/useQueryPlugin.js +++ b/src/hooks/api/useQueryPlugin.js @@ -4,11 +4,7 @@ export const useQueryPlugin = (recordType) => { const ky = useOkapiKy(); const entityTypeDataSource = async () => { - if (recordType) { - const response = ky.get(`entity-types/${recordType}`); - return response.json(); - } - return null; + return ky.get(`entity-types/${recordType}`).json(); }; const queryDetailsDataSource = async ({ queryId, includeContent, offset, limit }) => { diff --git a/src/hooks/api/useRecordTypes.js b/src/hooks/api/useRecordTypes.js index 19a30cce..192d30d9 100644 --- a/src/hooks/api/useRecordTypes.js +++ b/src/hooks/api/useRecordTypes.js @@ -1,18 +1,19 @@ import { useOkapiKy } from '@folio/stripes/core'; import { useQuery } from 'react-query'; -const ENTITY_TYPE_HASH = 'entityType'; +const ENTITY_TYPE_KEY = 'entityType'; -export const useRecordTypes = () => { +export const useRecordTypes = ({ enabled } = {}) => { const ky = useOkapiKy(); const { data, isLoading, error } = useQuery({ - queryKey: [ENTITY_TYPE_HASH], + queryKey: [ENTITY_TYPE_KEY], queryFn: async () => { const response = await ky.get('entity-types'); return response.json(); }, refetchOnWindowFocus: false, + enabled }); return ({ diff --git a/src/hooks/api/useRecordTypes.test.js b/src/hooks/api/useRecordTypes.test.js index 15cef392..b27aa3d2 100644 --- a/src/hooks/api/useRecordTypes.test.js +++ b/src/hooks/api/useRecordTypes.test.js @@ -40,7 +40,7 @@ describe('useRecordTypes', () => { let result; await act(async () => { - result = renderHook(() => useRecordTypes()).result; + result = renderHook(() => useRecordTypes({ enabled: true })).result; }); expect(result.current.recordTypes).toEqual([