import * as React from "react"; import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import TextField from "@material-ui/core/TextField"; import Grid from "@material-ui/core/Grid"; import Button from "@material-ui/core/Button"; import Typography from "@material-ui/core/Typography"; import Popover from "@material-ui/core/Popover"; import Paper from "@material-ui/core/Paper"; import { Redirect } from "react-router-dom"; import { IVocab, ReviewMode, VocabType } from "../models/vocab"; interface IProps { levelId: number; vocabByLevel: (level: number) => IVocab[]; } interface IState { input: string; current: number; toSummary: boolean; popoverOpen: boolean; popoverText: string; popoverColor: string; } export default class ReviewPage extends React.Component { private vocab: IVocab[] = []; // Used for positioning the popover private buttonRef: HTMLButtonElement; constructor(props: any) { super(props); this.state = { input: "", current: 0, toSummary: false, popoverOpen: false, popoverText: "", popoverColor: "red", }; const { vocabByLevel, levelId } = this.props; this.vocab = vocabByLevel(levelId); } currentVocab = () => { return this.vocab[this.state.current]; } checkInput = () => { const current = this.currentVocab(); // Check if the given answer is somewhere in the german words const german = this.currentVocab().german.map((str) => str.toLowerCase()); const found = german.find((el) => el === this.state.input.toLowerCase()); // Check if the user's answer was correct // TODO: Levensthein-Distance? if (found) { // TODO: Show it's correct // Show the next vocab word if (this.state.current + 1 >= this.vocab.length) { // TODO: Set some data that the summary screen will show this.setState({ toSummary: true, }); } else { // Increase the vocab this.setState({ current: this.state.current + 1, input: "", }); } } else { this.setState({ popoverOpen: true, popoverText: "Das war nicht richtig", popoverColor: "red", // TODO: Or maybe don't reset the text input: "", }); } // TODO(?): Show a snackbar for showing the updated score } render() { return
{ this.state.toSummary ? ( ) : undefined } {this.currentVocab().latin.grundform} this.setState({ input: ev.target.value, })} onKeyPress={(ev) => { // Allow checking of the answer by pressing Enter if (ev.key === "Enter") this.checkInput(); }} /> this.setState({ popoverOpen: false, })} PaperProps={{ style: { backgroundColor: this.state.popoverColor, padding: 10, color: "white" } }}> {this.state.popoverText}
; } };