mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: delete volume
This commit is contained in:
38
web/app/hooks/useDeleteVolume.ts
Normal file
38
web/app/hooks/useDeleteVolume.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { type } from "arktype";
|
||||
|
||||
const volumesSchema = type({
|
||||
message: "string",
|
||||
});
|
||||
|
||||
const deleteVolume = async (variables: { name: string }) => {
|
||||
const response = await fetch(`/api/volumes/${variables.name}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const useDeleteVolume = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: deleteVolume,
|
||||
onSuccess: (data) => {
|
||||
const result = volumesSchema(data);
|
||||
|
||||
if (result instanceof type.errors) {
|
||||
console.error("Volumes data validation failed:", result);
|
||||
return { message: "Invalid data format" };
|
||||
}
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ["volumes"] });
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user