fixing progress bar on game sections
This commit is contained in:
parent
9a6cd29af6
commit
b0ed40fb07
|
@ -4,17 +4,16 @@ import { useUser } from "../user-context";
|
||||||
import Link from "../components/Link";
|
import Link from "../components/Link";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import SmoothCollapse from "react-smooth-collapse";
|
import SmoothCollapse from "react-smooth-collapse";
|
||||||
import {
|
import { AT } from "@lingdocs/lingdocs-main";
|
||||||
AT,
|
|
||||||
} from "@lingdocs/lingdocs-main";
|
|
||||||
|
|
||||||
function GamesBrowser() {
|
function GamesBrowser() {
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
const [opened, setOpened] = useState<string | undefined>(undefined);
|
const [opened, setOpened] = useState<string | undefined>(undefined);
|
||||||
function handleChapterClick(id: string) {
|
function handleChapterClick(id: string) {
|
||||||
setOpened(prev => prev === id ? undefined : id);
|
setOpened((prev) => (prev === id ? undefined : id));
|
||||||
}
|
}
|
||||||
return <div>
|
return (
|
||||||
|
<div>
|
||||||
{games.map((chapter) => (
|
{games.map((chapter) => (
|
||||||
<div key={chapter.chapter}>
|
<div key={chapter.chapter}>
|
||||||
<ChapterDisplay
|
<ChapterDisplay
|
||||||
|
@ -26,59 +25,83 @@ function GamesBrowser() {
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ChapterDisplay({ chapter, user, handleClick, expanded }: {
|
function ChapterDisplay({
|
||||||
chapter: { chapter: string, items: GameRecord[] },
|
chapter,
|
||||||
user: AT.LingdocsUser | undefined,
|
user,
|
||||||
handleClick: (chapter: string) => void,
|
handleClick,
|
||||||
expanded: boolean,
|
expanded,
|
||||||
|
}: {
|
||||||
|
chapter: { chapter: string; items: GameRecord[] };
|
||||||
|
user: AT.LingdocsUser | undefined;
|
||||||
|
handleClick: (chapter: string) => void;
|
||||||
|
expanded: boolean;
|
||||||
}) {
|
}) {
|
||||||
const [opened, setOpened] = useState<string | undefined>(undefined);
|
const [opened, setOpened] = useState<string | undefined>(undefined);
|
||||||
const progress = getPercentageComplete(chapter, user?.tests);
|
const progress = getPercentageComplete(chapter, user?.tests);
|
||||||
function handleTitleClick(id: string) {
|
function handleTitleClick(id: string) {
|
||||||
setOpened(prev => prev === id ? undefined : id);
|
setOpened((prev) => (prev === id ? undefined : id));
|
||||||
}
|
}
|
||||||
return <div className="mb-3">
|
return (
|
||||||
<div className="card clickable" onClick={() => handleClick(chapter.chapter)}>
|
<div className="mb-3">
|
||||||
<div className="card-body" style={{
|
<div
|
||||||
|
className="card clickable"
|
||||||
|
onClick={() => handleClick(chapter.chapter)}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="card-body"
|
||||||
|
style={{
|
||||||
backgroundColor: expanded ? "#e6e6e6" : "inherit",
|
backgroundColor: expanded ? "#e6e6e6" : "inherit",
|
||||||
}}>
|
}}
|
||||||
|
>
|
||||||
<h4>{chapter.chapter}</h4>
|
<h4>{chapter.chapter}</h4>
|
||||||
<ChapterProgress progress={progress} />
|
<ChapterProgress progress={progress} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<SmoothCollapse expanded={expanded}>
|
<SmoothCollapse expanded={expanded}>
|
||||||
{chapter.items.map(({ id, title, Game }) => {
|
{chapter.items.map(({ id, title, Game }) => {
|
||||||
const done = user?.tests.some(t => t.done && t.id === id);
|
const done = user?.tests.some((t) => t.done && t.id === id);
|
||||||
const open = opened === id;
|
const open = opened === id;
|
||||||
return <div key={id}>
|
return (
|
||||||
|
<div key={id}>
|
||||||
<div className="d-flex flex-row justify-content-between align-items-center">
|
<div className="d-flex flex-row justify-content-between align-items-center">
|
||||||
<div>
|
<div>
|
||||||
<h5 className="my-3 clickable" onClick={() => handleTitleClick(id)}>
|
<h5
|
||||||
<i className={`fas fa-caret-${open ? "down" : "right"}`}></i> {title}
|
className="my-3 clickable"
|
||||||
|
onClick={() => handleTitleClick(id)}
|
||||||
|
>
|
||||||
|
<i
|
||||||
|
className={`fas fa-caret-${open ? "down" : "right"}`}
|
||||||
|
></i>{" "}
|
||||||
|
{title}
|
||||||
{` `}
|
{` `}
|
||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>{done && <h4>✅</h4>}</div>
|
||||||
{done && <h4>✅</h4>}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<SmoothCollapse expanded={open}>
|
<SmoothCollapse expanded={open}>
|
||||||
{open && <Game inChapter={false} />}
|
{open && <Game inChapter={false} />}
|
||||||
</SmoothCollapse>
|
</SmoothCollapse>
|
||||||
</div>
|
</div>
|
||||||
|
);
|
||||||
})}
|
})}
|
||||||
</SmoothCollapse>
|
</SmoothCollapse>
|
||||||
</div>
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ChapterProgress({ progress }: { progress: "not logged in" | number }) {
|
function ChapterProgress({ progress }: { progress: "not logged in" | number }) {
|
||||||
if (progress === "not logged in") {
|
if (progress === "not logged in") {
|
||||||
return <div className="small text-muted"><Link to="/account">Log in</Link> to see progress</div>;
|
return (
|
||||||
|
<div className="small text-muted">
|
||||||
|
<Link to="/account">Log in</Link> to see progress
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return <div>
|
return (
|
||||||
|
<div>
|
||||||
<div className="small text-muted">{progress}% mastered</div>
|
<div className="small text-muted">{progress}% mastered</div>
|
||||||
<div className="progress my-1" style={{ height: "5px" }}>
|
<div className="progress my-1" style={{ height: "5px" }}>
|
||||||
<div
|
<div
|
||||||
|
@ -87,25 +110,24 @@ function ChapterProgress({ progress }: { progress: "not logged in" | number }) {
|
||||||
style={{ width: `${progress}%` }}
|
style={{ width: `${progress}%` }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>;
|
</div>
|
||||||
}
|
|
||||||
|
|
||||||
function getPercentageComplete(
|
|
||||||
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
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPercentageComplete(
|
||||||
|
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) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
export default GamesBrowser;
|
export default GamesBrowser;
|
Loading…
Reference in New Issue