fix: swiper
This commit is contained in:
1 parent
1b87355925
commit
1aa145a98c
13 files changed
+125
-123
No files matched your search
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { Swiper } from 'swiper/types';
|
||||
|
||||
@@ -17,6 +17,7 @@ const useRandomMovie = () => {
|
||||
const indexByFilter = previousIndexes[JSON.stringify(filters)];
|
||||
const [index, setIndex] = useState(indexByFilter);
|
||||
const ref = useRef<Swiper>();
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
|
||||
const { data: movies, refetch } = useQuery({
|
||||
queryKey: ['random-movie', filters],
|
||||
@@ -49,13 +50,30 @@ const useRandomMovie = () => {
|
||||
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);
|
||||
},
|
||||
onIndexChange: useCallback(
|
||||
({ activeIndex }: Swiper) => {
|
||||
if ((activeIndex && activeIndex % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) {
|
||||
refetch();
|
||||
}
|
||||
clearTimeout(timeout.current);
|
||||
previousIndexes[JSON.stringify(filters)] = activeIndex;
|
||||
timeout.current = setTimeout(() => {
|
||||
setIndex(activeIndex);
|
||||
}, 100);
|
||||
},
|
||||
[movies, filters]
|
||||
),
|
||||
onDrag: useCallback(
|
||||
({ activeIndex }: Swiper) => {
|
||||
clearTimeout(timeout.current);
|
||||
timeout.current = setTimeout(() => {
|
||||
if (index === activeIndex) return;
|
||||
|
||||
setIndex(activeIndex);
|
||||
}, 100);
|
||||
},
|
||||
[index]
|
||||
),
|
||||
next,
|
||||
previous,
|
||||
index,
|
||||
|
||||
Reference in new issue
Block a user