update: add queries for upload file on database and local storage
This commit is contained in:
parent
521d39f7f6
commit
38ea7a580d
|
|
@ -1,7 +1,6 @@
|
|||
import client from "@/honoClient";
|
||||
import fetchRPC from "@/utils/fetchRPC";
|
||||
import { queryOptions, useMutation, UseMutationOptions } from "@tanstack/react-query";
|
||||
import { InferRequestType } from "hono";
|
||||
import { queryOptions, UseMutationOptions } from "@tanstack/react-query";
|
||||
|
||||
type SubmitOptionResponse = {
|
||||
message: string;
|
||||
|
|
@ -123,4 +122,35 @@ export const submitValidation = async (
|
|||
|
||||
export const submitValidationMutationOptions = () => ({
|
||||
mutationFn: submitValidation,
|
||||
});
|
||||
});
|
||||
|
||||
// Function to upload a file
|
||||
const uploadFile = async (formData: FormData, assessmentId: string, questionId: string) => {
|
||||
const token = localStorage.getItem('accessToken');
|
||||
|
||||
const response = await fetch(`${import.meta.env.VITE_BACKEND_BASE_URL}/assessments/uploadFile?assessmentId=${assessmentId}&questionId=${questionId}`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error('Error Data:', errorData);
|
||||
throw new Error(errorData.message || 'Error uploading file');
|
||||
}
|
||||
|
||||
const responseData = await response.json();
|
||||
return responseData; // Return the JSON response with file URL
|
||||
};
|
||||
|
||||
// Options for the mutation
|
||||
export const uploadFileMutationOptions = (): UseMutationOptions<{ imageUrl: string }, Error, FormData> => ({
|
||||
mutationFn: (formData: FormData) => {
|
||||
const assessmentId = formData.get('assessmentId') as string;
|
||||
const questionId = formData.get('questionId') as string;
|
||||
return uploadFile(formData, assessmentId, questionId);
|
||||
},
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user