2021-08-26 22:33:06 +03:00
|
|
|
import { Action, AnyAction } from 'redux';
|
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
import {
|
|
|
|
QueryInfo,
|
|
|
|
RtkQueryMonitorState,
|
|
|
|
QueryFormValues,
|
|
|
|
RtkQueryMonitorProps,
|
|
|
|
QueryPreviewTabs,
|
|
|
|
} from './types';
|
|
|
|
import { QueryComparators } from './utils/comparators';
|
|
|
|
import { QueryFilters } from './utils/filters';
|
|
|
|
|
|
|
|
const initialState: RtkQueryMonitorState = {
|
|
|
|
queryForm: {
|
|
|
|
values: {
|
|
|
|
queryComparator: QueryComparators.fulfilledTimeStamp,
|
|
|
|
isAscendingQueryComparatorOrder: false,
|
|
|
|
searchValue: '',
|
|
|
|
isRegexSearch: false,
|
|
|
|
queryFilter: QueryFilters.queryKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
selectedPreviewTab: QueryPreviewTabs.queryinfo,
|
|
|
|
selectedQueryKey: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const monitorSlice = createSlice({
|
|
|
|
/**
|
|
|
|
* `@@` prefix is mandatory.
|
|
|
|
* @see lifedAction @ `packages/redux-devtools-app/src/actions/index.ts`
|
|
|
|
*/
|
|
|
|
name: '@@rtk-query-monitor',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
changeQueryFormValues(
|
|
|
|
state,
|
2023-07-12 21:03:20 +03:00
|
|
|
action: PayloadAction<Partial<QueryFormValues>>,
|
2021-08-26 22:33:06 +03:00
|
|
|
) {
|
|
|
|
state.queryForm.values = { ...state.queryForm.values, ...action.payload };
|
|
|
|
},
|
|
|
|
selectQueryKey(
|
|
|
|
state,
|
2023-07-12 21:03:20 +03:00
|
|
|
action: PayloadAction<Pick<QueryInfo, 'reducerPath' | 'queryKey'>>,
|
2021-08-26 22:33:06 +03:00
|
|
|
) {
|
|
|
|
state.selectedQueryKey = {
|
|
|
|
queryKey: action.payload.queryKey,
|
|
|
|
reducerPath: action.payload.reducerPath,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
selectedPreviewTab(state, action: PayloadAction<QueryPreviewTabs>) {
|
|
|
|
state.selectedPreviewTab = action.payload;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
export function reducer<S, A extends Action<string>>(
|
2021-08-26 22:33:06 +03:00
|
|
|
props: RtkQueryMonitorProps<S, A>,
|
|
|
|
state: RtkQueryMonitorState | undefined,
|
2023-07-12 21:03:20 +03:00
|
|
|
action: AnyAction,
|
2021-08-26 22:33:06 +03:00
|
|
|
): RtkQueryMonitorState {
|
|
|
|
return monitorSlice.reducer(state, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const { selectQueryKey, changeQueryFormValues, selectedPreviewTab } =
|
|
|
|
monitorSlice.actions;
|