refactoring: server actions data fetching -> api routes

This commit is contained in:
ner committed 2024-10-26 21:33:39 +03:00
1 parent 1111712d2c
commit 5cbf93b3b8
38 files changed
+326 -308

No files matched your search

@@ -1,22 +1,18 @@
import { useRef } from 'react';
import { search } from '@/api';
import searchQuery, { SearchQueryProps } from '@/api/queries/search';
import useInfiniteList from '@/hooks/useInfiniteList';
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) => {
const useSearch = (props: SearchQueryProps) => {
const previousRef = useRef<Show[]>([]);
const infiniteQuery = useInfiniteList({
queryKey: ['search', query, type],
queryFn: ({ page }) => search({ query, page, type }),
enabled: Boolean(query)
});
const infiniteQuery = useInfiniteList(searchQuery(props));
if (infiniteQuery.isFetched) {
previousRef.current = infiniteQuery.shows;
}
if (!query) {
if (!props.query) {
previousRef.current = [];
}
@@ -1,20 +1,9 @@
import { useQuery } from '@tanstack/react-query';
import { credits } from '@/api';
import creditsQuery, { CreditsQueryProps } from '@/api/queries/credits';
interface Props {
showId?: Show['id'];
showType?: Show['type'];
}
export const KEY = ({ showId, showType }: Props) => ['credits', showId, showType];
const useCredits = ({ showId, showType }: Props) => {
return useQuery({
queryKey: KEY({ showId, showType }),
queryFn: () => credits({ showId, showType }),
enabled: Boolean(showId)
});
const useCredits = ({ showId, showType }: CreditsQueryProps) => {
return useQuery(creditsQuery({ showId, showType }));
};
export default useCredits;
+2 -9
View File
@@ -1,19 +1,12 @@
import { useQuery } from '@tanstack/react-query';
import { details } from '@/api';
import detailsQuery from '@/api/queries/details';
interface Props {
showId?: Show['id'];
showType?: Show['type'];
}
export const KEY = ({ showId, showType = 'movie' }: Props) => ['details', showId, showType];
const useDetails = ({ showId, showType = 'movie' }: Props) =>
useQuery({
enabled: Boolean(showId && showType),
queryKey: KEY({ showId, showType }),
queryFn: () => details({ showId: showId!, showType })
});
const useDetails = ({ showId, showType = 'movie' }: Props) => useQuery(detailsQuery({ showId, showType }));
export default useDetails;
+2 -6
View File
@@ -3,7 +3,7 @@
import { startTransition, useCallback } from 'react';
import { atom, getDefaultStore, useAtom } from 'jotai';
import { randomMovies } from '@/api';
import randomQuery from '@/api/queries/random';
import useInfiniteList from '@/hooks/useInfiniteList';
const indexAtom = atom(0);
@@ -13,11 +13,7 @@ export const useSilentIndex = () => getDefaultStore().get(indexAtom);
const useRandomMovie = () => {
const [index, setIndex] = useAtom(indexAtom);
const { shows: movies, fetchNextPage } = useInfiniteList({
suspense: true,
queryKey: ['random-movie'],
queryFn: randomMovies
});
const { shows: movies, fetchNextPage } = useInfiniteList(randomQuery());
if (movies.length && !movies[index]) {
setIndex(0);
@@ -1,3 +1,5 @@
'use client';
import { FC } from 'react';
import Poster from '@/components/Poster';
+2 -8
View File
@@ -1,13 +1,7 @@
import { useQuery } from '@tanstack/react-query';
import { popularByType, trendingMovies } from '@/api';
import trendingQuery, { TrendingQueryProps } from '@/api/queries/trending';
const useTrending = ({ type, enabled }: { enabled?: boolean; type: 'trending' | 'streaming' | 'theater' | 'rent' }) => {
return useQuery({
enabled,
queryKey: ['trending', type],
queryFn: () => (type === 'trending' ? trendingMovies() : popularByType({ type }))
});
};
const useTrending = ({ type }: TrendingQueryProps) => useQuery(trendingQuery({ type }));
export default useTrending;