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/frontend/src/components/scoreboard.tsx

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-08-26 15:12:07 +00:00
import * as React from "react";
import Table from "@material-ui/core/Table";
import TableHead from "@material-ui/core/TableHead";
import TableBody from "@material-ui/core/TableBody";
import TableRow from "@material-ui/core/TableRow";
import TableCell from "@material-ui/core/TableCell";
import Typography from "@material-ui/core/Typography";
import { ILearner } from "../models/learner";
interface IProps {
2018-09-14 16:53:01 +00:00
topTen: ILearner[];
2018-08-26 15:12:07 +00:00
}
2018-09-14 16:53:01 +00:00
export default class Scoreboard extends React.Component<IProps> {
2018-08-26 15:12:07 +00:00
private unique = 0;
private nr = 1;
constructor(props: any) {
super(props);
this.genId = this.genId.bind(this);
this.tableRow = this.tableRow.bind(this);
}
genId() {
return "SCOREBOARD" + this.unique++;
}
tableRow(learner: ILearner) {
return <TableRow key={this.genId()}>
<TableCell>
<Typography variant="title" component="b">
{this.nr++}
</Typography>
</TableCell>
<TableCell>
<Typography component="b">{learner.username}</Typography>
</TableCell>
{/*<TableCell numeric>{learner.level}</TableCell>*/}
{/* To make this fit on both mobile and desktop, we don't use
numeric, as it would otherwise look weir otherwise look weird */}
<TableCell>{learner.score}</TableCell>
</TableRow>
}
render() {
2018-09-14 16:53:01 +00:00
const sortedLearners = this.props.topTen.sort((a, b) => {
2018-08-26 15:12:07 +00:00
if (a.score > b.score) {
return -1;
} else if (a.score < b.score) {
return 1;
}
return 0;
});
return <Table padding="none">
<TableHead>
<TableRow>
<TableCell>#</TableCell>
<TableCell>User</TableCell>
{/*<TableCell>Level</TableCell>*/}
<TableCell>Punktzahl</TableCell>
</TableRow>
</TableHead>
<TableBody>
{sortedLearners.map(this.tableRow)}
</TableBody>
</Table>;
}
}