mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
refactor: switch from go to bun
This commit is contained in:
57
apps/client/app/hooks/useCreateVolume.ts
Normal file
57
apps/client/app/hooks/useCreateVolume.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { type } from "arktype";
|
||||
import { slugify } from "~/lib/utils";
|
||||
|
||||
const createVolume = async (variables: { name: string }) => {
|
||||
const cleanName = slugify(variables.name);
|
||||
if (!cleanName) {
|
||||
throw new Error("Invalid volume name");
|
||||
}
|
||||
const response = await fetch("/api/volumes", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ name: cleanName }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let errorText = "Network response was not ok";
|
||||
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
if (errorData.error && typeof errorData.error === "string") {
|
||||
errorText = errorData.error;
|
||||
} else {
|
||||
errorText = JSON.stringify(errorData);
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
throw new Error(errorText);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
};
|
||||
|
||||
const createVolumeSchema = type({
|
||||
name: "string",
|
||||
});
|
||||
|
||||
export const useCreateVolume = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createVolume,
|
||||
onSuccess: (data) => {
|
||||
const result = createVolumeSchema(data);
|
||||
|
||||
if (result instanceof type.errors) {
|
||||
console.error("Create volume response validation failed:", result);
|
||||
return { message: "Invalid data format" };
|
||||
}
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ["volumes"] });
|
||||
return result;
|
||||
},
|
||||
});
|
||||
};
|
||||
38
apps/client/app/hooks/useDeleteVolume.ts
Normal file
38
apps/client/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