Skip to content

Commit

Permalink
chore: solved cards
Browse files Browse the repository at this point in the history
  • Loading branch information
ThEditor committed Sep 17, 2024
1 parent 083d07a commit ae1a4f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/app/challenges/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,36 @@ function getTypesFromMask(mask: number) {
export default function ChallengesPage() {
const [type, setType] = useState("all");
const [challenges, setChallenges] = useState<ChallengeData[]>([]);
const [solved, setSolved] = useState<string[]>([]);
const [loading, setLoading] = useState(true);
const { toast } = useToast();

const checkSolved = async () => {
await axios
.get(`${BACKEND_URL}/ctf/completed`, {
headers: {
Authorization: `Bearer ${window.localStorage.getItem("token")}`,
},
})
.then((res) => {
const data = res.data as {
id: string;
}[];
const all = [];
for (const item of data) {
all.push(item.id);
}
setSolved(all);
})
.catch(() => {
toast({
title: "Error",
description: "Failed to check if challenge is solved",
duration: 5000,
});
});
};

useEffect(() => {
if (!window.localStorage.getItem("token")) {
window.location.href = "/signin";
Expand All @@ -57,7 +84,7 @@ export default function ChallengesPage() {
Authorization: `Bearer ${window.localStorage.getItem("token")}`,
},
})
.then((res) => {
.then(async (res) => {
const data = res.data as {
id: string;
name: string;
Expand All @@ -76,6 +103,7 @@ export default function ChallengesPage() {
} as unknown as ChallengeData;
});
setChallenges(challenges);
await checkSolved();
setLoading(false);
})
.catch(() => {
Expand Down Expand Up @@ -139,6 +167,7 @@ export default function ChallengesPage() {
types={challenge.types}
// description={challenge.description}
points={challenge.points}
solved={solved.includes(challenge.id)}
// solves={challenge.solves}
onClick={() => setChallenge(challenge.id)}
/>
Expand Down
8 changes: 7 additions & 1 deletion src/components/challengeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import Text from "~/components/text";
import { BsNut } from "react-icons/bs";

import React, { type HTMLAttributes } from "react";
import { cn } from "~/lib/utils";

export interface ChallengeItem {
title: string;
types: string[];
// description: string;
points: number;
solved?: boolean;
// solves: number;
}

Expand All @@ -22,12 +24,16 @@ const ChallengeCard: React.FC<ChallengeProps> = ({
types,
// description,
points,
solved = false,
...other
}) => {
return (
<div
{...other}
className={`transition-bg mb-5 cursor-pointer rounded-lg bg-[#2D2D2D80] p-6 duration-200 ease-in-out hover:border-2 hover:border-[#ff0000] hover:shadow-sm hover:shadow-[#ff0000]`}
className={cn(
`transition-bg mb-5 cursor-pointer rounded-lg bg-[#2D2D2D80] p-6 duration-200 ease-in-out hover:border-2 hover:border-[#ff0000] hover:shadow-sm hover:shadow-[#ff0000]`,
solved ? "border border-green-500" : "",
)}
>
<div className="flex flex-col">
<BsNut size={52} className="rotate-90 text-white" />
Expand Down

0 comments on commit ae1a4f1

Please sign in to comment.