2025-12-22 15:26:34 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query";
|
2025-10-22 11:51:37 +01:00
|
|
|
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;
|
2025-10-22 16:12:49 +01:00
|
|
|
|
2025-10-22 11:51:37 +01:00
|
|
|
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 = () => {
|
2025-10-22 11:51:37 +01:00
|
|
|
const mutation = useMutation({
|
|
|
|
|
mutationFn: (file: File) => uploadFile(file),
|
|
|
|
|
mutationKey: ["uploadFile"],
|
2025-10-22 16:12:49 +01:00
|
|
|
onError: (err) => toast.error(err ? err.message : ""),
|
2025-10-29 15:04:40 +00:00
|
|
|
onSuccess: async (msg) => toast.success(msg),
|
2025-10-22 11:51:37 +01:00
|
|
|
});
|
|
|
|
|
|
2025-12-22 15:26:34 +00:00
|
|
|
return { mutation };
|
2025-10-22 11:51:37 +01:00
|
|
|
};
|