Files
Mav-Mobile-UI/src/hooks/useFileUpload.ts

30 lines
844 B
TypeScript
Raw Normal View History

2025-12-22 15:26:34 +00:00
import { useMutation } from "@tanstack/react-query";
import { CAM_BASE } from "../utils/config";
import { toast } from "sonner";
2025-12-22 15:26:34 +00:00
const camBase = import.meta.env.MODE !== "development" ? CAM_BASE : CAM_BASE;
const uploadFile = async (file: File) => {
const form = new FormData();
form.append("upload", file, file.name);
const response = await fetch(`${camBase}/upload/file-upload/3`, {
method: "POST",
body: form,
});
if (!response.ok) {
throw new Error("Cannot reach upload file endpoint");
}
return response.text();
};
2025-12-22 15:26:34 +00:00
export const useFileUpload = () => {
const mutation = useMutation({
mutationFn: (file: File) => uploadFile(file),
mutationKey: ["uploadFile"],
onError: (err) => toast.error(err ? err.message : ""),
onSuccess: async (msg) => toast.success(msg),
});
2025-12-22 15:26:34 +00:00
return { mutation };
};