feat: access to competition is blocked if it is ended

This commit is contained in:
rngsurrounded
2025-03-03 23:12:40 +09:00
parent 63f3867bc3
commit 70acce4b9f
3 changed files with 54 additions and 12 deletions
@@ -1,6 +1,6 @@
import { useParams, Link, useNavigate } from "react-router-dom";
import { Button } from "@/components/ui/button";
import { ArrowLeft, Clock, Trophy, BookOpen } from "lucide-react";
import { ArrowLeft, Clock, Trophy, BookOpen, BarChart2 } from "lucide-react";
import ReactMarkdown from "react-markdown";
import { useQuery, useMutation } from "@tanstack/react-query";
import { getCompetition, startCompetition } from "@/shared/api/competitions";
@@ -39,6 +39,7 @@ const CompetitionPage = () => {
console.error("Failed to start competition:", error);
}
});
const formatDate = (date?: Date | string) => {
if (!date) return "";
@@ -56,6 +57,20 @@ const CompetitionPage = () => {
const handleStart = () => {
startMutation.mutate();
};
const handleViewResults = () => {
navigate(`/competition/${competitionId}/results`);
};
// Check if competition has ended
const isCompetitionEnded = () => {
if (!competitionQuery.data?.end_date) return false;
const endDate = new Date(competitionQuery.data.end_date);
const now = new Date();
return now > endDate;
};
if (competitionQuery.isLoading) {
return <Loading />;
@@ -66,6 +81,7 @@ const CompetitionPage = () => {
}
const competition = competitionQuery.data;
const competitionEnded = isCompetitionEnded();
return (
<div className="flex flex-col gap-4">
@@ -103,6 +119,12 @@ const CompetitionPage = () => {
</>
)}
</div>
{competitionEnded && competition.type === CompetitionType.COMPETITIVE && (
<div className="bg-red-100 text-red-700 px-3 py-1 rounded-full text-sm font-medium">
Завершено
</div>
)}
</div>
<h1 className="text-[34px] leading-11 font-semibold text-balance">
@@ -133,13 +155,24 @@ const CompetitionPage = () => {
</div>
<div className="w-full *:w-full md:w-96">
<Button
size={"lg"}
onClick={handleStart}
disabled={startMutation.isPending}
>
{startMutation.isPending ? "Загрузка..." : "Приступить к выполнению"}
</Button>
{competitionEnded && competition.type === CompetitionType.COMPETITIVE ? (
<Button
size={"lg"}
onClick={handleViewResults}
className="bg-indigo-600 hover:bg-indigo-700"
>
<BarChart2 size={18} className="mr-2" />
Смотреть результаты
</Button>
) : (
<Button
size={"lg"}
onClick={handleStart}
disabled={startMutation.isPending}
>
{startMutation.isPending ? "Загрузка..." : "Приступить к выполнению"}
</Button>
)}
</div>
</div>
</div>