Skip to content

Commit

Permalink
feat(series): use named params
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh committed Jul 13, 2023
1 parent f4f8dea commit ba6560e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/hooks/series/useEpisodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/series/useNextEpisode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
},
Expand Down
44 changes: 28 additions & 16 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EpisodesWithPagination> => {
export const getEpisodes = async ({
seriesId,
pageOffset,
pageLimit = PAGE_LIMIT,
afterId,
}: {
seriesId: string | undefined;
pageOffset?: number;
pageLimit?: number;
afterId?: string;
}): Promise<EpisodesWithPagination> => {
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);
Expand All @@ -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<EpisodesWithPagination> => {
export const getSeasonWithEpisodes = async ({
seriesId,
seasonNumber,
pageOffset,
pageLimit = PAGE_LIMIT,
}: {
seriesId: string | undefined;
seasonNumber: number;
pageOffset?: number;
pageLimit?: number;
}): Promise<EpisodesWithPagination> => {
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);
Expand Down

0 comments on commit ba6560e

Please sign in to comment.