From e03427b076a9def227c1e95166f06c459f0c8724 Mon Sep 17 00:00:00 2001 From: hidden4003 Date: Thu, 4 Aug 2022 18:52:00 +0300 Subject: [PATCH] Migrate filters to rtk query --- src/components/Dialogs/FiltersModal.tsx | 18 +++++++++++------- src/core/events.ts | 2 -- src/core/middlewares/rtkQueryError.ts | 2 +- src/core/rtkQuery/collectionApi.ts | 6 +++++- src/core/sagas/collection.ts | 20 -------------------- src/core/sagas/index.ts | 3 --- src/core/types/api.ts | 2 +- src/core/types/api/collection.ts | 13 +++++++++++++ 8 files changed, 31 insertions(+), 35 deletions(-) delete mode 100644 src/core/sagas/collection.ts diff --git a/src/components/Dialogs/FiltersModal.tsx b/src/components/Dialogs/FiltersModal.tsx index 454e31834..8479606a4 100644 --- a/src/components/Dialogs/FiltersModal.tsx +++ b/src/components/Dialogs/FiltersModal.tsx @@ -4,20 +4,24 @@ import ModalPanel from '../Panels/ModalPanel'; import { useDispatch, useSelector } from 'react-redux'; import { RootState } from '../../core/store'; import { setStatus } from '../../core/slices/modals/filters'; -import Events from '../../core/events'; +import { useLazyGetTopFiltersQuery } from '../../core/rtkQuery/collectionApi'; + +import type { CollectionFilterType } from '../../core/types/api/collection'; function FiltersModal() { const dispatch = useDispatch(); const status = useSelector((state: RootState) => state.modals.filters.status); - const filters = useSelector((state: RootState) => state.modals.filters.filters); - - const handleClose = () => dispatch(setStatus(false)); + const [trigger, filtersResult] = useLazyGetTopFiltersQuery({}); + const filters: Array = filtersResult?.data?.List ?? [] as Array; useEffect(() => { - dispatch({ type: Events.FILTERS_GET }); - }, []); + if (!status) { return; } + trigger({}).catch(() => {}); + }, [status]); + + const handleClose = () => dispatch(setStatus(false)); - const renderItem = item => ( + const renderItem = (item: CollectionFilterType) => (
{item.Name} {item.Size} diff --git a/src/core/events.ts b/src/core/events.ts index 2f38d01d0..973b4f37a 100644 --- a/src/core/events.ts +++ b/src/core/events.ts @@ -19,8 +19,6 @@ export default { FIRSTRUN_TEST_DATABASE: 'EVENT_FIRSTRUN_TEST_DATABASE', // DASHBOARD DASHBOARD_UPCOMING_ANIME: 'EVENT_DASHBOARD_UPCOMING_ANIME', - // FILTERS - FILTERS_GET: 'EVENT_FILTERS_GET', // FOLDER FOLDER_BROWSE: 'EVENT_FOLDER_BROWSE', // QUICK ACTIONS diff --git a/src/core/middlewares/rtkQueryError.ts b/src/core/middlewares/rtkQueryError.ts index a0b34cc73..271372455 100644 --- a/src/core/middlewares/rtkQueryError.ts +++ b/src/core/middlewares/rtkQueryError.ts @@ -11,7 +11,7 @@ const rtkQueryErrorLogger: Middleware = if (action.payload.status === 401) { toast.error('Unauthorized!'); } else { - toast.error(action.payload?.data?.title ?? action.payload.error); + toast.error(action.payload?.data?.title ?? action.payload.error ?? `${action.payload.status} - ${action.error.message}`); } } diff --git a/src/core/rtkQuery/collectionApi.ts b/src/core/rtkQuery/collectionApi.ts index 110b11fe9..663f389d5 100644 --- a/src/core/rtkQuery/collectionApi.ts +++ b/src/core/rtkQuery/collectionApi.ts @@ -1,7 +1,7 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; import { RootState } from '../store'; -import { CollectionGroupType } from '../types/api/collection'; +import { CollectionFilterType, CollectionGroupType } from '../types/api/collection'; import { ListResultType, PaginationType } from '../types/api'; import { SeriesType } from '../types/api/series'; @@ -22,10 +22,14 @@ export const collectionApi = createApi({ getGroupSeries: build.query, { groupId?: string }>({ query: ({ groupId }) => ({ url: `Group/${groupId}/Series` }), }), + getTopFilters: build.query>, PaginationType>({ + query: params => ({ url: 'Filter', params: { page: params.page ?? 1, pageSize: params.pageSize ?? 0 } }), + }), }), }); export const { useLazyGetGroupsQuery, useGetGroupSeriesQuery, + useLazyGetTopFiltersQuery, } = collectionApi; \ No newline at end of file diff --git a/src/core/sagas/collection.ts b/src/core/sagas/collection.ts deleted file mode 100644 index 98e725c9b..000000000 --- a/src/core/sagas/collection.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { put, call } from 'redux-saga/effects'; -import { toast } from 'react-toastify'; - -import ApiFilters from '../api/v3/filter'; - -import { setFilters } from '../slices/modals/filters'; - -function* getFilters() { - const resultJson = yield call(ApiFilters.getFilters); - if (resultJson.error) { - toast.error(resultJson.message); - return; - } - - yield put(setFilters(resultJson.data.List)); -} - -export default { - getFilters, -}; \ No newline at end of file diff --git a/src/core/sagas/index.ts b/src/core/sagas/index.ts index 05db3bcf3..3b42e6bda 100644 --- a/src/core/sagas/index.ts +++ b/src/core/sagas/index.ts @@ -9,7 +9,6 @@ import ApiCommon from '../api/common'; import Events from '../events'; import SagaAuth from './auth'; -import SagaCollection from './collection'; import SagaFile from './file'; import SagaFolder from './folder'; import SagaImportFolder from './import-folder'; @@ -58,8 +57,6 @@ export default function* rootSaga() { takeEvery(Events.FIRSTRUN_START_SERVER, SagaInit.startServer), takeEvery(Events.FIRSTRUN_TEST_ANIDB, SagaInit.testAniDB), takeEvery(Events.FIRSTRUN_TEST_DATABASE, SagaInit.testDatabase), - // FILTERS - takeEvery(Events.FILTERS_GET, SagaCollection.getFilters), // FOLDER takeEvery(Events.FOLDER_BROWSE, SagaFolder.folderBrowse), // QUICK ACTIONS diff --git a/src/core/types/api.ts b/src/core/types/api.ts index df532d4df..2aea18481 100644 --- a/src/core/types/api.ts +++ b/src/core/types/api.ts @@ -39,7 +39,7 @@ export type QueueStatusType = { }; export type PaginationType = { - pageSize: number; + pageSize?: number; page?: number; }; diff --git a/src/core/types/api/collection.ts b/src/core/types/api/collection.ts index 1487dd060..4cb880efd 100644 --- a/src/core/types/api/collection.ts +++ b/src/core/types/api/collection.ts @@ -29,4 +29,17 @@ export type GroupSizesSeriesTypesType = { Web: number; Movie: number; OVA: number; +}; + +export type CollectionFilterType = { + IDs: { + ParentFilter: number | null; + ID: number; + }, + Locked: boolean; + ApplyAtSeriesLevel: boolean; + Directory: boolean; + Hidden: boolean; + Name: string; + Size: number; }; \ No newline at end of file