Files
Mav-Mobile-UI/src/hooks/useFileUpload.ts
Toba Ojo 4e5bff60ae - bumped to 1.0.24
- removed stray console.log
2026-01-06 12:37:07 +00:00

30 lines
844 B
TypeScript

import { useMutation } from "@tanstack/react-query";
import { CAM_BASE } from "../utils/config";
import { toast } from "sonner";
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();
};
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),
});
return { mutation };
};