diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx index 8f97216..b7d351b 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/FileSolution/index.tsx @@ -99,7 +99,7 @@ const FileSolution: React.FC = ({ )} - {selectedFile ? ( + {selectedFile || existingFileUrl ? ( - ) : existingFileUrl ? ( -
- - -
) : null} diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index fb8324b..697b955 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -17,6 +17,7 @@ interface TaskSolutionProps { selectedFile: File | null; setSelectedFile: (file: File | null) => void; onSubmit: () => void; + isSubmitting?: boolean; } const TaskSolution: React.FC = ({ @@ -30,6 +31,7 @@ const TaskSolution: React.FC = ({ const fileInputRef = useRef(null); const [isHistoryOpen, setIsHistoryOpen] = useState(false); const [selectedSolutionUrl, setSelectedSolutionUrl] = useState(null); + const [currentSolution, setCurrentSolution] = useState(null); const { id: competitionId } = useParams<{ id: string }>(); const solutionsQuery = useQuery({ @@ -39,18 +41,40 @@ const TaskSolution: React.FC = ({ }); const solutionHistory = solutionsQuery.data || []; - const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null; + + const getLatestSolution = () => { + return solutionHistory.length > 0 ? solutionHistory[solutionHistory.length - 1] : null; + }; useEffect(() => { - const loadLatestSolution = async () => { - if (!latestSolution || !latestSolution.content) return; + if (solutionHistory.length > 0 && !currentSolution) { + setCurrentSolution(getLatestSolution()); + } + }, [solutionHistory, currentSolution]); + + useEffect(() => { + const latestSolution = getLatestSolution(); + if (solutionHistory.length > 0 && currentSolution && latestSolution && + latestSolution.id !== currentSolution.id) { + setCurrentSolution(latestSolution); + } + }, [solutionHistory, currentSolution]); + + useEffect(() => { + setCurrentSolution(null); + setSelectedSolutionUrl(null); + }, [task.id]); + + useEffect(() => { + const loadSolutionContent = async () => { + if (!currentSolution || !currentSolution.content) return; try { if (task.type === TaskType.FILE) { setSelectedFile(null); - setSelectedSolutionUrl(latestSolution.content); + setSelectedSolutionUrl(currentSolution.content); } 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}`); } @@ -58,38 +82,20 @@ const TaskSolution: React.FC = ({ setAnswer(text); } } catch (error) { - console.error('Error loading latest solution content:', error); - } finally { + console.error('Error loading solution content:', error); } }; - if (latestSolution && !solutionsQuery.isLoading && !solutionsQuery.isError) { - loadLatestSolution(); - } - }, [latestSolution, task.id, task.type, setAnswer, setSelectedFile]); + loadSolutionContent(); + }, [currentSolution, 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) { - 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 handleSolutionSelect = (solution: Solution) => { + setCurrentSolution(solution); + setIsHistoryOpen(false); }; const handleClearExistingFile = () => { @@ -98,8 +104,8 @@ const TaskSolution: React.FC = ({ return (
- {latestSolution ? ( - + {currentSolution ? ( + ) : (
Решение еще не отправлено @@ -141,6 +147,7 @@ const TaskSolution: React.FC = ({ solutions={solutionHistory} maxPoints={task.points} onSolutionSelect={handleSolutionSelect} + currentSolutionId={currentSolution?.id} />
);