From 77661792d4dc67b048e78e8a710f2cfa600824e7 Mon Sep 17 00:00:00 2001 From: rngsurrounded Date: Mon, 3 Mar 2025 06:51:47 +0900 Subject: [PATCH 1/5] fix: status change --- .../components/FileSolution/index.tsx | 19 +---- .../modules/TaskSolution/index.tsx | 69 ++++++++++--------- 2 files changed, 39 insertions(+), 49 deletions(-) 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} />
); From 6c4d5e34b74ffd2a6363117467a72c67d32a8956 Mon Sep 17 00:00:00 2001 From: rngsurrounded Date: Mon, 3 Mar 2025 07:04:35 +0900 Subject: [PATCH 2/5] minor fixes --- .../src/pages/CompetitionSession/index.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/services/frontend/src/pages/CompetitionSession/index.tsx b/services/frontend/src/pages/CompetitionSession/index.tsx index 09fc40c..c2141d2 100644 --- a/services/frontend/src/pages/CompetitionSession/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/index.tsx @@ -4,6 +4,7 @@ import CompetitionHeader from "./components/CompetitionHeader"; import TaskContent from "./components/TaskContent"; import TaskSolution from "./modules/TaskSolution"; import { getCompetitionTasks, submitTaskSolution } from "@/shared/api/session"; +import { getCompetition } from "@/shared/api/competitions"; import { Loader2 } from "lucide-react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { TaskType } from "@/shared/types/task"; @@ -15,6 +16,12 @@ const CompetitionSession = () => { const competitionId = id || ""; const queryClient = useQueryClient(); + const competitionQuery = useQuery({ + queryKey: ["competition", competitionId], + queryFn: () => getCompetition(competitionId), + enabled: !!competitionId, + }); + const tasksQuery = useQuery({ queryKey: ["competitionTasks", competitionId], queryFn: () => getCompetitionTasks(competitionId), @@ -46,9 +53,12 @@ const CompetitionSession = () => { } }); + const competition = competitionQuery.data; const tasks = tasksQuery.data || []; - const isLoading = tasksQuery.isLoading; - const error = tasksQuery.error ? "Не удалось загрузить задания. Пожалуйста, попробуйте позже." : null; + const isLoading = tasksQuery.isLoading || competitionQuery.isLoading; + const error = tasksQuery.error || competitionQuery.error + ? "Не удалось загрузить данные. Пожалуйста, попробуйте позже." + : null; const currentTask = tasks.find((t) => t.id === taskId) || null; @@ -77,10 +87,12 @@ const CompetitionSession = () => { submitMutation.mutate(); }; + const competitionTitle = competition?.title || "Загрузка соревнования..."; + return (
@@ -106,6 +118,7 @@ const CompetitionSession = () => { selectedFile={selectedFile} setSelectedFile={setSelectedFile} onSubmit={handleSubmit} + isSubmitting={submitMutation.isPending} />
) : ( From 5cad051e7dd4950677b0d9c04dec3c6208cb2053 Mon Sep 17 00:00:00 2001 From: rngsurrounded Date: Mon, 3 Mar 2025 07:06:50 +0900 Subject: [PATCH 3/5] test --- .../modules/TaskSolution/index.tsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index 697b955..3daea18 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -41,22 +41,17 @@ const TaskSolution: React.FC = ({ }); const solutionHistory = solutionsQuery.data || []; - - const getLatestSolution = () => { - return solutionHistory.length > 0 ? solutionHistory[solutionHistory.length - 1] : null; - }; useEffect(() => { if (solutionHistory.length > 0 && !currentSolution) { - setCurrentSolution(getLatestSolution()); + setCurrentSolution(solutionHistory[0]); } }, [solutionHistory, currentSolution]); useEffect(() => { - const latestSolution = getLatestSolution(); - if (solutionHistory.length > 0 && currentSolution && latestSolution && - latestSolution.id !== currentSolution.id) { - setCurrentSolution(latestSolution); + if (solutionHistory.length > 0 && currentSolution && + solutionHistory[0].id !== currentSolution.id) { + setCurrentSolution(solutionHistory[0]); } }, [solutionHistory, currentSolution]); @@ -95,7 +90,7 @@ const TaskSolution: React.FC = ({ const handleSolutionSelect = (solution: Solution) => { setCurrentSolution(solution); - setIsHistoryOpen(false); + setIsHistoryOpen(false); }; const handleClearExistingFile = () => { From 399d2614b240e817b64cb9c21f188e7817889ab9 Mon Sep 17 00:00:00 2001 From: rngsurrounded Date: Mon, 3 Mar 2025 07:14:03 +0900 Subject: [PATCH 4/5] fix --- .../modules/TaskSolution/index.tsx | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index 3daea18..e39ffc5 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -17,7 +17,6 @@ interface TaskSolutionProps { selectedFile: File | null; setSelectedFile: (file: File | null) => void; onSubmit: () => void; - isSubmitting?: boolean; } const TaskSolution: React.FC = ({ @@ -33,6 +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 solutionsQuery = useQuery({ queryKey: ['solutionHistory', competitionId, task.id], @@ -44,21 +44,32 @@ const TaskSolution: React.FC = ({ useEffect(() => { if (solutionHistory.length > 0 && !currentSolution) { - setCurrentSolution(solutionHistory[0]); + setCurrentSolution(solutionHistory[solutionHistory.length - 1]); } }, [solutionHistory, currentSolution]); useEffect(() => { if (solutionHistory.length > 0 && currentSolution && solutionHistory[0].id !== currentSolution.id) { - setCurrentSolution(solutionHistory[0]); + setCurrentSolution(solutionHistory[solutionHistory.length - 1]); } }, [solutionHistory, currentSolution]); useEffect(() => { - setCurrentSolution(null); - setSelectedSolutionUrl(null); - }, [task.id]); + if (previousTaskIdRef.current !== task.id) { + setCurrentSolution(null); + setSelectedSolutionUrl(null); + + setAnswer(""); + setSelectedFile(null); + + if (solutionHistory.length > 0 && !solutionsQuery.isLoading) { + setCurrentSolution(solutionHistory[solutionHistory.length - 1]); + } + + previousTaskIdRef.current = task.id; + } + }, [task.id, solutionHistory, solutionsQuery.isLoading, setAnswer, setSelectedFile]); useEffect(() => { const loadSolutionContent = async () => { @@ -89,8 +100,8 @@ const TaskSolution: React.FC = ({ }; const handleSolutionSelect = (solution: Solution) => { - setCurrentSolution(solution); - setIsHistoryOpen(false); + setCurrentSolution(solution); + setIsHistoryOpen(false); }; const handleClearExistingFile = () => { @@ -110,7 +121,7 @@ const TaskSolution: React.FC = ({ {task.type === TaskType.INPUT && ( )} @@ -121,13 +132,14 @@ const TaskSolution: React.FC = ({ fileInputRef={fileInputRef} existingFileUrl={selectedSolutionUrl} onClearExistingFile={handleClearExistingFile} + isLoading={isSubmitting} /> )} {task.type === TaskType.CODE && ( )} From 420a84e2459e0b26aa805f0555ac5a06d9dc9a4d Mon Sep 17 00:00:00 2001 From: rngsurrounded Date: Mon, 3 Mar 2025 07:26:37 +0900 Subject: [PATCH 5/5] fix --- services/frontend/src/pages/CompetitionSession/index.tsx | 1 - .../src/pages/CompetitionSession/modules/TaskSolution/index.tsx | 1 - 2 files changed, 2 deletions(-) diff --git a/services/frontend/src/pages/CompetitionSession/index.tsx b/services/frontend/src/pages/CompetitionSession/index.tsx index c2141d2..4d0c5f8 100644 --- a/services/frontend/src/pages/CompetitionSession/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/index.tsx @@ -118,7 +118,6 @@ const CompetitionSession = () => { selectedFile={selectedFile} setSelectedFile={setSelectedFile} onSubmit={handleSubmit} - isSubmitting={submitMutation.isPending} />
) : ( diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index e39ffc5..20fdda1 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -132,7 +132,6 @@ const TaskSolution: React.FC = ({ fileInputRef={fileInputRef} existingFileUrl={selectedSolutionUrl} onClearExistingFile={handleClearExistingFile} - isLoading={isSubmitting} /> )}