11import { getApi } from '@/app/api/config/appConfig' ;
22import { useAuthStore } from '@/domains/shared/store/auth' ;
3- import { useInfiniteQuery , useQuery } from '@tanstack/react-query' ;
3+ import { useInfiniteQuery , useQuery , useQueryClient } from '@tanstack/react-query' ;
44import { Cocktail , Sort } from '../types/types' ;
5+ import { useEffect , useRef } from 'react' ;
56
67interface CocktailResponse {
78 data : Cocktail [ ] ;
@@ -22,6 +23,11 @@ interface CocktailFilter extends SearchFilters {
2223 sortBy ?: Sort ;
2324}
2425
26+ interface PageParam {
27+ lastId : number ;
28+ lastValue : number | string ;
29+ }
30+
2531const fetchKeep = async ( ) : Promise < Set < number > > => {
2632 const res = await fetch ( `${ getApi } /me/bar` , {
2733 method : 'GET' ,
@@ -36,15 +42,15 @@ const fetchKeep = async (): Promise<Set<number>> => {
3642} ;
3743
3844const fetchRecipe = async (
39- lastId : number | null ,
45+ pageParam : PageParam | null ,
4046 size : number ,
4147 sortBy ?: Sort
4248) : Promise < Cocktail [ ] > => {
4349 const url = new URL ( `${ getApi } /cocktails` ) ;
4450 url . searchParams . set ( 'size' , String ( size ) ) ;
45- if ( lastId !== null ) {
46- url . searchParams . set ( 'lastId' , String ( lastId ) ) ;
47- url . searchParams . set ( 'lastValue' , String ( lastId ) ) ;
51+ if ( pageParam ) {
52+ url . searchParams . set ( 'lastId' , String ( pageParam . lastId ) ) ;
53+ url . searchParams . set ( 'lastValue' , String ( pageParam . lastValue ) ) ;
4854 }
4955
5056 if ( sortBy ) {
@@ -95,6 +101,18 @@ const hasActiveFilters = (filters: SearchFilters): boolean => {
95101
96102export const useCocktailsInfiniteQuery = ( size : number = 20 , sortBy ?: Sort ) => {
97103 const user = useAuthStore ( ( state ) => state . user ) ;
104+ const queryClient = useQueryClient ( ) ;
105+ const prevSortBy = useRef ( sortBy ) ;
106+
107+ useEffect ( ( ) => {
108+ if ( prevSortBy . current !== undefined && prevSortBy . current !== sortBy ) {
109+ queryClient . removeQueries ( {
110+ queryKey : [ 'cocktails' , 'infinite' , prevSortBy . current ] ,
111+ } ) ;
112+ }
113+ prevSortBy . current = sortBy ;
114+ } , [ sortBy , queryClient ] ) ;
115+
98116 return useInfiniteQuery ( {
99117 queryKey : [ 'cocktails' , 'infinite' , sortBy , size , user ?. id ] ,
100118 queryFn : async ( { pageParam } ) => {
@@ -110,11 +128,37 @@ export const useCocktailsInfiniteQuery = (size: number = 20, sortBy?: Sort) => {
110128
111129 return cocktails ;
112130 } ,
113- getNextPageParam : ( lastpage ) => {
114- if ( lastpage . length < size ) return undefined ;
115- return lastpage [ lastpage . length - 1 ] ?. cocktailId ?? undefined ;
131+ getNextPageParam : ( lastPage ) => {
132+ if ( lastPage . length < size ) {
133+ return undefined ;
134+ }
135+
136+ const lastItem = lastPage [ lastPage . length - 1 ] ;
137+ if ( ! lastItem ) return undefined ;
138+
139+ let lastValue : number | string ;
140+
141+ switch ( sortBy ) {
142+ case 'keeps' :
143+ lastValue = lastItem . keepCount ?? lastItem . cocktailId ;
144+ break ;
145+ case 'comments' :
146+ lastValue = lastItem . commentCount ?? lastItem . cocktailId ;
147+ break ;
148+ case 'recent' :
149+ default :
150+ lastValue = lastItem . cocktailId ;
151+ break ;
152+ }
153+
154+ return {
155+ lastId : lastItem . cocktailId ,
156+ lastValue : lastValue ,
157+ } ;
116158 } ,
117- initialPageParam : null as number | null ,
159+ initialPageParam : null as PageParam | null ,
160+ refetchOnMount : false ,
161+ refetchOnWindowFocus : false ,
118162 } ) ;
119163} ;
120164
@@ -161,13 +205,17 @@ export const useCocktails = (
161205 }
162206
163207 const allCocktails = infiniteQuery . data ?. pages . flatMap ( ( page ) => page ) ?? [ ] ;
208+ const uniqueCocktails = allCocktails . filter (
209+ ( cocktail , index , self ) => index === self . findIndex ( ( c ) => c . cocktailId === cocktail . cocktailId )
210+ ) ;
164211
212+ const hasDuplicates = allCocktails . length !== uniqueCocktails . length ;
165213 return {
166- data : allCocktails ,
214+ data : uniqueCocktails ,
167215 noResults : false ,
168216 isSearchMode : false ,
169217 fetchNextPage : infiniteQuery . fetchNextPage ,
170- hasNextPage : infiniteQuery . hasNextPage ,
218+ hasNextPage : hasDuplicates ? false : infiniteQuery . hasNextPage ,
171219 isFetchingNextPage : infiniteQuery . isFetchingNextPage ,
172220 } ;
173221} ;
0 commit comments