diff --git a/services/frontend/src/pages/CompetitionSession/index.tsx b/services/frontend/src/pages/CompetitionSession/index.tsx index 4d0c5f8..5c8e7c1 100644 --- a/services/frontend/src/pages/CompetitionSession/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/index.tsx @@ -44,9 +44,6 @@ const CompetitionSession = () => { queryClient.invalidateQueries({ queryKey: ['solutionHistory', competitionId, taskId] }); - - setAnswer(""); - setSelectedFile(null); }, onError: (error) => { console.error("Error submitting solution:", error); diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx index 3f181bd..83d8cda 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx @@ -22,6 +22,7 @@ const SolutionHistorySheet: React.FC = ({ onSolutionSelect, currentSolutionId }) => { + return ( diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index 20fdda1..a463cde 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -32,7 +32,7 @@ const TaskSolution: React.FC = ({ const [selectedSolutionUrl, setSelectedSolutionUrl] = useState(null); const [currentSolution, setCurrentSolution] = useState(null); const { id: competitionId } = useParams<{ id: string }>(); - const previousTaskIdRef = useRef(null); + const taskIdRef = useRef(null); const solutionsQuery = useQuery({ queryKey: ['solutionHistory', competitionId, task.id], @@ -40,37 +40,42 @@ const TaskSolution: React.FC = ({ enabled: !!(competitionId && task.id), }); + // Get the solution history - already sorted from oldest to newest const solutionHistory = solutionsQuery.data || []; - + + // Handle task changes useEffect(() => { - if (solutionHistory.length > 0 && !currentSolution) { - setCurrentSolution(solutionHistory[solutionHistory.length - 1]); - } - }, [solutionHistory, currentSolution]); - - useEffect(() => { - if (solutionHistory.length > 0 && currentSolution && - solutionHistory[0].id !== currentSolution.id) { - setCurrentSolution(solutionHistory[solutionHistory.length - 1]); - } - }, [solutionHistory, currentSolution]); - - useEffect(() => { - if (previousTaskIdRef.current !== task.id) { + // If task changed, reset everything and load the latest solution + if (taskIdRef.current !== task.id) { setCurrentSolution(null); setSelectedSolutionUrl(null); - setAnswer(""); setSelectedFile(null); + taskIdRef.current = task.id; - if (solutionHistory.length > 0 && !solutionsQuery.isLoading) { - setCurrentSolution(solutionHistory[solutionHistory.length - 1]); + // Wait for the query to complete + if (!solutionsQuery.isLoading && solutionHistory.length > 0) { + // Get the most recent solution (last in the array) + const latestSolution = solutionHistory[solutionHistory.length - 1]; + setCurrentSolution(latestSolution); } - - previousTaskIdRef.current = task.id; } }, [task.id, solutionHistory, solutionsQuery.isLoading, setAnswer, setSelectedFile]); + // Refresh current solution when the solution history changes (after a new submission) + useEffect(() => { + if (!solutionsQuery.isLoading && solutionHistory.length > 0) { + // If we don't have a current solution or there's a new submission + // (which would be the last item in the array) + if (!currentSolution || + currentSolution.id !== solutionHistory[solutionHistory.length - 1].id) { + // Set to the latest solution (last in the array) + setCurrentSolution(solutionHistory[solutionHistory.length - 1]); + } + } + }, [solutionHistory, currentSolution, solutionsQuery.isLoading]); + + // Load solution content when current solution changes useEffect(() => { const loadSolutionContent = async () => { if (!currentSolution || !currentSolution.content) return; @@ -108,6 +113,13 @@ const TaskSolution: React.FC = ({ setSelectedSolutionUrl(null); }; + const handleSubmitWrapper = () => { + onSubmit(); + setAnswer(""); + setSelectedFile(null); + setSelectedSolutionUrl(null); + }; + return (
{currentSolution ? ( @@ -143,7 +155,7 @@ const TaskSolution: React.FC = ({ )}