fix: submission update logic

This commit is contained in:
rngsurrounded
2025-03-03 06:26:49 +09:00
parent 0f088c0145
commit a153940c4a
2 changed files with 63 additions and 42 deletions
@@ -1,9 +1,9 @@
import React from 'react'; import React from 'react';
import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { X } from "lucide-react"; import { X, Check } from "lucide-react";
import SolutionStatus from '../SolutionStatus'; import SolutionStatus from '../SolutionStatus';
import { Solution, TaskType } from '@/shared/types/task'; import { Solution } from '@/shared/types/task';
interface SolutionHistorySheetProps { interface SolutionHistorySheetProps {
isOpen: boolean; isOpen: boolean;
@@ -11,6 +11,7 @@ interface SolutionHistorySheetProps {
solutions: Solution[]; solutions: Solution[];
maxPoints: number; maxPoints: number;
onSolutionSelect: (solution: Solution) => void; onSolutionSelect: (solution: Solution) => void;
currentSolutionId?: string | null;
} }
const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({ const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({
@@ -18,7 +19,8 @@ const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({
onOpenChange, onOpenChange,
solutions, solutions,
maxPoints, maxPoints,
onSolutionSelect onSolutionSelect,
currentSolutionId
}) => { }) => {
return ( return (
<Sheet open={isOpen} onOpenChange={onOpenChange}> <Sheet open={isOpen} onOpenChange={onOpenChange}>
@@ -36,15 +38,17 @@ const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({
<div className="flex flex-col mt-3 space-y-2.5 overflow-y-auto max-h-[calc(100vh-80px)] px-4 pb-4"> <div className="flex flex-col mt-3 space-y-2.5 overflow-y-auto max-h-[calc(100vh-80px)] px-4 pb-4">
{solutions.length > 0 ? ( {solutions.length > 0 ? (
solutions.map((solution, index) => ( solutions.map((solution) => (
<div <div
key={solution.id || index} key={solution.id}
className="w-full cursor-pointer transition-transform hover:scale-[1.01]" className={`w-full cursor-pointer relative ${solution.id === currentSolutionId ? 'ring-2 ring-blue-500 rounded-lg' : ''}`}
onClick={() => { onClick={() => onSolutionSelect(solution)}
onSolutionSelect(solution);
onOpenChange(false);
}}
> >
{solution.id === currentSolutionId && (
<div className="absolute top-2 right-2 z-10 bg-blue-500 text-white rounded-full p-1">
<Check size={14} />
</div>
)}
<SolutionStatus solution={solution} maxPoints={maxPoints} /> <SolutionStatus solution={solution} maxPoints={maxPoints} />
</div> </div>
)) ))
@@ -30,6 +30,8 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
const fileInputRef = useRef<HTMLInputElement>(null); const fileInputRef = useRef<HTMLInputElement>(null);
const [isHistoryOpen, setIsHistoryOpen] = useState(false); const [isHistoryOpen, setIsHistoryOpen] = useState(false);
const [selectedSolutionUrl, setSelectedSolutionUrl] = useState<string | null>(null); const [selectedSolutionUrl, setSelectedSolutionUrl] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [currentSolution, setCurrentSolution] = useState<Solution | null>(null);
const { id: competitionId } = useParams<{ id: string }>(); const { id: competitionId } = useParams<{ id: string }>();
const solutionsQuery = useQuery({ const solutionsQuery = useQuery({
@@ -39,48 +41,33 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
}); });
const solutionHistory = solutionsQuery.data || []; const solutionHistory = solutionsQuery.data || [];
const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null;
// Set the current solution to the latest one when the task changes or new solutions arrive
useEffect(() => { useEffect(() => {
const loadLatestSolution = async () => { if (solutionHistory.length > 0 && !currentSolution) {
if (!latestSolution || !latestSolution.content) return; setCurrentSolution(solutionHistory[0]);
}
}, [solutionHistory, currentSolution, task.id]);
// Reset current solution when task changes
useEffect(() => {
setCurrentSolution(null);
setSelectedSolutionUrl(null);
setAnswer(""); // Reset answer when task changes
}, [task.id, setAnswer]);
// Load the current solution content when it changes
useEffect(() => {
const loadSolutionContent = async () => {
if (!currentSolution || !currentSolution.content) return;
setIsLoading(true);
try { try {
if (task.type === TaskType.FILE) { if (task.type === TaskType.FILE) {
setSelectedFile(null); setSelectedFile(null);
setSelectedSolutionUrl(latestSolution.content); setSelectedSolutionUrl(currentSolution.content);
} else { } else {
const response = await fetch(latestSolution.content); const response = await fetch(currentSolution.content);
if (!response.ok) {
throw new Error(`Failed to fetch solution content: ${response.status}`);
}
const text = await response.text();
setAnswer(text);
}
} catch (error) {
console.error('Error loading latest solution content:', error);
} finally {
}
};
if (latestSolution && !solutionsQuery.isLoading && !solutionsQuery.isError) {
loadLatestSolution();
}
}, [latestSolution, task.id, task.type, setAnswer, setSelectedFile]);
const handleOpenHistory = () => {
setIsHistoryOpen(true);
};
const handleSolutionSelect = async (solution: Solution) => {
if (!solution.content) return;
try {
if (task.type === TaskType.FILE) {
setSelectedFile(null);
setSelectedSolutionUrl(solution.content);
} else {
const response = await fetch(solution.content);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch solution content: ${response.status}`); throw new Error(`Failed to fetch solution content: ${response.status}`);
} }
@@ -89,17 +76,46 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
} }
} catch (error) { } catch (error) {
console.error('Error loading solution content:', error); console.error('Error loading solution content:', error);
} finally {
setIsLoading(false);
} }
}; };
loadSolutionContent();
}, [currentSolution, task.type, setAnswer, setSelectedFile]);
const handleOpenHistory = () => {
setIsHistoryOpen(true);
};
const handleSolutionSelect = (solution: Solution) => {
setCurrentSolution(solution);
setIsHistoryOpen(false);
};
const handleClearExistingFile = () => { const handleClearExistingFile = () => {
setSelectedSolutionUrl(null); setSelectedSolutionUrl(null);
}; };
// Handle successful submission
const handleSubmitSuccess = (newSolution: Solution) => {
// Update the current solution to the newly submitted one
setCurrentSolution(newSolution);
// Reset form
setAnswer("");
setSelectedFile(null);
setSelectedSolutionUrl(null);
};
const handleSubmit = () => {
onSubmit();
};
return ( return (
<div className="md:w-[500px] flex flex-col gap-4"> <div className="md:w-[500px] flex flex-col gap-4">
{latestSolution ? ( {currentSolution ? (
<SolutionStatus solution={latestSolution} maxPoints={task.points}/> <SolutionStatus solution={currentSolution} maxPoints={task.points}/>
) : ( ) : (
<div className="bg-gray-100 rounded-lg p-4 text-gray-600 font-hse-sans"> <div className="bg-gray-100 rounded-lg p-4 text-gray-600 font-hse-sans">
Решение еще не отправлено Решение еще не отправлено
@@ -120,7 +136,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
fileInputRef={fileInputRef} fileInputRef={fileInputRef}
existingFileUrl={selectedSolutionUrl} existingFileUrl={selectedSolutionUrl}
onClearExistingFile={handleClearExistingFile} onClearExistingFile={handleClearExistingFile}
isLoading={isInitialLoading} isLoading={isLoading}
/> />
)} )}
@@ -132,7 +148,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
)} )}
<ActionButtons <ActionButtons
onSubmit={onSubmit} onSubmit={handleSubmit}
onHistoryClick={handleOpenHistory} onHistoryClick={handleOpenHistory}
/> />
@@ -142,6 +158,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
solutions={solutionHistory} solutions={solutionHistory}
maxPoints={task.points} maxPoints={task.points}
onSolutionSelect={handleSolutionSelect} onSolutionSelect={handleSolutionSelect}
currentSolutionId={currentSolution?.id}
/> />
</div> </div>
); );