diff --git a/src/hooks/series/useEpisodes.ts b/src/hooks/series/useEpisodes.ts index 5334ac15a..f9de76446 100644 --- a/src/hooks/series/useEpisodes.ts +++ b/src/hooks/series/useEpisodes.ts @@ -36,12 +36,12 @@ export const useEpisodes = ( async ({ pageParam = 0 }) => { if (Number(seasonNumber)) { // Get episodes from a selected season using pagination - const season = await getSeasonWithEpisodes(seriesId || '', Number(seasonNumber), pageParam); + const season = await getSeasonWithEpisodes({ seriesId, seasonNumber: Number(seasonNumber), pageOffset: pageParam }); return { pagination: season.pagination, episodes: season.episodes }; } else { // Get episodes from a selected series using pagination - const data = await getEpisodes(seriesId || '', pageParam); + const data = await getEpisodes({ seriesId, pageOffset: pageParam }); return data; } }, diff --git a/src/hooks/series/useNextEpisode.ts b/src/hooks/series/useNextEpisode.ts index 60fda9e5e..bcbb648f1 100644 --- a/src/hooks/series/useNextEpisode.ts +++ b/src/hooks/series/useNextEpisode.ts @@ -4,11 +4,11 @@ import type { Series } from '#types/series'; import { SERIES_CACHE_TIME } from '#src/config'; import { getEpisodes } from '#src/services/api.service'; -export const useNextEpisode = ({ series, episodeId }: { series: Series | undefined; episodeId: string | null }) => { +export const useNextEpisode = ({ series, episodeId }: { series: Series | undefined; episodeId: string | undefined }) => { const { isLoading, data } = useQuery( ['next-episode', series?.series_id, episodeId], async () => { - const item = await getEpisodes(series?.series_id, null, 1, episodeId); + const item = await getEpisodes({ seriesId: series?.series_id, pageLimit: 1, afterId: episodeId }); return item?.episodes?.[0]; }, diff --git a/src/services/api.service.ts b/src/services/api.service.ts index 6de762543..9fa284ac1 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -149,20 +149,27 @@ export const getSeriesByMediaIds = async (mediaIds: string[]): Promise<{ [mediaI * Get all episodes of the selected series (when no particular season is selected or when episodes are attached to series) * @param {string} seriesId */ -export const getEpisodes = async ( - seriesId: string | undefined | null, - pageOffset: number | null, - pageLimit: number | null = PAGE_LIMIT, - afterId?: string | null, -): Promise => { +export const getEpisodes = async ({ + seriesId, + pageOffset, + pageLimit = PAGE_LIMIT, + afterId, +}: { + seriesId: string | undefined; + pageOffset?: number; + pageLimit?: number; + afterId?: string; +}): Promise => { if (!seriesId) { throw new Error('Series ID is required'); - } else if (pageOffset && afterId) { - throw new Error('page_offset and after_id are not allowed in the same query'); } const pathname = `/apps/series/${seriesId}/episodes`; - const url = addQueryParams(`${import.meta.env.APP_API_BASE_URL}${pathname}`, { page_offset: pageOffset, page_limit: pageLimit, after_id: afterId }); + const url = addQueryParams(`${import.meta.env.APP_API_BASE_URL}${pathname}`, { + page_offset: pageOffset, + page_limit: pageLimit, + after_id: afterId, + }); const response = await fetch(url); const { episodes, page, page_limit, total }: EpisodesRes = await getDataOrThrow(response); @@ -182,18 +189,23 @@ export const getEpisodes = async ( * Get season of the selected series * @param {string} seriesId */ -export const getSeasonWithEpisodes = async ( - seriesId: string | undefined, - seasonNumber: number, - pageOffset: number, - pageLimit?: number, -): Promise => { +export const getSeasonWithEpisodes = async ({ + seriesId, + seasonNumber, + pageOffset, + pageLimit = PAGE_LIMIT, +}: { + seriesId: string | undefined; + seasonNumber: number; + pageOffset?: number; + pageLimit?: number; +}): Promise => { if (!seriesId) { throw new Error('Series ID is required'); } const pathname = `/apps/series/${seriesId}/seasons/${seasonNumber}/episodes`; - const url = addQueryParams(`${import.meta.env.APP_API_BASE_URL}${pathname}`, { page_offset: pageOffset, page_limit: pageLimit || PAGE_LIMIT }); + const url = addQueryParams(`${import.meta.env.APP_API_BASE_URL}${pathname}`, { page_offset: pageOffset, page_limit: pageLimit }); const response = await fetch(url); const { episodes, page, page_limit, total }: EpisodesRes = await getDataOrThrow(response);