- added new sound files
- new functionality to upload files - need to get and locatate uploaded files
This commit is contained in:
41
src/hooks/useFileUpload.ts
Normal file
41
src/hooks/useFileUpload.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { useMutation, useQuery } 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();
|
||||
};
|
||||
|
||||
const getUploadFiles = async () => {
|
||||
const response = await fetch(`${camBase}/upload/file-upload/3`);
|
||||
if (!response.ok) {
|
||||
throw new Error("Cannot reach upload file endpoint");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const useFileUpload = () => {
|
||||
const query = useQuery({
|
||||
queryKey: ["getUploadFiles"],
|
||||
queryFn: getUploadFiles,
|
||||
});
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: (file: File) => uploadFile(file),
|
||||
mutationKey: ["uploadFile"],
|
||||
onError: (err) => console.log(err),
|
||||
onSuccess: (msg) => toast.success(msg),
|
||||
});
|
||||
|
||||
return { query, mutation };
|
||||
};
|
||||
Reference in New Issue
Block a user