feat: trending blocks on main page

This commit is contained in:
ner committed 2024-09-02 19:57:54 +03:00
1 parent 8263a4abd4
commit aff28018f6
8 files changed
+122 -19

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
'use client';
import Marquee from '@/components/Marquee';
import { Badge } from '@/components/ui/badge';
import { FC } from 'react';
import useTrending from './useTrending';
const Trending: FC<{ label: string; type: 'trending' | 'streaming' | 'theater' | 'rent' }> = ({ label, type }) => {
const isMobile = typeof window === 'undefined' ? true : matchMedia('(max-width: 600px)').matches;
const { data: movies, isFetched } = useTrending({ type, enabled: !isMobile });
return (
isFetched && (
<>
<Badge className='mb-2 font-normal'>{label}</Badge>
<Marquee movies={movies || []} />
</>
)
);
};
export default Trending;
@@ -0,0 +1,13 @@
import { useQuery } from '@tanstack/react-query';
import { popularByType, trendingMovies } from '@/lib/api';
const useTrending = ({ type, enabled }: { enabled?: boolean; type: 'trending' | 'streaming' | 'theater' | 'rent' }) => {
return useQuery({
enabled,
queryKey: ['trending', type],
queryFn: () => (type === 'trending' ? trendingMovies() : popularByType({ type }))
});
};
export default useTrending;
+32 -9
View File
@@ -4,18 +4,41 @@ import Preview from '@/components/Movie/Preview';
import Carousel from './components/Carousel'; import Carousel from './components/Carousel';
import useRandomMovie from './useRandomMovie'; import useRandomMovie from './useRandomMovie';
import Trending from './components/Trending';
export default function Container() { const Random = () => {
const { movies, movie, index, onIndexChange, fetchNextPage } = useRandomMovie(); const { movies, movie, index, onIndexChange, fetchNextPage } = useRandomMovie();
return ( return (
<> <Preview
<Preview movie={movie}
movie={movie} className='bg-background rounded-md'
className='bg-background rounded-md' card={<Carousel index={index} movies={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
card={<Carousel index={index} movies={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />} />
/> );
<div className='w-full h-2' /> };
</>
export default function Container() {
return (
<div className='flex flex-col flex-1'>
<div className='xs:mb-4'>
<Random />
<div className='w-full h-2' />
</div>
<div className='xs:flex hidden flex-1 w-full h-full overflow-auto snap-mandatory snap-y relative rounded-lg no-scrollbar'>
<div className='absolute left-0 top-0 w-full'>
<div className='snap-center'>
<Trending label='Trending' type='trending' />
</div>
<div className='mt-6 snap-center'>
<Trending label='Streaming' type='streaming' />
</div>
<div className='mt-6 mb-6 snap-center'>
<Trending label='In theaters' type='theater' />
</div>
</div>
</div>
</div>
); );
} }
+2 -2
View File
@@ -34,10 +34,10 @@ export default function RootLayout({ children }: { children: ReactNode }) {
</head> </head>
<body style={poppins.style}> <body style={poppins.style}>
<Providers theme={theme}> <Providers theme={theme}>
<div className='sticky top-0 z-20'> <div className='fixed w-full left-0 top-0 z-20'>
<Header /> <Header />
</div> </div>
<main className='px-2 mx-auto max-w-[1240px]'>{children}</main> <main className='px-2 mx-auto flex flex-col flex-1 w-full max-w-[1240px] mt-[56px]'>{children}</main>
</Providers> </Providers>
</body> </body>
</html> </html>
+21
View File
@@ -0,0 +1,21 @@
import { FC } from 'react';
import Card from '../Movie/Card';
import { showPreviewModal } from '../Movie/Preview';
interface Props {
movies: Movie[];
}
const Marquee: FC<Props> = ({ movies }) => {
return (
<ul className='flex overflow-auto gap-2 rounded-lg snap-mandatory snap-x no-scrollbar'>
{movies.map((movie) => (
<li key={movie.id} className='snap-start'>
<Card className='!h-[250px] !w-[175px] text-sm' movie={movie} onClick={() => showPreviewModal({ movie })} />
</li>
))}
</ul>
);
};
export default Marquee;
+2 -2
View File
@@ -21,7 +21,7 @@ const Card: FC<Props> = ({ movie, onClick, className, containerProps }) => {
<div <div
{...(containerProps || {})} {...(containerProps || {})}
className={cn( className={cn(
'card-aspect-ratio relative bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg', 'card-aspect-ratio relative bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg text-lg',
className className
)} )}
> >
@@ -30,7 +30,7 @@ const Card: FC<Props> = ({ movie, onClick, className, containerProps }) => {
</div> </div>
<div className='text-white absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 card-gradient'> <div className='text-white absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 card-gradient'>
<Handlers movie={movie} onClick={onClick} /> <Handlers movie={movie} onClick={onClick} />
<div className='grid grid-cols-[1fr_auto] text-lg opacity-75 gap-2'> <div className='grid grid-cols-[1fr_auto] opacity-75 gap-2'>
<span className='overflow-ellipsis overflow-hidden whitespace-nowrap'>{movie.name || movie.title}</span> <span className='overflow-ellipsis overflow-hidden whitespace-nowrap'>{movie.name || movie.title}</span>
{Boolean(movie.release_date) && <span>{new Date(movie.release_date).getFullYear()}</span>} {Boolean(movie.release_date) && <span>{new Date(movie.release_date).getFullYear()}</span>}
</div> </div>
+23 -6
View File
@@ -45,6 +45,21 @@ export const randomMovies = async ({ filters }: { filters: Partial<Filters> } =
.then((data) => filterUnknownMovies(data.results)); .then((data) => filterUnknownMovies(data.results));
}; };
export const popularByType = async ({ type }: { type: 'streaming' | 'theater' | 'rent' }): Promise<Movie[]> => {
const params =
type === 'theater'
? { with_release_type: 3 }
: { with_watch_monetization_types: type === 'streaming' ? 'flatrate' : 'rent' };
return server
.get<MovieDBResponse>('/discover/movie', {
params: {
...params,
watch_region: 'US'
}
})
.then((data) => data.results);
};
export const popularMovies = async ({ page }: { page?: string } = {}): Promise<MovieDBResponse> => { export const popularMovies = async ({ page }: { page?: string } = {}): Promise<MovieDBResponse> => {
return server.get('/discover/movie', { return server.get('/discover/movie', {
params: { params: {
@@ -59,12 +74,14 @@ export const popularMovies = async ({ page }: { page?: string } = {}): Promise<M
}); });
}; };
export const trendingMovies = async ({ page }: { page?: string } = {}): Promise<MovieDBResponse> => { export const trendingMovies = async ({ page }: { page?: string } = {}): Promise<Movie[]> => {
return server.get('/trending/movie/day', { return server
params: { .get<MovieDBResponse>('/trending/movie/day', {
page params: {
} page
}); }
})
.then((data) => data.results);
}; };
export const topMovies = async ({ export const topMovies = async ({
+7
View File
@@ -89,6 +89,13 @@
} }
} }
html,
body {
height: 100%;
display: flex;
flex-direction: column;
}
* { * {
-webkit-tap-highlight-color: transparent; -webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none; -webkit-touch-callout: none;