diff --git a/lib/leaderboard.ts b/lib/leaderboard.ts new file mode 100644 index 0000000..0f3d387 --- /dev/null +++ b/lib/leaderboard.ts @@ -0,0 +1,27 @@ +import axios from 'axios'; + +export const getScores = async (): Promise => { + const { data, status } = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/leaderboard`, { + insecureHTTPParser: true + }); + + const scores = data; + + if (status !== 200) { + throw new Error('Failed to fetch scores'); + } + + if (!scores) { + return [] as Score[]; + } + + return scores as Score[]; +}; + +export type Score = { + score: number; + tries: number; + completions: number; + pseudo: string; + group: string; +}; diff --git a/lib/nav-items.ts b/lib/nav-items.ts index 09005a9..9754c02 100644 --- a/lib/nav-items.ts +++ b/lib/nav-items.ts @@ -25,7 +25,7 @@ export const navItems: NavItem[] = [ }, { name: 'Classement', - slug: 'ranking', + slug: 'leaderboard', icon: 'line-chart-line', disabled: false }, diff --git a/ui/Select.tsx b/ui/Select.tsx new file mode 100644 index 0000000..778c1e9 --- /dev/null +++ b/ui/Select.tsx @@ -0,0 +1,37 @@ +import { forwardRef } from 'react'; +import type { UseFormRegister } from 'react-hook-form'; +import ErrorMessage from './ErrorMessage'; +import Label from './Label'; + +const Select = forwardRef< + HTMLSelectElement, + React.SelectHTMLAttributes & { + label?: string; + error?: React.ReactNode; + description?: string; + options: { value: string; title: string }[]; + } & Partial>> +>(({ options, className, label, description, error, ...props }, ref) => ( + <> + + {error && {error}} + +)); + +Select.displayName = 'Select'; + +export default Select; diff --git a/ui/UserAuthForm.tsx b/ui/UserAuthForm.tsx index b57c4a1..75cae6d 100644 --- a/ui/UserAuthForm.tsx +++ b/ui/UserAuthForm.tsx @@ -1,19 +1,12 @@ 'use client'; +import axios from 'axios'; import { usePathname, useRouter } from 'next/navigation'; import { useForm } from 'react-hook-form'; import AppLink from './AppLink'; import Button from './Button'; import Input from './Input'; -export default function UserAuthForm() { - return ( - <> - - - ); -} - type FormData = { pseudo: string; email: string; @@ -25,7 +18,7 @@ type FormData = { avatar: string; }; -function AuthForm() { +export default function UserAuthForm() { const { register, handleSubmit, @@ -49,27 +42,57 @@ function AuthForm() { const isSignIn = pathname.includes('sign-in'); async function onSubmit(data: FormData) { - const response = await fetch( + const { data: response, status } = await axios.post( `${process.env.NEXT_PUBLIC_API_URL}/${isSignIn ? 'login' : 'register'}`, { - method: 'POST', + data: { + pseudo: data.pseudo, + email: data.email, + passwd: data.passwd, + firstname: data.firstname, + lastname: data.lastname, + description: data.description, + sgroup: data.sgroup, + avatar: data.avatar + }, headers: { 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - ...data - }) - } + } + }, + { insecureHTTPParser: true } ); - const status = response.status; - const json = await response.json(); - - console.log(json, status); + console.log('response ', response); if (status === 200) { router.push('/dashboard'); } + + // const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/login`, { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/json' + // }, + // body: JSON.stringify({ + // pseudo: data.pseudo, + // email: data.email, + // passwd: data.passwd, + // firstname: data.firstname, + // lastname: data.lastname, + // sgroup: data.sgroup, + // avatar: data.avatar + // }) + // }); + + // const result = await response.json(); + + // if (response.status !== 200) { + // setError('email', { message: result.message }); + // } + + // if (response.status === 200) { + // router.push('/dashboard'); + // } } return (