From d6d7892684877b734ff9be21cfb8a5ddb91718f7 Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Wed, 15 Apr 2026 09:44:46 +0200 Subject: [PATCH] feat(expo): add etape CRUD hooks --- xtablo-expo/hooks/etapes.ts | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 xtablo-expo/hooks/etapes.ts diff --git a/xtablo-expo/hooks/etapes.ts b/xtablo-expo/hooks/etapes.ts new file mode 100644 index 0000000..0d02fa2 --- /dev/null +++ b/xtablo-expo/hooks/etapes.ts @@ -0,0 +1,109 @@ +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] }); + }, + 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] }); + }, + 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."); + }, + }); +};