'use client'; import { UserContext } from '@/context/user'; import { useGroups } from '@/lib/hooks/use-groups'; import { usePuzzles } from '@/lib/hooks/use-puzzles'; import type { Chapter, Puzzle } from '@/lib/puzzles'; import { cn } from '@/lib/utils'; import { useContext, useState } from 'react'; import { useForm } from 'react-hook-form'; import AppLink from './AppLink'; import Button from './Button'; import Dialog from './Dialog'; import Icon from './Icon'; import Input from './Input'; import Select from './Select'; export default function Puzzles({ token }: { token: string }) { const { data: me } = useContext(UserContext); const { data, isLoading } = usePuzzles({ token }); const [isOpen, setIsOpen] = useState([]); function handleClick(index: number) { setIsOpen((prevState) => { const newState = [...prevState]; newState[index] = !newState[index]; return newState; }); } console.log(me); function isInEventGroup(chapter: Chapter) { return ( chapter.startDate && chapter.endDate && me?.groups?.some((group) => group.chapter && group.chapter === chapter.id) ); } return ( <> {(!isLoading && data?.map((chapter) => (

Chapitre {chapter.id} - {chapter.name}{' '}

{!isInEventGroup(chapter) && ( handleClick(chapter.id)} trigger={ } className="right-96 p-4" > )}
{chapter.startDate && chapter.endDate ? (
{new Date(chapter.startDate).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: 'numeric' })}{' '} -{' '} {new Date(chapter.endDate).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric', hour: 'numeric' })}
) : (
)}
{isInEventGroup(chapter) && (
    {chapter.puzzles && chapter.puzzles.map((puzzle) => ( ))}
)}
))) || (
{[...Array(3).keys()].map((i) => (
    {[...Array(6).keys()].map((j) => ( ))}
))}
)} ); } function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { const isPuzzleAvailable = (chapter: Chapter) => { const today = new Date(); const startDate = new Date(chapter.startDate!); const endDate = new Date(chapter.endDate!); return ( (chapter.startDate && chapter.endDate && today >= startDate && today <= endDate) || (!chapter.startDate && !chapter.endDate) ); }; return (
  • tag.name.toLowerCase()).includes('easy'), 'border-yellow-600/30': puzzle.tags ?.map((tag) => tag.name.toLowerCase()) .includes('medium'), 'border-red-600/30': puzzle.tags?.map((tag) => tag.name.toLowerCase()).includes('hard'), 'border-highlight-primary': !puzzle.tags?.length, 'cursor-not-allowed': !isPuzzleAvailable(chapter) } )} > {isPuzzleAvailable(chapter) ? (
    {puzzle.name} {puzzle.tags?.length && (
    {puzzle.tags .filter((tag) => !['easy', 'medium', 'hard'].includes(tag.name)) .map((tag, i) => ( {tag.name} ))}
    )}
    ) : (
    {puzzle.name} {puzzle.tags?.length && (
    {puzzle.tags .filter((tag) => !['easy', 'medium', 'hard'].includes(tag.name)) .map((tag, i) => ( {tag.name} ))}
    )}
    )}
  • ); } type GroupData = { name?: string; chapter?: number; puzzle?: number; }; function GroupForm({ chapter, token }: { chapter: Chapter; token: string }) { const [isJoining, setIsJoining] = useState(false); const { data: groups } = useGroups({ token }); const { register, handleSubmit, formState: { errors }, setError, reset } = useForm({ defaultValues: { name: undefined, chapter: chapter.id, puzzle: undefined } }); async function onSubmit(data: GroupData) { await fetch(`${process.env.NEXT_PUBLIC_API_URL}/${isJoining ? 'groupJoin' : 'groupCreate'}`, { method: 'POST', body: JSON.stringify(data), headers: { Authorization: `Bearer ${token}` } }); } return (

    {!isJoining ? ( <>
    ) : ( <>