Skip to content

Commit

Permalink
Misc refactors (#711)
Browse files Browse the repository at this point in the history
* Fix alt row colour in manually linked files util

* Refactor `ListResultType` and `InfiniteResultType`

* API cleanup, add rtkPaginationUtil
  • Loading branch information
harshithmohan authored Dec 7, 2023
1 parent 073b6f8 commit 4772e3a
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 216 deletions.
36 changes: 36 additions & 0 deletions src/core/rtkPaginationUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// TODO: Figure how to add types to everything
import { defaultSerializeQueryArgs } from '@reduxjs/toolkit/query';
import { omit } from 'lodash';

import type { InfiniteResultType, ListResultType, PaginationType } from '@/core/types/api';
import type { EndpointDefinition } from '@reduxjs/toolkit/query';

export const transformPaginatedResponse = (response: ListResultType<any>, _: any, args: PaginationType) => ({
pages: {
[args.page ?? 1]: response.List,
},
total: response.Total,
});

export const serializePaginatedQueryArgs = (
{ endpointDefinition, endpointName, queryArgs }: {
endpointDefinition: EndpointDefinition<any, any, any, any>;
endpointName: string;
queryArgs: PaginationType;
},
) =>
defaultSerializeQueryArgs({
endpointName,
queryArgs: omit(queryArgs, ['page']),
endpointDefinition,
});

export const paginatedQueryMerge = (currentCache: InfiniteResultType<any>, newItems: InfiniteResultType<any>) => {
const tempCache = { ...currentCache };
tempCache.pages = { ...currentCache.pages, ...newItems.pages };
return tempCache;
};

export const paginatedForceRefetch = ({ currentArg, previousArg }: { currentArg: any, previousArg: any }) =>
currentArg !== previousArg;
110 changes: 15 additions & 95 deletions src/core/rtkQuery/splitV3Api/collectionApi.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,41 @@
import { defaultSerializeQueryArgs } from '@reduxjs/toolkit/query';
import { omit } from 'lodash';

import {
paginatedForceRefetch,
paginatedQueryMerge,
serializePaginatedQueryArgs,
transformPaginatedResponse,
} from '@/core/rtkPaginationUtil';
import { splitV3Api } from '@/core/rtkQuery/splitV3Api';

import type { InfiniteResultType, ListResultType, PaginationType } from '@/core/types/api';
import type { CollectionFilterType, CollectionGroupType } from '@/core/types/api/collection';
import type { SeriesType } from '@/core/types/api/series';

const collectionApi = splitV3Api.injectEndpoints({
endpoints: build => ({
getGroupsInfinite: build.query<
InfiniteResultType<CollectionGroupType[]>,
PaginationType & { filterId: string, randomImages?: boolean, queryId: number }
>({
query: ({ filterId, queryId: _, randomImages, ...params }) => ({
url: `Filter/${filterId}/Group`,
params: { includeEmpty: true, randomImages, ...params },
}),
transformResponse: (response: ListResultType<CollectionGroupType[]>, _, args) => ({
pages: {
[args.page ?? 1]: response.List,
},
total: response.Total,
}),
// Only have one cache entry because the arg always maps to one string
serializeQueryArgs: ({ endpointDefinition, endpointName, queryArgs }) =>
defaultSerializeQueryArgs({
endpointName,
queryArgs: omit(queryArgs, ['page']),
endpointDefinition,
}),
// Always merge incoming data to the cache entry
merge: (currentCache, newItems) => {
const tempCache = { ...currentCache };
tempCache.pages = { ...currentCache.pages, ...newItems.pages };
return tempCache;
},
// Refetch when the page arg changes
forceRefetch({ currentArg, previousArg }) {
return currentArg !== previousArg;
},
}),
getGroup: build.query<CollectionGroupType, { groupId: string }>({
query: ({ groupId }) => ({ url: `Group/${groupId}` }),
}),
getGroupLetters: build.query<{ [index: string]: number }, { includeEmpty: boolean, topLevelOnly: boolean }>({
query: ({ includeEmpty = false, topLevelOnly = true }) => ({
url: 'Group/Letters',
params: { includeEmpty, topLevelOnly },
}),
}),

getGroupInfinite: build.query<
InfiniteResultType<CollectionGroupType[]>,
InfiniteResultType<CollectionGroupType>,
PaginationType & { randomImages?: boolean, startsWith?: string, topLevelOnly?: boolean, includeEmpty?: boolean }
>({
query: ({ ...params }) => ({
url: 'Group',
params: { ...params },
}),
transformResponse: (response: ListResultType<CollectionGroupType[]>, _, args) => ({
pages: {
[args.page ?? 1]: response.List,
},
total: response.Total,
}),
// Only have one cache entry because the arg always maps to one string
serializeQueryArgs: ({ endpointDefinition, endpointName, queryArgs }) =>
defaultSerializeQueryArgs({
endpointName,
queryArgs: omit(queryArgs, ['page']),
endpointDefinition,
}),
// Always merge incoming data to the cache entry
merge: (currentCache, newItems) => {
const tempCache = { ...currentCache };
tempCache.pages = { ...currentCache.pages, ...newItems.pages };
return tempCache;
},
// Refetch when the page arg changes
forceRefetch({ currentArg, previousArg }) {
return currentArg !== previousArg;
},
transformResponse: transformPaginatedResponse,
serializeQueryArgs: serializePaginatedQueryArgs,
merge: paginatedQueryMerge,
forceRefetch: paginatedForceRefetch,
}),
getGroupSeries: build.query<InfiniteResultType<SeriesType[]>, { groupId: string, randomImages?: boolean }>({
query: ({ groupId, randomImages = true }) => ({
url: `Group/${groupId}/Series`,
params: {
randomImages,
recursive: true,
includeDataFrom: 'AniDB',
},
}),
transformResponse: (response: SeriesType[]) => ({
pages: {
1: response,
},
total: response.length,
}),
}),
getTopFilters: build.query<ListResultType<CollectionFilterType[]>, PaginationType>({

getTopFilters: build.query<ListResultType<CollectionFilterType>, PaginationType>({
query: params => ({ url: 'Filter', params: { page: params.page ?? 1, pageSize: params.pageSize ?? 0 } }),
}),
getFilters: build.query<ListResultType<CollectionFilterType[]>, string>({

getFilters: build.query<ListResultType<CollectionFilterType>, string>({
query: filterId => ({ url: `Filter/${filterId}/Filter`, params: { page: 1, pageSize: 0 } }),
}),
getFilterGroupLetters: build.query<
{ [index: string]: number },
{ includeEmpty: boolean, topLevelOnly: boolean, filterId?: string }
>({
query: ({ filterId = '', includeEmpty = false, topLevelOnly = true }) => ({
url: `Filter/${filterId}/Group/Letters`,
params: { includeEmpty, topLevelOnly },
}),
}),
}),
});

Expand All @@ -122,7 +44,5 @@ export const {
useGetGroupQuery,
useLazyGetFiltersQuery,
useLazyGetGroupInfiniteQuery,
useLazyGetGroupSeriesQuery,
useLazyGetGroupsInfiniteQuery,
useLazyGetTopFiltersQuery,
} = collectionApi;
1 change: 0 additions & 1 deletion src/core/rtkQuery/splitV3Api/dashboardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
import type { SeriesType } from '@/core/types/api/series';

const dashboardApi = splitV3Api.injectEndpoints({
// refetchOnMountOrArgChange: true, // Refresh stats on component mount/page refresh (I think it works correctly)
endpoints: build => ({
// Get the counters of various collection stats
getDashboardStats: build.query<DashboardStatsType, void>({
Expand Down
21 changes: 0 additions & 21 deletions src/core/rtkQuery/splitV3Api/episodeApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { splitV3Api } from '@/core/rtkQuery/splitV3Api';

import type { DataSourceType } from '@/core/types/api/common';
import type { EpisodeAniDBType, EpisodeTvDBType, EpisodeType } from '@/core/types/api/episode';
import type { FileType } from '@/core/types/api/file';

type EpisodeFilesQuery = {
Expand All @@ -12,25 +11,8 @@ type EpisodeFilesQuery = {
includeMediaInfo?: boolean;
};

type EpisodeQuery = {
episodeId: string;
includeDataFrom?: DataSourceType[];
};

const episodeApi = splitV3Api.injectEndpoints({
endpoints: build => ({
// Get the Shoko.Server.API.v3.Models.Shoko.Episode.AniDB entry for the given episodeID.
getEpisodeAnidb: build.query<EpisodeAniDBType, number>({
query: episodeId => ({ url: `Episode/${episodeId}/AniDB` }),
}),
// Get the Shoko.Server.API.v3.Models.Shoko.Episode.AniDB entry for the given episodeID.
getEpisode: build.query<EpisodeType, EpisodeQuery>({
query: ({ episodeId, ...params }) => ({ url: `Episode/${episodeId}`, params }),
}),
// Get the Shoko.Server.API.v3.Models.Shoko.Episode.AniDB entry for the given episodeID.
getEpisodeTvdb: build.query<EpisodeTvDBType[], number>({
query: episodeId => ({ url: `Episode/${episodeId}/TvDB` }),
}),
// Get the Shoko.Server.API.v3.Models.Shoko.Files for the Shoko.Server.API.v3.Models.Shoko.Episode with the given episodeID.
getEpisodeFiles: build.query<FileType[], EpisodeFilesQuery>({
query: ({ episodeId, ...params }) => ({ url: `Episode/${episodeId}/File?includeAbsolutePaths=true`, params }),
Expand Down Expand Up @@ -60,9 +42,6 @@ const episodeApi = splitV3Api.injectEndpoints({
});

export const {
useGetEpisodeAnidbQuery,
useGetEpisodeQuery,
useGetEpisodeTvdbQuery,
useLazyGetEpisodeFilesQuery,
usePostEpisodeHiddenMutation,
usePostEpisodeWatchedMutation,
Expand Down
2 changes: 1 addition & 1 deletion src/core/rtkQuery/splitV3Api/fileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const fileApi = splitV3Api.injectEndpoints({
}),

// Get or search through the files accessible to the current user.
getFiles: build.query<ListResultType<FileType[]>, FileRequestType>({
getFiles: build.query<ListResultType<FileType>, FileRequestType>({
query: params => ({
url: 'File',
params,
Expand Down
44 changes: 15 additions & 29 deletions src/core/rtkQuery/splitV3Api/filterApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { defaultSerializeQueryArgs } from '@reduxjs/toolkit/query';
import { omit } from 'lodash';

import {
paginatedForceRefetch,
paginatedQueryMerge,
serializePaginatedQueryArgs,
transformPaginatedResponse,
} from '@/core/rtkPaginationUtil';
import { splitV3Api } from '@/core/rtkQuery/splitV3Api';

import type { InfiniteResultType, ListResultType, PaginationType } from '@/core/types/api';
import type { InfiniteResultType, PaginationType } from '@/core/types/api';
import type { CollectionGroupType } from '@/core/types/api/collection';
import type { FilterType } from '@/core/types/api/filter';
import type { SeriesType } from '@/core/types/api/series';
Expand All @@ -18,8 +21,9 @@ const filterApi = splitV3Api.injectEndpoints({
},
}),
}),

getFilteredGroupsInfinite: build.query<
InfiniteResultType<CollectionGroupType[]>,
InfiniteResultType<CollectionGroupType>,
PaginationType & { randomImages?: boolean, filterCriteria: FilterType }
>({
query: ({ filterCriteria, ...params }) => ({
Expand All @@ -28,32 +32,14 @@ const filterApi = splitV3Api.injectEndpoints({
params,
body: filterCriteria,
}),
transformResponse: (response: ListResultType<CollectionGroupType[]>, _, args) => ({
pages: {
[args.page ?? 1]: response.List,
},
total: response.Total,
}),
// Only have one cache entry because the arg always maps to one string
serializeQueryArgs: ({ endpointDefinition, endpointName, queryArgs }) =>
defaultSerializeQueryArgs({
endpointName,
queryArgs: omit(queryArgs, ['page']),
endpointDefinition,
}),
// Always merge incoming data to the cache entry
merge: (currentCache, newItems) => {
const tempCache = { ...currentCache };
tempCache.pages = { ...currentCache.pages, ...newItems.pages };
return tempCache;
},
// Refetch when the page arg changes
forceRefetch({ currentArg, previousArg }) {
return currentArg !== previousArg;
},
transformResponse: transformPaginatedResponse,
serializeQueryArgs: serializePaginatedQueryArgs,
merge: paginatedQueryMerge,
forceRefetch: paginatedForceRefetch,
}),

getFilteredGroupSeries: build.query<
InfiniteResultType<SeriesType[]>,
InfiniteResultType<SeriesType>,
{ groupId: string, randomImages?: boolean, filterCriteria: FilterType }
>({
query: ({ filterCriteria, groupId, ...params }) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/core/rtkQuery/splitV3Api/queueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const queueApi = splitV3Api.injectEndpoints({
},
}),
getQueueItems: build.query<
ListResultType<QueueItemType[]>,
ListResultType<QueueItemType>,
{ queueName: string, showAll?: boolean } & PaginationType
>({
query: ({ page = 1, pageSize = 10, queueName, showAll = true }) => ({
Expand Down
Loading

0 comments on commit 4772e3a

Please sign in to comment.