Skip to content

Commit

Permalink
attempt to make flashcard game more obvious
Browse files Browse the repository at this point in the history
  • Loading branch information
piatrashkakanstantinass committed Dec 17, 2023
1 parent 63b507f commit 85edf2e
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions better-frontend/src/components/FlashcardShufflePlay.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useEffect, useState } from "react";
import { useState } from "react";
import { FlashcardDto } from "../types/flashcard";
import { Button, HStack, Heading, Text, VStack } from "@chakra-ui/react";
import {
Button,
HStack,
Heading,
Progress,
Text,
VStack,
} from "@chakra-ui/react";

function shuffle<T>(arr: T[]): T[] {
return Array.from(arr).sort(() => Math.random() - 0.5);
Expand All @@ -15,16 +22,19 @@ export default function FlashcardShufflePlay({
const [nextLeftFlashcards, setNextLeftFlashcards] = useState<FlashcardDto[]>(
[]
);
const [rememberedFlashcards, setRememberedFlashcards] = useState<
FlashcardDto[]
>([]);

useEffect(() => {
if (leftFlashcards.length === 0 && nextLeftFlashcards.length > 0) {
setLeftFlashcards(shuffle(nextLeftFlashcards));
setNextLeftFlashcards([]);
}
}, [leftFlashcards, nextLeftFlashcards]);
function nextRound() {
setLeftFlashcards(shuffle(nextLeftFlashcards));
setNextLeftFlashcards([]);
setRememberedFlashcards([]);
}

function remember() {
setLeftFlashcards(leftFlashcards.filter((_v, i) => i !== 0));
setRememberedFlashcards([...rememberedFlashcards, leftFlashcards[0]]);
}

function repeat() {
Expand All @@ -43,9 +53,28 @@ export default function FlashcardShufflePlay({
);
}

if (leftFlashcards.length === 0 && nextLeftFlashcards.length > 0) {
return (
<NextRoundView
rememberedCount={rememberedFlashcards.length}
nextCount={nextLeftFlashcards.length}
onProceed={nextRound}
/>
);
}

if (leftFlashcards.length > 0) {
return (
<>
<Progress
w="full"
value={
((nextLeftFlashcards.length + rememberedFlashcards.length) * 100) /
(nextLeftFlashcards.length +
leftFlashcards.length +
rememberedFlashcards.length)
}
/>
<Flashcard flashcard={leftFlashcards[0]} />
<HStack justifyContent="center" mx="auto">
<Button onClick={remember}>Remeber</Button>
Expand All @@ -56,6 +85,26 @@ export default function FlashcardShufflePlay({
}
}

function NextRoundView({
rememberedCount,
nextCount,
onProceed,
}: {
rememberedCount: number;
nextCount: number;
onProceed: () => void;
}) {
return (
<VStack w="full">
<Heading size="lg">Your progress so far</Heading>
<Text>
{rememberedCount} remembered, {nextCount} left
</Text>
<Button onClick={onProceed}>Continue</Button>
</VStack>
);
}

function Flashcard({ flashcard }: { flashcard: FlashcardDto }) {
const [isFlipped, setIsFlipped] = useState(false);

Expand Down

0 comments on commit 85edf2e

Please sign in to comment.