feat(expo): add etape CRUD hooks

This commit is contained in:
Arthur Belleville 2026-04-15 09:44:46 +02:00
parent 1d38602465
commit d6d7892684
No known key found for this signature in database

109
xtablo-expo/hooks/etapes.ts Normal file
View file

@ -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.");
},
});
};