mirror of
https://gitlab.com/megazordpobeda/DataRush.git
synced 2026-05-23 01:37:11 +00:00
Merge branch 'master' of gitlab.prodcontest.ru:team-15/project
This commit is contained in:
@@ -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 (
|
||||
<div className="flex min-h-screen flex-col">
|
||||
<CompetitionHeader
|
||||
title="Олимпиада DANO 2025. Индивидуальный этап"
|
||||
title={competitionTitle}
|
||||
tasks={tasks}
|
||||
competitionId={competitionId}
|
||||
/>
|
||||
|
||||
+1
-18
@@ -99,7 +99,7 @@ const FileSolution: React.FC<FileSolutionProps> = ({
|
||||
</a>
|
||||
)}
|
||||
|
||||
{selectedFile ? (
|
||||
{selectedFile || existingFileUrl ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="text-sm p-0 h-auto hover:bg-transparent font-hse-sans"
|
||||
@@ -107,23 +107,6 @@ const FileSolution: React.FC<FileSolutionProps> = ({
|
||||
>
|
||||
Очистить
|
||||
</Button>
|
||||
) : existingFileUrl ? (
|
||||
<div className="flex gap-3">
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="text-sm p-0 h-auto hover:bg-transparent font-hse-sans"
|
||||
onClick={handleSelectNewFile}
|
||||
>
|
||||
Выбрать другой файл
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="text-sm p-0 h-auto hover:bg-transparent font-hse-sans"
|
||||
onClick={handleClearFile}
|
||||
>
|
||||
Очистить
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -30,7 +30,9 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
|
||||
const [selectedSolutionUrl, setSelectedSolutionUrl] = useState<string | null>(null);
|
||||
const [currentSolution, setCurrentSolution] = useState<Solution | null>(null);
|
||||
const { id: competitionId } = useParams<{ id: string }>();
|
||||
const previousTaskIdRef = useRef<string | null>(null);
|
||||
|
||||
const solutionsQuery = useQuery({
|
||||
queryKey: ['solutionHistory', competitionId, task.id],
|
||||
@@ -39,18 +41,46 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
|
||||
});
|
||||
|
||||
const solutionHistory = solutionsQuery.data || [];
|
||||
const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null;
|
||||
|
||||
useEffect(() => {
|
||||
const loadLatestSolution = async () => {
|
||||
if (!latestSolution || !latestSolution.content) return;
|
||||
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) {
|
||||
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 () => {
|
||||
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 +88,20 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
|
||||
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 +110,8 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
|
||||
|
||||
return (
|
||||
<div className="md:w-[500px] flex flex-col gap-4">
|
||||
{latestSolution ? (
|
||||
<SolutionStatus solution={latestSolution} maxPoints={task.points}/>
|
||||
{currentSolution ? (
|
||||
<SolutionStatus solution={currentSolution} maxPoints={task.points}/>
|
||||
) : (
|
||||
<div className="bg-gray-100 rounded-lg p-4 text-gray-600 font-hse-sans">
|
||||
Решение еще не отправлено
|
||||
@@ -141,6 +153,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
|
||||
solutions={solutionHistory}
|
||||
maxPoints={task.points}
|
||||
onSolutionSelect={handleSolutionSelect}
|
||||
currentSolutionId={currentSolution?.id}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user