xtablo-source/xtablo-expo/hooks/etapes.ts
Arthur Belleville 66d9ab9ad8
fix(expo): address review findings - cache invalidation, date handling, cleanup
- Add tasks cache invalidation to etape create/update hooks
- Fix timezone-unsafe overdue date comparison in TaskRow
- Add due date picker to EtapeSheet (was missing from spec)
- Use initialized ref to prevent form state overwrite on refetch
- Remove unused deleteTask import from TaskList

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 14:07:42 +02:00

111 lines
3.2 KiB
TypeScript

import { supabase } from "@/lib/supabase";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Etape } from "@/types/tasks.types";
import { Alert } from "react-native";
export const useTabloEtapes = (tabloId: string | undefined) => {
return useQuery({
queryKey: ["tablo-etapes", tabloId],
queryFn: async () => {
const { data, error } = await supabase
.from("tasks")
.select("*")
.eq("tablo_id", tabloId!)
.eq("is_parent", true)
.is("deleted_at", null)
.order("position", { ascending: true });
if (error) throw error;
return data as Etape[];
},
enabled: !!tabloId,
});
};
export const useCreateEtape = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ tabloId, title, description, position, due_date }: {
tabloId: string;
title: string;
description?: string | null;
position?: number;
due_date?: string | null;
}) => {
const { data, error } = await supabase
.from("tasks")
.insert({
tablo_id: tabloId,
title,
description: description ?? null,
position: position ?? 0,
is_parent: true,
status: "todo",
due_date: due_date ?? null,
})
.select()
.single();
if (error) throw error;
return data;
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
},
onError: () => {
Alert.alert("Erreur", "Impossible de créer l'étape.");
},
});
};
export const useUpdateEtape = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ id, tabloId, ...updates }: {
id: string;
tabloId: string;
title?: string;
description?: string | null;
position?: number;
due_date?: string | null;
}) => {
const { data, error } = await supabase
.from("tasks")
.update(updates)
.eq("id", id)
.select()
.single();
if (error) throw error;
return data;
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
},
onError: () => {
Alert.alert("Erreur", "Impossible de modifier l'étape.");
},
});
};
export const useDeleteEtape = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ id, tabloId }: { id: string; tabloId: string }) => {
const { error } = await supabase.from("tasks").delete().eq("id", id);
if (error) throw error;
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ["tablo-etapes", variables.tabloId] });
queryClient.invalidateQueries({ queryKey: ["tasks", "tablo", variables.tabloId] });
},
onError: () => {
Alert.alert("Erreur", "Impossible de supprimer l'étape.");
},
});
};