diff --git a/src/games/GamesBrowser.tsx b/src/games/GamesBrowser.tsx index 97fef53..07b7a9f 100644 --- a/src/games/GamesBrowser.tsx +++ b/src/games/GamesBrowser.tsx @@ -4,108 +4,130 @@ import { useUser } from "../user-context"; import Link from "../components/Link"; // @ts-ignore import SmoothCollapse from "react-smooth-collapse"; -import { - AT, -} from "@lingdocs/lingdocs-main"; +import { AT } from "@lingdocs/lingdocs-main"; function GamesBrowser() { - const { user } = useUser(); - const [opened, setOpened] = useState(undefined); - function handleChapterClick(id: string) { - setOpened(prev => prev === id ? undefined : id); - } - return
- {games.map((chapter) => ( -
- -
- ))} + const { user } = useUser(); + const [opened, setOpened] = useState(undefined); + function handleChapterClick(id: string) { + setOpened((prev) => (prev === id ? undefined : id)); + } + return ( +
+ {games.map((chapter) => ( +
+ +
+ ))}
+ ); } -function ChapterDisplay({ chapter, user, handleClick, expanded }: { - chapter: { chapter: string, items: GameRecord[] }, - user: AT.LingdocsUser | undefined, - handleClick: (chapter: string) => void, - expanded: boolean, +function ChapterDisplay({ + chapter, + user, + handleClick, + expanded, +}: { + chapter: { chapter: string; items: GameRecord[] }; + user: AT.LingdocsUser | undefined; + handleClick: (chapter: string) => void; + expanded: boolean; }) { - const [opened, setOpened] = useState(undefined); - const progress = getPercentageComplete(chapter, user?.tests); - function handleTitleClick(id: string) { - setOpened(prev => prev === id ? undefined : id); - } - return
-
handleClick(chapter.chapter)}> -
-

{chapter.chapter}

- -
+ const [opened, setOpened] = useState(undefined); + const progress = getPercentageComplete(chapter, user?.tests); + function handleTitleClick(id: string) { + setOpened((prev) => (prev === id ? undefined : id)); + } + return ( +
+
handleClick(chapter.chapter)} + > +
+

{chapter.chapter}

+
- - {chapter.items.map(({ id, title, Game }) => { - const done = user?.tests.some(t => t.done && t.id === id); - const open = opened === id; - return
-
-
-
handleTitleClick(id)}> - {title} - {` `} -
-
-
- {done &&

} -
-
- - {open && } - +
+ + {chapter.items.map(({ id, title, Game }) => { + const done = user?.tests.some((t) => t.done && t.id === id); + const open = opened === id; + return ( +
+
+
+
handleTitleClick(id)} + > + {" "} + {title} + {` `} +
- })} - +
{done &&

}
+
+ + {open && } + +
+ ); + })} +
- + ); } function ChapterProgress({ progress }: { progress: "not logged in" | number }) { - if (progress === "not logged in") { - return
Log in to see progress
; - } - return
-
{progress}% mastered
-
-
-
-
; + if (progress === "not logged in") { + return ( +
+ Log in to see progress +
+ ); + } + return ( +
+
{progress}% mastered
+
+
+
+
+ ); } function getPercentageComplete( - chapter: { chapter: string, items: GameRecord[] }, - tests: undefined | AT.TestResult[], + chapter: { chapter: string; items: GameRecord[] }, + tests: undefined | AT.TestResult[] ): "not logged in" | number { - if (!tests) return "not logged in"; - const chapterTestIds = chapter.items.map(gr => gr.id); - const userCompletedIds = tests.filter(t => t.done).map(t => t.id); - - const required = chapterTestIds.length; - const completed = chapterTestIds - .filter(x => userCompletedIds.includes(x)) - .length; - - return Math.round( - (completed / (required + 1)) * 100 - ); + if (!tests) return "not logged in"; + const chapterTestIds = chapter.items.map((gr) => gr.id); + const userCompletedIds = tests.filter((t) => t.done).map((t) => t.id); + + const required = chapterTestIds.length; + const completed = chapterTestIds.filter((x) => + userCompletedIds.includes(x) + ).length; + + return Math.round((completed / required) * 100); } -export default GamesBrowser; \ No newline at end of file +export default GamesBrowser;