feat: swiper
This commit is contained in:
1 parent
cd8928ab54
commit
12223b79d5
20 files changed
+222
-164
No files matched your search
+56
-27
@@ -1,11 +1,14 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { ArrowLeft, ArrowRight, SlidersHorizontal } from 'lucide-react';
|
||||
import { EffectCreative } from 'swiper/modules';
|
||||
import { Swiper, SwiperSlide } from 'swiper/react';
|
||||
|
||||
import Card from '@/components/Movie/Card';
|
||||
import Movie from '@/components/Movie/Preview';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
@@ -17,10 +20,23 @@ import useRandomMovie from './useRandomMovie';
|
||||
|
||||
const isShowFilterAtom = atomWithStorage('is-show-filters', false);
|
||||
|
||||
const creativeEffectProps = {
|
||||
creativeEffect: {
|
||||
prev: {
|
||||
shadow: true,
|
||||
translate: ['-120%', 0, -500]
|
||||
},
|
||||
next: {
|
||||
shadow: true,
|
||||
translate: ['120%', 0, -500]
|
||||
}
|
||||
},
|
||||
modules: [EffectCreative]
|
||||
};
|
||||
|
||||
export default function Container() {
|
||||
const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom);
|
||||
const { movie, next, previous, hasPrevious } = useRandomMovie();
|
||||
const [direction, setDirection] = useState(0);
|
||||
const { ref, hasPrevious, movies, movie, next, previous, index, onIndexChange } = useRandomMovie();
|
||||
const isMounted = useMounted();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -29,37 +45,50 @@ export default function Container() {
|
||||
queryClient.prefetchQuery({ queryKey: ['genres'], queryFn: () => genres() });
|
||||
}, []);
|
||||
|
||||
const handleChangeMovie = (direction: 1 | -1) => () => {
|
||||
direction === 1 ? next() : previous();
|
||||
setDirection(direction);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{isShowFilter && <Filter />}
|
||||
<Movie movie={movie} direction={direction}>
|
||||
<div className='gap-2 flex w-full bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 lg:static max-sm:z-10 max-sm:fixed max-sm:bottom-0 max-sm:left-0 max-sm:w-full max-sm:p-2 max-sm:px-2 max-sm:mb-0'>
|
||||
{isMounted &&
|
||||
createPortal(
|
||||
<Button
|
||||
className='grow h-12 sm:h-8'
|
||||
onClick={handleChangeMovie(-1)}
|
||||
disabled={!hasPrevious}
|
||||
title='Previous movie'
|
||||
className='p-2'
|
||||
onClick={() => setIsShowFilter((isShow) => !isShow)}
|
||||
title={isShowFilter ? 'Hide filters' : 'Show filters'}
|
||||
variant='ghost'
|
||||
>
|
||||
<SlidersHorizontal size={19} />
|
||||
</Button>,
|
||||
document.getElementById('actions')!
|
||||
)}
|
||||
<Movie
|
||||
movie={movie}
|
||||
className='bg-background rounded-md'
|
||||
renderCard={() => (
|
||||
<Swiper
|
||||
onSwiper={(instance) => (ref.current = instance)}
|
||||
effect='creative'
|
||||
lazyPreloadPrevNext={5}
|
||||
resistance={false}
|
||||
longSwipesRatio={0.25}
|
||||
speed={600}
|
||||
initialSlide={index}
|
||||
onActiveIndexChange={onIndexChange}
|
||||
className='w-[300px] shrink-0'
|
||||
{...creativeEffectProps}
|
||||
>
|
||||
{movies?.map((movie) => (
|
||||
<SwiperSlide key={movie.id}>
|
||||
<Card movie={movie} />
|
||||
</SwiperSlide>
|
||||
))}
|
||||
</Swiper>
|
||||
)}
|
||||
>
|
||||
<div className='gap-2 flex w-full bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 lg:static max-sm:z-10 max-sm:fixed max-sm:bottom-0 max-sm:left-0 max-sm:w-full max-sm:p-2 max-sm:px-2 max-sm:mb-0'>
|
||||
<Button className='grow h-8' onClick={previous} disabled={!hasPrevious} title='Previous movie'>
|
||||
<ArrowLeft size={24} />
|
||||
</Button>
|
||||
{isMounted &&
|
||||
createPortal(
|
||||
<Button
|
||||
className='p-2'
|
||||
onClick={() => setIsShowFilter((isShow) => !isShow)}
|
||||
title={isShowFilter ? 'Hide filters' : 'Show filters'}
|
||||
variant='ghost'
|
||||
>
|
||||
<SlidersHorizontal size={19} />
|
||||
</Button>,
|
||||
document.getElementById('actions')!
|
||||
)}
|
||||
<Button className='grow h-12 sm:h-8' onClick={handleChangeMovie(1)} title='Next movie'>
|
||||
<Button className='grow h-8' onClick={next} title='Next movie'>
|
||||
<ArrowRight size={24} />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Container from './container';
|
||||
|
||||
export default async function Favorites() {
|
||||
export default function Favorites() {
|
||||
return <Container />;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ queryClient.prefetchInfiniteQuery({
|
||||
initialPageParam: '1'
|
||||
});
|
||||
|
||||
export default async function Popular() {
|
||||
export default function Popular() {
|
||||
return (
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
<Container />
|
||||
|
||||
@@ -13,7 +13,7 @@ queryClient.prefetchInfiniteQuery({
|
||||
initialPageParam: '1'
|
||||
});
|
||||
|
||||
export default async function Top() {
|
||||
export default function Top() {
|
||||
return (
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
<Container />
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { Swiper } from 'swiper/types';
|
||||
|
||||
import useFilters from '@/app/(site)/useFilters';
|
||||
import { randomMovies } from '@/lib/api';
|
||||
@@ -15,7 +16,8 @@ const useRandomMovie = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { filters } = useFilters();
|
||||
const indexByFilter = previousIndexes[JSON.stringify(filters)];
|
||||
const [index, setIndex] = useState(indexByFilter || 0);
|
||||
const [index, setIndex] = useState(indexByFilter);
|
||||
const ref = useRef<Swiper>();
|
||||
|
||||
const { data: movies, refetch } = useQuery({
|
||||
queryKey: ['random-movie', filters],
|
||||
@@ -43,28 +45,25 @@ const useRandomMovie = () => {
|
||||
setIndex(indexByFilter || 0);
|
||||
}, [indexByFilter]);
|
||||
|
||||
const previous = () => {
|
||||
if (!index) return;
|
||||
const previous = () => ref.current?.slidePrev();
|
||||
|
||||
setIndex((index) => index - 1);
|
||||
};
|
||||
|
||||
const next = () => {
|
||||
if ((index && index % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) {
|
||||
refetch();
|
||||
}
|
||||
|
||||
const nextIndex = index + 1;
|
||||
|
||||
previousIndexes[JSON.stringify(filters)] = nextIndex;
|
||||
setIndex(nextIndex);
|
||||
};
|
||||
const next = () => ref.current?.slideNext();
|
||||
|
||||
return {
|
||||
movies,
|
||||
movie: movies?.[index],
|
||||
hasPrevious: index > 0,
|
||||
onIndexChange: ({ activeIndex }: Swiper) => {
|
||||
if ((activeIndex && activeIndex % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) {
|
||||
refetch();
|
||||
}
|
||||
previousIndexes[JSON.stringify(filters)] = activeIndex;
|
||||
setIndex(activeIndex);
|
||||
},
|
||||
next,
|
||||
previous
|
||||
previous,
|
||||
index,
|
||||
ref
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React, { ReactNode } from 'react';
|
||||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
||||
import type { Metadata, Viewport } from 'next';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
@@ -33,7 +32,6 @@ export default function RootLayout({ children }: { children: ReactNode }) {
|
||||
<Providers theme={theme}>
|
||||
<Header />
|
||||
<main className='px-2 max-w-[1260px] mx-auto'>{children}</main>
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in new issue
Block a user