Skip to content

Commit

Permalink
UIBULKED-403 enable query-related call only on Query tab
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Feb 2, 2024
1 parent 876e6e1 commit 729dcc9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -86,7 +85,6 @@ export const BulkEditListFilters = ({
criteria: CRITERIA.LOGS,
fileName: initialFileName,
step: initialStep,
recordTypes: initialRecordType,
};

const [
Expand All @@ -109,15 +107,17 @@ 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,
queryDetailsDataSource,
testQueryDataSource,
getParamsSource,
cancelQueryDataSource,
} = useQueryPlugin(initialRecordType);
} = useQueryPlugin(recordTypeId);

const handleChange = (value, field) => setFilters(prev => ({
...prev, [field]: value,
Expand All @@ -132,16 +132,13 @@ export const BulkEditListFilters = ({
recordIdentifier: '',
}));

const selected = recordTypes?.find(type => type.label === getRecordType(value))?.id;

history.replace({
pathname: '/bulk-edit',
search: buildSearch({
capabilities: value,
identifier: null,
step: null,
fileName: null,
recordTypes: selected
}, location.search),
});

Expand All @@ -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),
});
};

Expand Down Expand Up @@ -332,7 +327,7 @@ export const BulkEditListFilters = ({
componentType="builder"
type="query-builder"
disabled={isQueryBuilderDisabled}
key={capabilities}
key={recordTypeId}
entityTypeDataSource={entityTypeDataSource}
testQueryDataSource={testQueryDataSource}
getParamsSource={getParamsSource}
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/api/useQueryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/api/useRecordTypes.js
Original file line number Diff line number Diff line change
@@ -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 ({
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/api/useRecordTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 729dcc9

Please sign in to comment.