From e962fab69badb37084180ae3a235dc74af2aa84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Sun, 23 Apr 2023 15:40:57 +0200 Subject: [PATCH] Fix mutate, added 404 & puzzle double sort --- app/dashboard/puzzles/[id]/not-found.tsx | 22 +++- app/dashboard/puzzles/[id]/page.tsx | 10 +- lib/puzzles.ts | 17 ++- ui/Puzzle.tsx | 5 +- ui/Puzzles.tsx | 151 ++++++++++++++++++----- ui/Select.tsx | 2 +- 6 files changed, 155 insertions(+), 52 deletions(-) diff --git a/app/dashboard/puzzles/[id]/not-found.tsx b/app/dashboard/puzzles/[id]/not-found.tsx index 44d91bd..b297a8c 100644 --- a/app/dashboard/puzzles/[id]/not-found.tsx +++ b/app/dashboard/puzzles/[id]/not-found.tsx @@ -3,10 +3,20 @@ import Image from 'next/image'; import error404 from '@/public/assets/404.png'; export default function NotFound() { - return ( -
-

Oh non! Un François 404

- François 404 -
- ); + const random = Math.floor(Math.random() * 100); + + if (random > 50) { + return ( +
+

Oh non! Un François 404

+ François 404 +
+ ); + } else { + return ( +
+

Oh non! Ce puzzle est introuvable

+
+ ); + } } diff --git a/app/dashboard/puzzles/[id]/page.tsx b/app/dashboard/puzzles/[id]/page.tsx index 09a97f1..9a92695 100644 --- a/app/dashboard/puzzles/[id]/page.tsx +++ b/app/dashboard/puzzles/[id]/page.tsx @@ -17,7 +17,15 @@ export async function generateMetadata({ params }: { params: { id: number } }): const puzzle = await getPuzzle({ token, id }); if (!puzzle) { - notFound(); + return { + title: 'Puzzle introuvable', + openGraph: { + title: 'Puzzle introuvable', + type: 'website', + url: getURL(`/dashboard/puzzles/${id}`) + // IMAGES WITH OG IMAGE + } + }; } return { diff --git a/lib/puzzles.ts b/lib/puzzles.ts index 570162d..1d44eff 100644 --- a/lib/puzzles.ts +++ b/lib/puzzles.ts @@ -5,12 +5,12 @@ export const getChapters = async ({ token }: { token: string }): Promise([]); - const [filter, setFilter] = useState(''); + const [filterTags, setFilterTags] = useState(''); + const [filterDifficulty, setFilterDifficulty] = useState(''); const [filterChapter, setFilterChapter] = useState(); function handleClick(index: number) { @@ -52,7 +61,7 @@ export default function Puzzles({ token }: { token: string }) { if (now > startDate) { const minutes = 10 * 60 * 1000; - if (startDate.getTime() + minutes < now.getTime()) { + if (now.getTime() - startDate.getTime() < minutes) { return true; } } @@ -61,25 +70,39 @@ export default function Puzzles({ token }: { token: string }) { } const filteredData = useMemo(() => { - if (filter && filterChapter) { - if (filter === 'completed' || filter === 'no-completed') { - return data - ?.find((chapter) => chapter.id === filterChapter) - ?.puzzles.filter((puzzle) => { - if (filter === 'completed') { - return puzzle!.score; - } - return !puzzle!.score; - }) - .map((puzzle) => puzzle); - } + if ((filterTags || filterDifficulty) && filterChapter) { return data ?.find((chapter) => chapter.id === filterChapter) - ?.puzzles.filter((puzzle) => puzzle!.tags!.some((tag) => tag.name === filter)) + ?.puzzles.filter((puzzle) => { + if (!puzzle?.tags) return false; + if (filterDifficulty && filterTags) { + if (filterTags === 'completed') { + return puzzle!.tags!.some((tag) => tag.name === filterDifficulty) && puzzle!.score; + } else if (filterTags === 'not-completed') { + return puzzle!.tags!.some((tag) => tag.name === filterDifficulty) && !puzzle!.score; + } + return ( + puzzle!.tags!.some((tag) => tag.name === filterTags) && + puzzle!.tags!.some((tag) => tag.name === filterDifficulty) + ); + } + if (filterDifficulty) { + return puzzle!.tags!.some((tag) => tag.name === filterDifficulty); + } + if (filterTags) { + if (filterTags === 'completed') { + return puzzle!.score; + } else if (filterTags === 'not-completed') { + return !puzzle!.score; + } + return puzzle!.tags!.some((tag) => tag.name === filterTags); + } + return puzzle; + }) .map((puzzle) => puzzle); } return data?.find((chapter) => chapter.id === filterChapter)?.puzzles; - }, [data, filter, filterChapter]); + }, [data, filterTags, filterDifficulty, filterChapter]); return ( <> @@ -109,7 +132,7 @@ export default function Puzzles({ token }: { token: string }) {
{chapter.startDate && chapter.endDate ? ( -
+
{new Date(chapter.startDate).toLocaleDateString('fr-FR', { @@ -134,15 +157,24 @@ export default function Puzzles({ token }: { token: string }) {
)} -
+
{isInEventGroup(chapter) && ( - + <> + + + )}
@@ -202,10 +234,7 @@ function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { const now = new Date(); if (now > startDate) { - const minutes = 10 * 60 * 1000; - if (startDate.getTime() + minutes < now.getTime()) { - return true; - } + return true; } return false; @@ -257,7 +286,10 @@ function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) {
- {puzzle.name} ({puzzle.scoreMax}) + {puzzle.name}{' '} + + ({puzzle.score ? `${puzzle.score}` : '?'}/{puzzle.scoreMax} points) + {puzzle.tags?.length && (
@@ -280,7 +312,7 @@ function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { ); } -function FilterChapter({ +function FilterDifficulty({ chapters, chapter, filter, @@ -294,7 +326,7 @@ function FilterChapter({ setFilterChapter: (chapter: number) => void; }) { const [stored, setStored] = useLocalStorage({ - key: 'puzzles-filter', + key: 'filter-difficulty', initialValue: '' }); @@ -304,12 +336,65 @@ function FilterChapter({ .find((c) => c.id === chapter.id) ?.puzzles?.map((p) => p.tags) .flat() + .filter((tag) => difficulty.some((d) => tag?.name === d.value)) .filter((tag, index, self) => self.findIndex((t) => t!.name === tag!.name) === index) .map((t) => { return { title: t!.name, value: t!.name }; }) as { title: string; value: string }[]; - options?.unshift({ title: 'Pas encore terminé(s)', value: 'no-completed' }); + options?.unshift({ title: 'Toutes les difficultés', value: '' }); + + setFilterChapter(chapter.id); + + function handleChange(event: ChangeEvent) { + setFilter(event.target.value); + // TODO OPTI + // @ts-ignore + setStored(event.target.value); + } + + useEffect(() => { + if (stored) { + // TODO OPTI + // @ts-ignore + setFilter(stored); + } + }, [stored]); + + return