From aff28018f607600f68243b80b80c6c7f8f5c3ea3 Mon Sep 17 00:00:00 2001 From: ner Date: Mon, 2 Sep 2024 19:57:54 +0300 Subject: [PATCH] feat: trending blocks on main page --- app/(site)/components/Trending/index.tsx | 22 ++++++++++ app/(site)/components/Trending/useTrending.ts | 13 ++++++ app/(site)/container.tsx | 41 +++++++++++++++---- app/layout.tsx | 4 +- components/Marquee/index.tsx | 21 ++++++++++ components/Movie/Card.tsx | 4 +- lib/api/index.ts | 29 ++++++++++--- styles/index.css | 7 ++++ 8 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 app/(site)/components/Trending/index.tsx create mode 100644 app/(site)/components/Trending/useTrending.ts create mode 100644 components/Marquee/index.tsx diff --git a/app/(site)/components/Trending/index.tsx b/app/(site)/components/Trending/index.tsx new file mode 100644 index 0000000..04d15ed --- /dev/null +++ b/app/(site)/components/Trending/index.tsx @@ -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 && ( + <> + {label} + + + ) + ); +}; + +export default Trending; diff --git a/app/(site)/components/Trending/useTrending.ts b/app/(site)/components/Trending/useTrending.ts new file mode 100644 index 0000000..5c6655e --- /dev/null +++ b/app/(site)/components/Trending/useTrending.ts @@ -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; diff --git a/app/(site)/container.tsx b/app/(site)/container.tsx index 0e3ebeb..97fbf9d 100644 --- a/app/(site)/container.tsx +++ b/app/(site)/container.tsx @@ -4,18 +4,41 @@ import Preview from '@/components/Movie/Preview'; import Carousel from './components/Carousel'; import useRandomMovie from './useRandomMovie'; +import Trending from './components/Trending'; -export default function Container() { +const Random = () => { const { movies, movie, index, onIndexChange, fetchNextPage } = useRandomMovie(); return ( - <> - } - /> -
- + } + /> + ); +}; + +export default function Container() { + return ( +
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+
+
); } diff --git a/app/layout.tsx b/app/layout.tsx index 06bbc89..e478f68 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -34,10 +34,10 @@ export default function RootLayout({ children }: { children: ReactNode }) { -
+
-
{children}
+
{children}
diff --git a/components/Marquee/index.tsx b/components/Marquee/index.tsx new file mode 100644 index 0000000..6367acb --- /dev/null +++ b/components/Marquee/index.tsx @@ -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 = ({ movies }) => { + return ( +
    + {movies.map((movie) => ( +
  • + showPreviewModal({ movie })} /> +
  • + ))} +
+ ); +}; + +export default Marquee; diff --git a/components/Movie/Card.tsx b/components/Movie/Card.tsx index 16e1f3c..ff04686 100644 --- a/components/Movie/Card.tsx +++ b/components/Movie/Card.tsx @@ -21,7 +21,7 @@ const Card: FC = ({ movie, onClick, className, containerProps }) => {
@@ -30,7 +30,7 @@ const Card: FC = ({ movie, onClick, className, containerProps }) => {
-
+
{movie.name || movie.title} {Boolean(movie.release_date) && {new Date(movie.release_date).getFullYear()}}
diff --git a/lib/api/index.ts b/lib/api/index.ts index fa1e3ae..5747d41 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -45,6 +45,21 @@ export const randomMovies = async ({ filters }: { filters: Partial } = .then((data) => filterUnknownMovies(data.results)); }; +export const popularByType = async ({ type }: { type: 'streaming' | 'theater' | 'rent' }): Promise => { + const params = + type === 'theater' + ? { with_release_type: 3 } + : { with_watch_monetization_types: type === 'streaming' ? 'flatrate' : 'rent' }; + return server + .get('/discover/movie', { + params: { + ...params, + watch_region: 'US' + } + }) + .then((data) => data.results); +}; + export const popularMovies = async ({ page }: { page?: string } = {}): Promise => { return server.get('/discover/movie', { params: { @@ -59,12 +74,14 @@ export const popularMovies = async ({ page }: { page?: string } = {}): Promise => { - return server.get('/trending/movie/day', { - params: { - page - } - }); +export const trendingMovies = async ({ page }: { page?: string } = {}): Promise => { + return server + .get('/trending/movie/day', { + params: { + page + } + }) + .then((data) => data.results); }; export const topMovies = async ({ diff --git a/styles/index.css b/styles/index.css index 37d00c1..3b0ceef 100644 --- a/styles/index.css +++ b/styles/index.css @@ -89,6 +89,13 @@ } } +html, +body { + height: 100%; + display: flex; + flex-direction: column; +} + * { -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none;