Update channel when updating tablo

This commit is contained in:
Arthur Belleville 2025-07-05 21:04:43 +02:00
parent 904482bced
commit f07a62bfb4
No known key found for this signature in database
2 changed files with 74 additions and 25 deletions

View file

@ -34,26 +34,20 @@ tabloRouter.post("/create", async (c) => {
const tablo = data as Omit<TabloInsert, "owner_id">;
const { error } = await supabase.from("tablos").insert({
...tablo,
owner_id: user.id,
});
const { data: insertedTablo, error } = await supabase
.from("tablos")
.insert({
...tablo,
owner_id: user.id,
})
.select()
.single();
if (error) {
return c.json({ error: error.message }, 500);
}
// TODO: find a better way to get the tablo id
const { data: tabloData, error: tabloError } = await supabase
.from("tablos")
.select("id")
.eq("owner_id", user.id)
.eq("name", tablo.name)
.single();
if (tabloError) {
return c.json({ error: tabloError.message }, 500);
}
const tabloData = insertedTablo as Tables<"tablos">;
const streamServerClient = c.get("streamServerClient");
const channel = streamServerClient.channel("messaging", tabloData.id, {
@ -67,6 +61,34 @@ tabloRouter.post("/create", async (c) => {
return c.json({ message: "Tablo created successfully" });
});
tabloRouter.patch("/update", async (c) => {
const user = c.get("user");
const supabase = c.get("supabase");
const streamServerClient = c.get("streamServerClient");
const data = await c.req.json();
const { id, ...tablo } = data;
const { error } = await supabase
.from("tablos")
.update(tablo)
.eq("id", id)
// TODO: this condition will need to be modified in the future
.eq("owner_id", user.id);
if (error) {
return c.json({ error: error.message }, 500);
}
const channel = streamServerClient.channel("messaging", id);
await channel.update({
// @ts-ignore
name: tablo.name,
});
return c.json({ message: "Tablo updated successfully" });
});
tabloRouter.post("/invite", async (c) => {
const sender = c.get("user");
const supabase = c.get("supabase");

View file

@ -70,11 +70,16 @@ export const useCreateTablo = () => {
},
onError: (error) => {
console.error(error);
toast.add({
title: "Échec de la création du tablo",
description: "Veuillez réessayer",
type: "error",
});
toast.add(
{
title: "Échec de la création du tablo",
description: "Veuillez réessayer",
type: "error",
},
{
timeout: 5000,
}
);
},
});
};
@ -82,19 +87,41 @@ export const useCreateTablo = () => {
// Update tablo
export const useUpdateTablo = () => {
const queryClient = useQueryClient();
const { session } = useSession();
return useMutation({
mutationFn: async ({ id, ...tablo }: TabloUpdate & { id: string }) => {
const { error } = await supabase
.from("tablos")
.update(tablo)
.eq("id", id);
if (error) throw error;
const { data } = await api.patch(
`/api/v1/tablos/update`,
{
id,
...tablo,
},
{
headers: {
Authorization: `Bearer ${session?.access_token}`,
},
}
);
return data;
},
onSuccess: (_, { id }) => {
queryClient.invalidateQueries({ queryKey: ["tablos"] });
queryClient.invalidateQueries({ queryKey: ["tablos", id] });
},
onError: (error) => {
console.error(error);
toast.add(
{
title: "Échec de la création du tablo",
description: "Veuillez réessayer",
type: "error",
},
{
timeout: 5000,
}
);
},
});
};