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/level.tsx

158 lines
6.2 KiB
TypeScript
Raw Normal View History

2018-09-06 18:05:21 +00:00
import * as React from "react";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import Grid from "@material-ui/core/Grid";
import Typography from "@material-ui/core/Typography";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import { Redirect } from "react-router-dom";
2018-09-06 18:05:21 +00:00
import { IVocab } from "../models/vocab";
interface IProps {
id: string;
levelVocab: (id: string) => IVocab[];
};
interface IState {
currentVocab: IVocab;
lookedAt: number[];
toReview: boolean;
2018-09-06 18:05:21 +00:00
};
export default class LevelPage extends React.Component<IProps, IState> {
private uid = 0;
2018-09-14 14:13:42 +00:00
// To prevent React from redrawing the vocabulary list and prematurely
// cancelling the animation
private uids: { [key: string]: string } = {};
2018-09-06 18:05:21 +00:00
constructor(props: any) {
super(props);
this.state = {
currentVocab: this.props.levelVocab(this.props.id)[0],
lookedAt: [0],
toReview: false,
2018-09-06 18:05:21 +00:00
};
}
2018-09-14 14:13:42 +00:00
genUID = (vocab: IVocab): string => {
const { grundform } = vocab.latin;
if (grundform in this.uids) {
return this.uids[grundform];
} else {
this.uids[grundform] = "LEVELPAGE" + this.uid++;
return this.uids[grundform];
}
2018-09-06 18:05:21 +00:00
}
renderVocabListItem = (vocab: IVocab): any => {
// Check if the vocab was already looked at
const lookedAt = this.state.lookedAt.find((el) => el === vocab.id) || vocab.id === 0;
2018-09-14 14:13:42 +00:00
return <ListItem button key={this.genUID(vocab)} onClick={() => {
2018-09-06 18:05:21 +00:00
// Prevent the user from using too much memory by always clicking on the elements
// Show the clicked at vocab word
2018-09-06 18:05:21 +00:00
this.setState({
currentVocab: vocab,
lookedAt: lookedAt ? this.state.lookedAt : this.state.lookedAt.concat(vocab.id),
2018-09-06 18:05:21 +00:00
});
}}>
<ListItemText>
2018-09-14 14:13:42 +00:00
{`${vocab.latin.grundform} ${lookedAt ? "✔" : ""}`}
2018-09-06 18:05:21 +00:00
</ListItemText>
</ListItem>;
}
2018-09-12 17:23:00 +00:00
toReview = () => {
2018-09-14 14:13:42 +00:00
const { levelVocab, id } = this.props;
2018-09-12 17:23:00 +00:00
// Only go to the review if all vocabulary item have been looked at
2018-09-14 14:13:42 +00:00
if (levelVocab(id).length === this.state.lookedAt.length) {
2018-09-12 17:23:00 +00:00
this.setState({
toReview: true,
});
}
}
2018-09-06 18:05:21 +00:00
render() {
const { currentVocab } = this.state;
return <div>
<Grid container direction="row">
<Grid item xs={3}>
<List>
2018-09-14 14:13:42 +00:00
{this.props.levelVocab(this.props.id)
.map(this.renderVocabListItem)}
2018-09-06 18:05:21 +00:00
{/* TODO*/}
2018-09-12 17:23:00 +00:00
<ListItem button onClick={this.toReview}>
2018-09-06 18:05:21 +00:00
<ListItemText>
Zur Übung
</ListItemText>
</ListItem>
</List>
</Grid>
{
this.state.toReview ? (
<Redirect to={`/review/level/${this.props.id}`} />
) : undefined
}
2018-09-06 18:05:21 +00:00
<Grid item lg={7} xs={9}>
<Grid container direction="column">
<Grid item style={{ margin: 12 }}>
<Card>
<CardContent>
<Typography gutterBottom variant="headline" component="h2">
2018-09-14 14:13:42 +00:00
{currentVocab.latin.grundform}
2018-09-06 18:05:21 +00:00
</Typography>
<Typography gutterBottom variant="headline" component="h3">
2018-09-14 14:13:42 +00:00
{currentVocab.german.join(", ")}
2018-09-06 18:05:21 +00:00
</Typography>
{
currentVocab.hint ? (
<div style={{
border: "dashed",
borderColor: "red",
padding: 12,
}}>
<Typography variant="subheading" component="p">
<b>Tipp:</b>
</Typography>
<Typography variant="body2">
{currentVocab.hint}
</Typography>
</div>
) : undefined
}
{
currentVocab.mnemonic ? (
<div style={{
border: "dashed",
borderColor: "#f1c40f",
marginTop: 12,
padding: 12,
}}>
<Typography variant="subheading" component="p">
<b>Eselsbrücke:</b>
</Typography>
<Typography variant="body2">
{currentVocab.mnemonic}
</Typography>
</div>
) : undefined
}
</CardContent>
{/*TODO: Maybe "next" and "prev" buttons?*/}
</Card>
</Grid>
</Grid>
</Grid>
</Grid>
</div>;
}
};