- need to find better way to fetch file urls to use for sound
This commit is contained in:
@@ -3,6 +3,10 @@ import { CAM_BASE } from "../utils/config";
|
||||
import { toast } from "sonner";
|
||||
const camBase = import.meta.env.MODE !== "development" ? CAM_BASE : CAM_BASE;
|
||||
|
||||
type UseFileUploadProps = {
|
||||
queryKey?: string[];
|
||||
};
|
||||
|
||||
const uploadFile = async (file: File) => {
|
||||
const form = new FormData();
|
||||
form.append("upload", file, file.name);
|
||||
@@ -16,26 +20,29 @@ const uploadFile = async (file: File) => {
|
||||
return response.text();
|
||||
};
|
||||
|
||||
const getUploadFiles = async () => {
|
||||
const response = await fetch(`${camBase}/upload/file-upload/3`);
|
||||
const getUploadFiles = async ({ queryKey }: { queryKey: string[] }) => {
|
||||
const [, fileName] = queryKey;
|
||||
console.log(`${camBase}/Mobile/${fileName}.mp3`);
|
||||
const response = await fetch(`${camBase}/Mobile/${fileName}`);
|
||||
if (!response.ok) {
|
||||
throw new Error("Cannot reach upload file endpoint");
|
||||
}
|
||||
return response.json();
|
||||
return response.blob();
|
||||
};
|
||||
|
||||
export const useFileUpload = () => {
|
||||
export const useFileUpload = ({ queryKey }: UseFileUploadProps) => {
|
||||
const query = useQuery({
|
||||
queryKey: ["getUploadFiles"],
|
||||
queryFn: getUploadFiles,
|
||||
queryKey: ["getUploadFiles", ...(queryKey ?? [])],
|
||||
queryFn: () => getUploadFiles({ queryKey: ["getUploadFiles", ...(queryKey ?? [])] }),
|
||||
enabled: !!queryKey,
|
||||
});
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: (file: File) => uploadFile(file),
|
||||
mutationKey: ["uploadFile"],
|
||||
onError: (err) => console.log(err),
|
||||
onError: (err) => toast.error(err ? err.message : ""),
|
||||
onSuccess: (msg) => toast.success(msg),
|
||||
});
|
||||
|
||||
return { query, mutation };
|
||||
return { query: queryKey ? query : undefined, mutation };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user