refactor: clean code and remove unnecessary dependencies

This commit is contained in:
ner committed 2024-09-30 18:54:13 +03:00
1 parent eceae6fec7
commit ebf599eed6
92 files changed
+1152 -2842

No files matched your search

+13 -14
View File
@@ -2,32 +2,31 @@
import { useSearchParams } from 'next/navigation';
import List from '@/components/Movie/List';
import List from '@/components/List';
import NoResults from '@/components/NoResults';
import useSimilar from './useSimilar';
export default function Container() {
const Container = () => {
const params = useSearchParams();
const title = params.get('title');
const type = params.get('type');
const movieId = params.get('movieId') || undefined;
const title = params.get('title') as string;
const showId = params.get('id') as string;
const showType = params.get('type') as Show['type'];
const { data: top, hasNextPage, fetchNextPage, isFetched } = useSimilar({ movieId, type: type as ShowType });
const { shows, fetchNextPage, isFetched } = useSimilar({ showId, showType });
return (
<>
{Boolean(title) && (
<span className='inline-flex w-full h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground mb-2'>
Similar: {decodeURIComponent(title || '')}
Similar: {decodeURIComponent(title)}
</span>
)}
<List type={type as ShowType} pages={top?.pages || []} fetchNextPage={fetchNextPage} hasNextPage={hasNextPage} />
{isFetched && !top?.pages?.[0]?.results?.length && (
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
Nothing was found
</div>
)}
<List shows={shows} fetchNextPage={fetchNextPage} />
{isFetched && !shows.length && <NoResults />}
</>
);
}
};
export default Container;
+7 -7
View File
@@ -1,18 +1,18 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { similarMovies } from '@/lib/api';
import { similarShows } from '@/api';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
export default async function Similar({ searchParams }: { searchParams: Record<string, string> }) {
const Similar = async ({ searchParams }: { searchParams: Record<string, string> }) => {
const queryClient = getQueryClient();
const { movieId, type } = searchParams || {};
const { id, type } = searchParams || {};
await queryClient.prefetchInfiniteQuery({
queryKey: ['similar', movieId, type],
queryFn: () => similarMovies({ movieId, type: type as ShowType }),
queryKey: ['similar', id, type],
queryFn: () => similarShows({ showId: id, showType: type as Show['type'] }),
initialPageParam: '1'
});
@@ -21,6 +21,6 @@ export default async function Similar({ searchParams }: { searchParams: Record<s
<Container />
</HydrationBoundary>
);
}
};
export const revalidate = 3600;
export default Similar;
+11 -17
View File
@@ -1,24 +1,18 @@
'use client';
import { useInfiniteQuery } from '@tanstack/react-query';
import { similarShows } from '@/api';
import useInfiniteList from '@/hooks/useInfiniteList';
import { similarMovies } from '@/lib/api';
interface Props {
showId: string;
showType: Show['type'];
}
const useSimilar = ({ movieId, type = 'movie' }: { movieId?: string; type?: ShowType }) => {
return useInfiniteQuery({
queryKey: ['similar', movieId, type],
enabled: Boolean(movieId),
queryFn: ({ pageParam }) => {
return similarMovies({ page: pageParam as string, movieId, type });
},
getNextPageParam(lastPage) {
const { page, total_pages } = lastPage as { page: number; total_pages: number };
if (page === total_pages) return undefined;
return `${page + 1}`;
},
initialPageParam: '1'
const useSimilar = ({ showId, showType = 'movie' }: Props) =>
useInfiniteList({
enabled: Boolean(showId),
queryKey: ['similar', showId, showType],
queryFn: ({ page }) => similarShows({ page, showId, showType })
});
};
export default useSimilar;