This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
Lateinicus/src/pages/review.tsx

253 lines
9.0 KiB
TypeScript
Raw Normal View History

import * as React from "react";
import Card from "@material-ui/core/Card";
2018-09-12 17:23:00 +00:00
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";
2018-09-12 17:23:00 +00:00
import Typography from "@material-ui/core/Typography";
import Popover from "@material-ui/core/Popover";
2018-09-16 15:16:24 +00:00
import LinearProgress from "@material-ui/core/LinearProgress";
2018-09-12 17:23:00 +00:00
import { Redirect } from "react-router-dom";
2018-09-15 14:51:21 +00:00
import { IVocab, IReviewCard, vocabToReviewCard, reviewQTypeToStr } from "../models/vocab";
import { ReviewType, IReviewMetadata } from "../models/review";
import { levW } from "../algorithms/levenshtein";
import { LEVENSHTEIN_MAX_DISTANCE } from "../config";
2018-09-15 14:51:21 +00:00
import { Queue } from "../utils/queue";
interface IProps {
levelId?: number;
vocabByLevel?: (level: number) => IVocab[];
vocabByQueue?: () => IVocab[];
setLastReview: (meta: IReviewMetadata) => void;
reviewType: ReviewType;
drawerButtonState: (state: boolean) => void;
}
interface IState {
input: string;
2018-09-15 14:51:21 +00:00
current: IReviewCard;
2018-09-12 17:23:00 +00:00
metadata: IReviewMetadata;
2018-09-12 17:23:00 +00:00
toSummary: boolean;
popoverOpen: boolean;
popoverText: string;
popoverColor: string;
}
2018-09-12 17:23:00 +00:00
export default class ReviewPage extends React.Component<IProps, IState> {
private vocab: IVocab[] = [];
private reviewQueue: Queue<IReviewCard> = undefined;
2018-09-12 17:23:00 +00:00
// Used for positioning the popover
private buttonRef: HTMLButtonElement;
constructor(props: any) {
super(props);
// Hide the drawer button
this.props.drawerButtonState(false);
// Get the correct vocabulary
const { reviewType, vocabByLevel, levelId, vocabByQueue } = this.props;
switch (reviewType) {
case ReviewType.LEVEL:
if (!vocabByLevel || !levelId) {
alert("[ReviewPage::constructor] vocabByLevel or levelId undefined");
} else {
this.vocab = vocabByLevel(levelId);
}
break;
case ReviewType.QUEUE:
if (!vocabByQueue) {
alert("[ReviewPage::constructor] vocabByQueue undefined");
} else {
this.vocab = vocabByQueue();
}
break;
}
2018-09-15 14:51:21 +00:00
// Turn the vocabulary into IReviewCards and queue them
if (!this.reviewQueue) {
this.reviewQueue = new Queue();
this.vocab.forEach((vocab) => {
vocabToReviewCard(vocab).forEach(this.reviewQueue.enqueue);
});
}
2018-09-15 14:51:21 +00:00
this.state = {
input: "",
current: this.reviewQueue.dequeue(),
metadata: {
correct: 0,
wrong: 0,
},
2018-09-15 14:51:21 +00:00
toSummary: false,
popoverOpen: false,
popoverText: "",
popoverColor: "red",
};
}
increaseMeta = (correct: number, wrong: number): IReviewMetadata => {
const { metadata } = this.state;
return {
wrong: metadata.wrong + wrong,
correct: metadata.correct + correct,
};
}
vocabFromId = (id: number) => {
2018-09-15 14:51:21 +00:00
return this.vocab.find((el) => el.id === this.state.current.id);
}
checkInput = () => {
2018-09-14 14:13:42 +00:00
// Check if the given answer is somewhere in the german words
const { input } = this.state;
2018-09-15 14:51:21 +00:00
// Map all possible answers to lowercase ( => ignore casing)
const answers = this.state.current.answers.map((el) => el.toLowerCase());
// Calculate the distances to all possible answers
const dists = answers.map((el) => levW(input.toLowerCase(), el));
// Find the lowest distance
const minDist = Math.min(...dists);
2018-09-14 14:13:42 +00:00
console.log(this.reviewQueue.size());
// Check if the user's answer was correct
if (minDist === 0) {
// TODO: Show it's correct?
// Show the next vocab word
2018-09-15 14:51:21 +00:00
if (this.reviewQueue.size() === 0) {
// Go to the summary screen
2018-09-12 17:23:00 +00:00
this.setState({
toSummary: true,
}, () => {
// Update the "Last Review" data
this.props.setLastReview(this.state.metadata);
2018-09-12 17:23:00 +00:00
});
} else {
// Increase the vocab
this.setState({
2018-09-15 14:51:21 +00:00
current: this.reviewQueue.dequeue(),
2018-09-12 17:23:00 +00:00
input: "",
// Add one correct answer
metadata: this.increaseMeta(1, 0),
});
}
} else if (minDist <= LEVENSHTEIN_MAX_DISTANCE) {
// TODO: Show a hint
console.log("Partially correct");
} else {
// Find the IVocab item
const vocab = this.vocabFromId(this.state.current.id);
if (vocab) {
// Re-Add the vocabulary item to the review queue
// TODO: Only re-add when it when it's not re-queued
vocabToReviewCard(vocab).forEach(this.reviewQueue.enqueue);
} else {
console.log("[ReviewPage::checkInput] Could not find IVocab item for wrong IReviewCard");
}
2018-09-12 17:23:00 +00:00
this.setState({
popoverOpen: true,
popoverText: "Das war nicht richtig",
popoverColor: "red",
// TODO: Or maybe don't reset the text
input: "",
metadata: this.increaseMeta(0, 1),
2018-09-12 17:23:00 +00:00
});
}
2018-09-12 17:23:00 +00:00
// TODO(?): Show a snackbar for showing the updated score
}
render() {
2018-09-15 14:51:21 +00:00
const { question, qtype } = this.state.current;
const questionTitle = `${question} (${reviewQTypeToStr(qtype)})`;
2018-09-16 15:16:24 +00:00
// TODO:
const progress = 50;
2018-09-12 17:23:00 +00:00
return <div>
{
this.state.toSummary ? (
<Redirect to="/review/summary" />
) : undefined
}
<Grid container justify="center">
<Grid item style={{ width: "100%" }}>
<Card>
2018-09-12 17:23:00 +00:00
<CardContent>
<Grid container direction="column">
<Typography variant="display2">
2018-09-15 14:51:21 +00:00
{questionTitle}
2018-09-12 17:23:00 +00:00
</Typography>
<TextField
margin="normal"
fullWidth={true}
value={this.state.input}
onChange={(ev) => this.setState({
input: ev.target.value,
})}
onKeyPress={(ev) => {
// Allow checking of the answer by pressing Enter
if (ev.key === "Enter")
this.checkInput();
}} />
2018-09-16 15:16:24 +00:00
<LinearProgress
variant="determinate"
value={progress} />
2018-09-12 17:23:00 +00:00
<Popover
open={this.state.popoverOpen}
anchorOrigin={{
vertical: "center",
2018-09-15 14:51:21 +00:00
horizontal: "center"
2018-09-12 17:23:00 +00:00
}}
transformOrigin={{
vertical: "bottom",
2018-09-15 14:51:21 +00:00
horizontal: "center"
2018-09-12 17:23:00 +00:00
}}
anchorEl={this.buttonRef}
onClose={() => this.setState({
popoverOpen: false,
})}
PaperProps={{
style: {
backgroundColor: this.state.popoverColor,
padding: 10,
color: "white"
}
}}>
2018-09-15 14:51:21 +00:00
<Typography
variant="button"
color="inherit">
2018-09-12 17:23:00 +00:00
{this.state.popoverText}
</Typography>
</Popover>
2018-09-15 14:51:21 +00:00
<Button
onClick={this.checkInput}
buttonRef={node => this.buttonRef = node}>
2018-09-12 17:23:00 +00:00
Prüfen
</Button>
2018-09-12 17:23:00 +00:00
</Grid>
</CardContent>
</Card>
</Grid>
</Grid>
</div>;
}
};