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

79 lines
2.3 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 { TopTen } from "../models/learner";
2018-08-26 15:12:07 +00:00
interface IProps {
topTen: TopTen[];
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;
constructor(props: any) {
super(props);
this.genId = this.genId.bind(this);
this.tableRow = this.tableRow.bind(this);
}
genId() {
return "SCOREBOARD" + this.unique++;
}
tableRow(learner: TopTen) {
// Gotta show respect! (That Unicode monster is a crown)
const username = {
false: learner.username,
true: `${learner.username} 👑`
}[learner.nr === 1];
2018-08-26 15:12:07 +00:00
return <TableRow key={this.genId()}>
<TableCell>
<Typography variant="title" component="b">
{learner.nr}
2018-08-26 15:12:07 +00:00
</Typography>
</TableCell>
<TableCell>
<Typography component="b">{username}</Typography>
2018-08-26 15:12:07 +00:00
</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>;
}
}