Skip to content

Commit 37f2092

Browse files
committed
fix(voting): update voting
1 parent 3824ba3 commit 37f2092

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/hooks/queries/useOfflineVotingQuery.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { supabase } from "@/integrations/supabase/client";
33
import { useToast } from "@/components/ui/use-toast";
44
import { offlineStorage } from "@/lib/offlineStorage";
55
import { useOnlineStatus, useOfflineQueue } from "@/hooks/useOffline";
6-
import { voteQueries } from "@/services/queries";
6+
import { voteQueries, setQueries, FestivalSet } from "@/services/queries";
77
import type { User } from "@supabase/supabase-js";
88

99
interface 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",

src/hooks/useOfflineVoting.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export function useOfflineVoting(user: User | null, onVoteUpdate?: () => void) {
1010
{},
1111
);
1212

13-
// Use React Query for data fetching
1413
const {
1514
data: userVotes = {},
1615
isLoading,
1716
error,
1817
} = useOfflineVotingQuery(user);
1918

20-
// Use React Query for mutations
2119
const voteMutation = useOfflineVoteMutation(user, onVoteUpdate);
2220

2321
const handleVote = useCallback(

0 commit comments

Comments
 (0)