2018-08-26 14:23:48 +00:00
|
|
|
import * as React from "react";
|
|
|
|
|
|
|
|
import Grid from "@material-ui/core/Grid";
|
|
|
|
import Typography from "@material-ui/core/Typography";
|
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import Card from '@material-ui/core/Card';
|
|
|
|
import CardActions from '@material-ui/core/CardActions';
|
|
|
|
import CardContent from '@material-ui/core/CardContent';
|
2018-09-19 16:00:38 +00:00
|
|
|
import Paper from "@material-ui/core/Paper";
|
2018-10-02 15:52:07 +00:00
|
|
|
import Snackbar from "@material-ui/core/Snackbar";
|
2018-09-19 16:00:38 +00:00
|
|
|
import CircularProgress from "@material-ui/core/CircularProgress";
|
2018-08-26 14:23:48 +00:00
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
import { withRouter } from "react-router-dom";
|
2018-09-14 15:19:11 +00:00
|
|
|
|
2018-09-19 16:00:38 +00:00
|
|
|
import { ILevel } from "../models/level";
|
2018-10-02 14:04:38 +00:00
|
|
|
import { IUser } from "../models/user";
|
2018-08-26 14:23:48 +00:00
|
|
|
|
|
|
|
interface IProps {
|
2018-09-19 16:00:38 +00:00
|
|
|
getLevels: () => Promise<ILevel[]>;
|
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
history: any;
|
|
|
|
|
2018-10-02 14:04:38 +00:00
|
|
|
user: IUser;
|
2018-09-19 16:00:38 +00:00
|
|
|
setLevels: (levels: ILevel[]) => void;
|
|
|
|
setLoading: (state: boolean) => void;
|
|
|
|
loading: boolean;
|
2018-10-02 15:52:07 +00:00
|
|
|
snackbar: boolean;
|
|
|
|
setSnackbar: (state: boolean) => void;
|
2018-09-06 18:13:29 +00:00
|
|
|
levels: ILevel[];
|
2018-08-26 14:23:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
const LevelListWithRouter = withRouter(
|
|
|
|
class Dashboard extends React.Component<IProps> {
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.setLoading(true);
|
2018-09-19 16:00:38 +00:00
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
// Fetch the levels
|
|
|
|
this.props.getLevels().then(res => {
|
|
|
|
this.props.setLevels(res);
|
|
|
|
this.props.setLoading(false);
|
|
|
|
});
|
|
|
|
}
|
2018-08-26 14:23:48 +00:00
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
toLevel(id: number) {
|
|
|
|
const maxLevel = Math.max(...this.props.user.levels);
|
2018-10-07 11:50:22 +00:00
|
|
|
if ((maxLevel === -Infinity || maxLevel === +Infinity) || maxLevel + 1 >= id) {
|
2018-10-02 15:52:07 +00:00
|
|
|
this.props.history.push(`/level/${id}`);
|
|
|
|
} else {
|
|
|
|
this.props.setSnackbar(true);
|
|
|
|
}
|
2018-09-19 16:00:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
render() {
|
|
|
|
if (this.props.loading) {
|
|
|
|
return <div>
|
|
|
|
<Grid
|
|
|
|
container
|
|
|
|
spacing={0}
|
|
|
|
direction="column"
|
|
|
|
alignItems="center"
|
|
|
|
justify="center"
|
|
|
|
style={{ minHeight: '100vh' }}>
|
|
|
|
<Grid item xs={12}>
|
|
|
|
<Paper className="paper">
|
|
|
|
<Grid container direction="column" spacing={8}>
|
|
|
|
<CircularProgress />
|
|
|
|
</Grid>
|
|
|
|
</Paper>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const small = window.matchMedia("(max-width: 700px)").matches;
|
|
|
|
const cName = small ? "lesson-card-xs" : "lesson-card-lg";
|
2018-10-02 15:55:29 +00:00
|
|
|
const { levels } = this.props.user;
|
2018-08-26 14:23:48 +00:00
|
|
|
|
2018-10-02 15:52:07 +00:00
|
|
|
let key = 0;
|
|
|
|
const levelToCard = (level: ILevel) => {
|
2018-10-02 15:55:29 +00:00
|
|
|
const suffix = levels.indexOf(level.level) !== -1 ? " ✔" : "";
|
2018-10-02 15:52:07 +00:00
|
|
|
return <Grid item key={key++}>
|
|
|
|
<Card style={{
|
2018-10-10 11:50:30 +00:00
|
|
|
width: small ? window.innerWidth - 32 : "300px"
|
2018-10-02 15:52:07 +00:00
|
|
|
}}>
|
|
|
|
<CardContent className={cName}>
|
|
|
|
<Typography variant="title">{`Level ${level.level}${suffix}`}</Typography>
|
|
|
|
<Typography variant="title" component="p">{level.name}</Typography>
|
|
|
|
<br />
|
|
|
|
<Typography component="p">
|
|
|
|
{level.description}
|
|
|
|
</Typography>
|
|
|
|
</CardContent>
|
|
|
|
<CardActions>
|
|
|
|
<Button
|
|
|
|
className="lesson-card-btn"
|
|
|
|
onClick={() => this.toLevel(level.level)}>
|
|
|
|
Zum Level
|
2018-08-26 14:23:48 +00:00
|
|
|
</Button>
|
2018-10-02 15:52:07 +00:00
|
|
|
</CardActions>
|
|
|
|
</Card>
|
|
|
|
</Grid>;
|
|
|
|
};
|
2018-08-26 14:23:48 +00:00
|
|
|
|
2018-10-10 11:50:30 +00:00
|
|
|
return <div className="content">
|
2018-10-02 15:52:07 +00:00
|
|
|
<Grid container spacing={16} direction="row">
|
|
|
|
{this.props.levels.map(levelToCard)}
|
|
|
|
</Grid>
|
|
|
|
<Snackbar
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: "top",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
open={this.props.snackbar}
|
|
|
|
onClose={() => this.props.setSnackbar(false)}
|
|
|
|
message={<span>Du hast dieses Level noch nicht freigeschaltet!</span>} />
|
|
|
|
</div>;
|
|
|
|
}
|
2018-08-26 14:23:48 +00:00
|
|
|
}
|
2018-10-02 15:52:07 +00:00
|
|
|
);
|
|
|
|
export default LevelListWithRouter;
|