minor fixes

This commit is contained in:
rngsurrounded
2025-03-03 06:35:22 +09:00
parent 446e68c979
commit 67a3cfe521
@@ -30,8 +30,6 @@ 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({
@@ -41,33 +39,18 @@ 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(() => {
if (solutionHistory.length > 0 && !currentSolution) { const loadLatestSolution = async () => {
setCurrentSolution(solutionHistory[0]); if (!latestSolution || !latestSolution.content) return;
}
}, [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(currentSolution.content); setSelectedSolutionUrl(latestSolution.content);
} else { } else {
const response = await fetch(currentSolution.content); const response = await fetch(latestSolution.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}`);
} }
@@ -75,47 +58,48 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
setAnswer(text); setAnswer(text);
} }
} catch (error) { } catch (error) {
console.error('Error loading solution content:', error); console.error('Error loading latest solution content:', error);
} finally { } finally {
setIsLoading(false);
} }
}; };
loadSolutionContent(); if (latestSolution && !solutionsQuery.isLoading && !solutionsQuery.isError) {
}, [currentSolution, task.type, setAnswer, setSelectedFile]); loadLatestSolution();
}
}, [latestSolution, task.id, task.type, setAnswer, setSelectedFile]);
const handleOpenHistory = () => { const handleOpenHistory = () => {
setIsHistoryOpen(true); setIsHistoryOpen(true);
}; };
const handleSolutionSelect = (solution: Solution) => { const handleSolutionSelect = async (solution: Solution) => {
setCurrentSolution(solution); if (!solution.content) return;
setIsHistoryOpen(false);
try {
if (task.type === TaskType.FILE) {
setSelectedFile(null);
setSelectedSolutionUrl(solution.content);
} else {
const response = await fetch(solution.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 solution content:', error);
}
}; };
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">
{currentSolution ? ( {latestSolution ? (
<SolutionStatus solution={currentSolution} maxPoints={task.points}/> <SolutionStatus solution={latestSolution} 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">
Решение еще не отправлено Решение еще не отправлено
@@ -136,7 +120,6 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
fileInputRef={fileInputRef} fileInputRef={fileInputRef}
existingFileUrl={selectedSolutionUrl} existingFileUrl={selectedSolutionUrl}
onClearExistingFile={handleClearExistingFile} onClearExistingFile={handleClearExistingFile}
isLoading={isLoading}
/> />
)} )}
@@ -148,7 +131,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
)} )}
<ActionButtons <ActionButtons
onSubmit={handleSubmit} onSubmit={onSubmit}
onHistoryClick={handleOpenHistory} onHistoryClick={handleOpenHistory}
/> />
@@ -158,7 +141,6 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
solutions={solutionHistory} solutions={solutionHistory}
maxPoints={task.points} maxPoints={task.points}
onSolutionSelect={handleSolutionSelect} onSolutionSelect={handleSolutionSelect}
currentSolutionId={currentSolution?.id}
/> />
</div> </div>
); );