feat: right file handling

This commit is contained in:
rngsurrounded
2025-03-02 23:45:13 +09:00
parent bfe9c2db70
commit c7c4640dc5
3 changed files with 41 additions and 9 deletions
@@ -6,10 +6,12 @@ import TaskSolution from "./modules/TaskSolution";
import { getCompetitionTasks, submitTaskSolution } from "@/shared/api/session"; import { getCompetitionTasks, submitTaskSolution } from "@/shared/api/session";
import { Loader2 } from "lucide-react"; import { Loader2 } from "lucide-react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { TaskType } from "@/shared/types/task";
const CompetitionSession = () => { const CompetitionSession = () => {
const { id, taskId } = useParams<{ id: string; taskId?: string }>(); const { id, taskId } = useParams<{ id: string; taskId?: string }>();
const [answer, setAnswer] = useState(""); const [answer, setAnswer] = useState("");
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const competitionId = id || ""; const competitionId = id || "";
const queryClient = useQueryClient(); const queryClient = useQueryClient();
@@ -20,12 +22,27 @@ const CompetitionSession = () => {
}); });
const submitMutation = useMutation({ const submitMutation = useMutation({
mutationFn: () => submitTaskSolution(competitionId, taskId || "", answer), mutationFn: () => {
if (!currentTask || !competitionId) throw new Error("Missing task or competition ID");
if (currentTask.type === TaskType.FILE) {
if (!selectedFile) throw new Error("No file selected");
return submitTaskSolution(competitionId, taskId || "", selectedFile);
} else {
if (!answer.trim()) throw new Error("Answer is empty");
return submitTaskSolution(competitionId, taskId || "", answer);
}
},
onSuccess: () => { onSuccess: () => {
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: ['submissionHistory', competitionId, taskId] queryKey: ['solutionHistory', competitionId, taskId]
}); });
setAnswer(""); setAnswer("");
setSelectedFile(null);
},
onError: (error) => {
console.error("Error submitting solution:", error);
} }
}); });
@@ -45,8 +62,18 @@ const CompetitionSession = () => {
} }
const handleSubmit = () => { const handleSubmit = () => {
console.log(currentTask, competitionId, answer) if (!currentTask || !competitionId) return;
if (!currentTask || !competitionId || !answer.trim()) return;
if (currentTask.type === TaskType.FILE && !selectedFile) {
console.error("No file selected");
return;
}
if (currentTask.type !== TaskType.FILE && !answer.trim()) {
console.error("Answer is empty");
return;
}
submitMutation.mutate(); submitMutation.mutate();
}; };
@@ -77,6 +104,8 @@ const CompetitionSession = () => {
solutions={[]} solutions={[]}
answer={answer} answer={answer}
setAnswer={setAnswer} setAnswer={setAnswer}
selectedFile={selectedFile}
setSelectedFile={setSelectedFile}
onSubmit={handleSubmit} onSubmit={handleSubmit}
/> />
</div> </div>
@@ -15,17 +15,20 @@ interface TaskSolutionProps {
solutions: Solution[]; solutions: Solution[];
answer: string; answer: string;
setAnswer: (value: string) => void; setAnswer: (value: string) => void;
selectedFile: File | null;
setSelectedFile: (file: File | null) => void;
onSubmit: () => void; onSubmit: () => void;
} }
const TaskSolution: React.FC<TaskSolutionProps> = ({ const TaskSolution: React.FC<TaskSolutionProps> = ({
task, task,
solutions = [], solutions = [],
answer, answer,
setAnswer, setAnswer,
onSubmit, selectedFile,
setSelectedFile,
onSubmit,
}) => { }) => {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const [isHistoryOpen, setIsHistoryOpen] = useState(false); const [isHistoryOpen, setIsHistoryOpen] = useState(false);
const { id: competitionId } = useParams<{ id: string }>(); const { id: competitionId } = useParams<{ id: string }>();
+1 -1
View File
@@ -27,7 +27,7 @@ export const submitTaskSolution = async (
}); });
} else { } else {
const formData = new FormData(); const formData = new FormData();
formData.append('file', solution); formData.append('content', solution);
return await userFetch(endpoint, { return await userFetch(endpoint, {
method: 'POST', method: 'POST',