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 Typography from "@material-ui/core/Typography"; import Paper from "@material-ui/core/Paper"; import Grid from "@material-ui/core/Grid"; import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import CircularProgress from "@material-ui/core/CircularProgress"; import { withRouter } from "react-router-dom"; import { IVocab } from "../models/vocab"; interface IProps { id: string; levelVocab: (id: string) => Promise; history: any; loading: boolean; setLoading: (state: boolean) => void; vocab: IVocab[]; setVocab: (vocab: IVocab[]) => void; setLookedAt: (ids: number[]) => void; setCurrentVocab: (vocab: IVocab) => void; drawerButtonState: (state: boolean) => void; currentVocab: IVocab; lookedAt: number[]; }; const LevelPageWithRouter = withRouter( class LevelPage extends React.Component { private uid = 0; // To prevent React from redrawing the vocabulary list and prematurely // cancelling the animation private uids: { [key: string]: string } = {}; componentDidMount() { // Hide the drawer this.props.drawerButtonState(false); // Fetch the vocabulary this.props.setLoading(true); // TODO: Error handling this.props.levelVocab(this.props.id).then(vocab => { this.props.setVocab(vocab); this.props.setCurrentVocab(vocab[0]); this.props.setLoading(false); }); } 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]; } } renderVocabListItem = (vocab: IVocab): any => { // Check if the vocab was already looked at const lookedAt = this.props.lookedAt.find((el) => el === vocab.id) || vocab.id === 0; return { // Prevent the user from using too much memory by always clicking on the elements // Show the clicked at vocab word this.props.setCurrentVocab(vocab); this.props.setLookedAt(lookedAt ? ( this.props.lookedAt ) : this.props.lookedAt.concat(vocab.id)); }}> {`${vocab.latin.grundform} ${lookedAt ? "✔" : ""}`} ; } toReview = () => { const { vocab, lookedAt, id } = this.props; // Only go to the review if all vocabulary item have been looked at if (vocab.length === lookedAt.length) { this.props.setLoading(true); this.props.history.push(`/review/level/${id}`); } } render() { if (this.props.loading) { return
; } const { currentVocab } = this.props; return
{this.props.vocab .map(this.renderVocabListItem)} Zur Übung {currentVocab.latin.grundform} {currentVocab.german.join(", ")} { currentVocab.hint ? (
Tipp: {currentVocab.hint}
) : undefined } { currentVocab.mnemonic ? (
Eselsbrücke: {currentVocab.mnemonic}
) : undefined }
{/*TODO: Maybe "next" and "prev" buttons?*/}
; } } ); export default LevelPageWithRouter;