@@ -3,7 +3,7 @@ import { supabase } from "@/integrations/supabase/client";
33import { useToast } from "@/components/ui/use-toast" ;
44import { offlineStorage } from "@/lib/offlineStorage" ;
55import { useOnlineStatus , useOfflineQueue } from "@/hooks/useOffline" ;
6- import { voteQueries } from "@/services/queries" ;
6+ import { voteQueries , setQueries , FestivalSet } from "@/services/queries" ;
77import type { User } from "@supabase/supabase-js" ;
88
99interface OfflineVote {
@@ -176,6 +176,60 @@ export function useOfflineVoteMutation(
176176 } ,
177177 ) ;
178178
179+ // Also update the sets cache to reflect vote count changes
180+ queryClient . setQueriesData (
181+ { queryKey : setQueries . all ( ) } ,
182+ ( oldData : any ) => {
183+ if ( ! oldData ) return oldData ;
184+
185+ // Handle different data structures (could be array of sets or object with sets property)
186+ function updateSetsArray ( sets : Array < FestivalSet > ) {
187+ return sets . map ( ( set : FestivalSet ) => {
188+ if ( set . id === setId ) {
189+ const updatedVotes = [ ...( set . votes || [ ] ) ] ;
190+
191+ // Remove existing vote from this user for this set
192+ const existingVoteIndex = updatedVotes . findIndex (
193+ ( vote : any ) => vote . user_id === userId ,
194+ ) ;
195+ if ( existingVoteIndex !== - 1 ) {
196+ updatedVotes . splice ( existingVoteIndex , 1 ) ;
197+ }
198+
199+ // Add new vote if not a toggle
200+ if ( ! isToggle ) {
201+ updatedVotes . push ( {
202+ vote_type : voteType ,
203+ user_id : userId ,
204+ } ) ;
205+ }
206+
207+ return {
208+ ...set ,
209+ votes : updatedVotes ,
210+ } ;
211+ }
212+ return set ;
213+ } ) ;
214+ }
215+
216+ // Handle array of sets
217+ if ( Array . isArray ( oldData ) ) {
218+ return updateSetsArray ( oldData ) ;
219+ }
220+
221+ // Handle object with sets property
222+ if ( oldData . sets && Array . isArray ( oldData . sets ) ) {
223+ return {
224+ ...oldData ,
225+ sets : updateSetsArray ( oldData . sets ) ,
226+ } ;
227+ }
228+
229+ return oldData ;
230+ } ,
231+ ) ;
232+
179233 // If online, sync immediately
180234 if ( isOnline ) {
181235 try {
@@ -233,14 +287,15 @@ export function useOfflineVoteMutation(
233287
234288 return isToggle ? null : voteType ;
235289 } ,
236- onError : ( error : Error ) => {
290+ onSettled : ( ) => {
237291 // Revert optimistic update on error
238292 if ( user ?. id ) {
239293 queryClient . invalidateQueries ( {
240294 queryKey : voteQueries . user ( user . id ) ,
241295 } ) ;
242296 }
243-
297+ } ,
298+ onError : ( error : any ) => {
244299 toast ( {
245300 title : "Error" ,
246301 description : error . message || "Failed to save vote" ,
0 commit comments