From d475a08790311c1ee1ddb5fbd13eef2ca0181e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Tue, 11 Apr 2023 11:22:32 +0200 Subject: [PATCH] Added group check --- ui/Puzzles.tsx | 333 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 268 insertions(+), 65 deletions(-) diff --git a/ui/Puzzles.tsx b/ui/Puzzles.tsx index b3d8c3c..62ae387 100644 --- a/ui/Puzzles.tsx +++ b/ui/Puzzles.tsx @@ -1,23 +1,40 @@ 'use client'; +import { UserContext } from '@/context/user'; +import { useGroups } from '@/lib/hooks/use-groups'; import { usePuzzles } from '@/lib/hooks/use-puzzles'; -import { type Chapter } from '@/lib/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 }); - // SOme chapters have a start date and a end date (for example, the first chapter is only available for 2 weeks), I want to want to lock the chapter if the current date is not between the start and end date - // I want to display a message to the user if the chapter is locked + const [isOpen, setIsOpen] = useState([]); - function isChapterLocked(chapter: Chapter) { + function handleClick(index: number) { + setIsOpen((prevState) => { + const newState = [...prevState]; + newState[index] = !newState[index]; + return newState; + }); + } + + console.log(me); + + function isInEventGroup(chapter: Chapter) { return ( - chapter.startDay && - chapter.endDay && - new Date() > new Date(chapter.startDay) && - new Date() < new Date(chapter.endDay) + chapter.startDate && + chapter.endDate && + me?.groups?.some((group) => group.chapter && group.chapter === chapter.id) ); } @@ -26,65 +43,63 @@ export default function Puzzles({ token }: { token: string }) { {(!isLoading && data?.map((chapter) => (
-
-

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

-
-
+
+
+

+ 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' + })} + +
+ ) : ( +
+
+
+ )}
-
    - {chapter.puzzles && - chapter.puzzles.map((puzzle) => ( - -
  • 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-secondary/30': !puzzle.tags?.length - } - )} - > -
    - {puzzle.name} - {puzzle.tags?.length && ( -
    - {puzzle.tags - .filter((tag) => !['easy', 'medium', 'hard'].includes(tag.name)) - .map((tag, i) => ( - - {tag.name} - - ))} -
    - )} -
    - -
  • -
    - ))} -
+ {isInEventGroup(chapter) && ( +
    + {chapter.puzzles && + chapter.puzzles.map((puzzle) => ( + + ))} +
+ )}
))) || (
@@ -119,3 +134,191 @@ export default function Puzzles({ token }: { token: string }) { ); } + +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 ? ( + <> +
    + +
    + + ) : ( + <> +