This commit is contained in:
Théo 2023-04-12 20:14:05 +02:00
parent 0b0c3104b7
commit cbdac511dd
5 changed files with 45 additions and 29 deletions

View file

@ -19,7 +19,7 @@ export default function Page() {
isLoading={isLoading} isLoading={isLoading}
icon="pie-chart-line" icon="pie-chart-line"
title="Puzzles résolus" title="Puzzles résolus"
data={me?.completions} data={me?.completions || 0}
/> />
<Card <Card
isLoading={isLoading} isLoading={isLoading}
@ -27,7 +27,12 @@ export default function Page() {
title="Badges obtenus" title="Badges obtenus"
data={me?.badges?.length || 'Aucun'} data={me?.badges?.length || 'Aucun'}
/> />
<Card isLoading={isLoading} icon="bar-chart-line" title="Rang actuel" data={me?.rank} /> <Card
isLoading={isLoading}
icon="bar-chart-line"
title="Rang actuel"
data={me?.rank || 'Non classé'}
/>
</main> </main>
</section> </section>
</div> </div>

View file

@ -15,11 +15,17 @@ type PuzzleData = {
code_file: File[]; code_file: File[];
}; };
type Granted = {
[key: string]: number;
};
export default function Puzzle({ puzzle }: { puzzle: PuzzleType }) { export default function Puzzle({ puzzle }: { puzzle: PuzzleType }) {
if (!puzzle) { if (!puzzle) {
notFound(); notFound();
} }
// const [granted, setGranted] = useState({});
const { const {
register, register,
handleSubmit, handleSubmit,
@ -81,6 +87,7 @@ export default function Puzzle({ puzzle }: { puzzle: PuzzleType }) {
required required
{...register('answer')} {...register('answer')}
/> />
{/* <Input {/* <Input
className="h-16 w-full sm:w-1/3" className="h-16 w-full sm:w-1/3"
label="Code" label="Code"

View file

@ -28,8 +28,6 @@ export function Timer({ targetDate, className }: { targetDate: Date; className?:
seconds: 0 seconds: 0
}); });
targetDate = new Date(targetDate);
useEffect(() => { useEffect(() => {
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
const timeDifference = targetDate.getTime() - Date.now(); const timeDifference = targetDate.getTime() - Date.now();

View file

@ -53,43 +53,53 @@ export default function UserAuthForm() {
} }
); );
if (!res || res.status !== 200) { if (!res) {
setError('passwd', { setError('passwd', {
type: 'manual', type: 'manual',
message: "Une erreur s'est produite." message: "Une erreur s'est produite."
}); });
setIsLoading(false);
} }
if (res && !isSignIn) { if (!isSignIn) {
const { username_valid, email_valid } = await res.json(); if (res.status === 400) {
if (!username_valid) { const { username_valid, email_valid } = await res.json();
setError('pseudo', { if (!username_valid) {
type: 'manual', setError('pseudo', {
message: "Nom d'utilisateur indisponible" type: 'manual',
}); message: "Nom d'utilisateur indisponible"
} });
if (!email_valid) {
setError('email', { setIsLoading(false);
type: 'manual', }
message: 'Adresse e-mail indisponible' if (!email_valid) {
}); setError('email', {
type: 'manual',
message: 'Adresse e-mail indisponible'
});
}
setIsLoading(false);
} }
} }
if (res && res.ok && res.status === 200) { if (res.status === 200) {
const token = res.headers.get('Authorization')?.split(' ')[1]; const token = res.headers.get('Authorization')?.split(' ')[1];
if (token) { if (token) {
console.log(token);
cookies.set('token', token, { cookies.set('token', token, {
sameSite: 'strict', sameSite: 'strict',
secure: process.env.NODE_ENV === 'production' secure: process.env.NODE_ENV === 'production'
}); });
router.refresh(); router.refresh();
} }
} else if (res && res.status === 400) { } else {
setError('passwd', { setError('passwd', {
type: 'manual', type: 'manual',
message: "Nom d'utilisateur ou mot de passe incorrect" message: "Nom d'utilisateur ou mot de passe incorrect"
}); });
setIsLoading(false);
} }
setIsLoading(false); setIsLoading(false);

View file

@ -6,15 +6,16 @@ import type { ScoreEvent } from '@/lib/leaderboard';
import type { SWRSubscription } from 'swr/subscription'; import type { SWRSubscription } from 'swr/subscription';
import useSWRSubscription from 'swr/subscription'; import useSWRSubscription from 'swr/subscription';
import Podium from './podium/Podium'; import Podium from './podium/Podium';
import { Timer } from '../Timer';
const SCORE_COLORS = ['text-yellow-400', 'text-gray-400', 'text-orange-400']; const SCORE_COLORS = ['text-yellow-400', 'text-gray-400', 'text-orange-400'];
export default function EventLeaderboard({ token, id }: { token: string; id: number }) { export default function EventLeaderboard({ id }: { token: string; id: number }) {
const subscription: SWRSubscription<string, ScoreEvent, Error> = (key, { next }) => { const subscription: SWRSubscription<string, ScoreEvent, Error> = (key, { next }) => {
const socket = new WebSocket(key); const socket = new WebSocket(key);
socket.addEventListener('message', (event) => { socket.addEventListener('message', (event) => {
next(JSON.parse(event.data)); next(null, JSON.parse(event.data));
}); });
socket.addEventListener('error', (event) => { socket.addEventListener('error', (event) => {
@ -25,12 +26,10 @@ export default function EventLeaderboard({ token, id }: { token: string; id: num
}; };
const { data, error } = useSWRSubscription( const { data, error } = useSWRSubscription(
`ws://${process.env.NEXT_PUBLIC_API_URL?.split('//')[1]}/rleaderboard/${id}`, `wss://${process.env.NEXT_PUBLIC_API_URL?.split('//')[1]}/rleaderboard/${id}`,
subscription subscription
); );
// const { data, isLoading } = useLeaderboardEvent({ token, id });
const scores = [data?.groups] const scores = [data?.groups]
.flat() .flat()
.sort((a, b) => a!.rank - b!.rank) .sort((a, b) => a!.rank - b!.rank)
@ -39,13 +38,10 @@ export default function EventLeaderboard({ token, id }: { token: string; id: num
place place
})); }));
if (error) return <div>failed to load</div>;
if (!data) return <div>loading...</div>;
return ( return (
<section className="flex h-full w-full flex-col space-y-4 p-4"> <section className="flex h-full w-full flex-col space-y-4 p-4">
{data && <Podium score={scores} />} {data && <Podium score={scores} />}
{/* <Timer targetDate={data && data.end_date} /> */} {data && data.end_date && <Timer targetDate={new Date(data.end_date)} />}
<main className="flex flex-col justify-between space-x-0 space-y-4 pb-4"> <main className="flex flex-col justify-between space-x-0 space-y-4 pb-4">
<ul className="flex flex-col space-y-2"> <ul className="flex flex-col space-y-2">
{data?.groups.map((group, key) => ( {data?.groups.map((group, key) => (