|
1 | 1 | import React from "react";
|
2 |
| -import { Row } from "reactstrap"; |
| 2 | +import { Row, Form, FormGroup, CustomInput } from "reactstrap"; |
3 | 3 | import { BootstrapTable, TableHeaderColumn } from "react-bootstrap-table";
|
4 | 4 | import { RankingEntry } from "../interfaces/RankingEntry";
|
| 5 | +import { useLocalStorage } from "../utils/LocalStorage"; |
| 6 | +import { UserNameLabel } from "./UserNameLabel"; |
5 | 7 |
|
6 | 8 | interface Props {
|
7 | 9 | title: React.ReactNode;
|
@@ -35,51 +37,68 @@ const refineRanking = (ranking: RankingEntry[]): InternalRankEntry[] =>
|
35 | 37 | return array;
|
36 | 38 | }, [] as InternalRankEntry[]);
|
37 | 39 |
|
38 |
| -export const Ranking: React.FC<Props> = (props) => ( |
39 |
| - <Row> |
40 |
| - <h2>{props.title}</h2> |
41 |
| - <BootstrapTable |
42 |
| - height="auto" |
43 |
| - data={refineRanking(props.ranking)} |
44 |
| - pagination |
45 |
| - striped |
46 |
| - hover |
47 |
| - search |
48 |
| - options={{ |
49 |
| - paginationPosition: "top", |
50 |
| - sizePerPage: 20, |
51 |
| - sizePerPageList: [ |
52 |
| - { |
53 |
| - text: "20", |
54 |
| - value: 20, |
55 |
| - }, |
56 |
| - { |
57 |
| - text: "50", |
58 |
| - value: 50, |
59 |
| - }, |
60 |
| - { |
61 |
| - text: "100", |
62 |
| - value: 100, |
63 |
| - }, |
64 |
| - { |
65 |
| - text: "200", |
66 |
| - value: 200, |
67 |
| - }, |
68 |
| - { |
69 |
| - text: "All", |
70 |
| - value: props.ranking.length, |
71 |
| - }, |
72 |
| - ], |
73 |
| - }} |
74 |
| - > |
75 |
| - <TableHeaderColumn dataField="rank">#</TableHeaderColumn> |
76 |
| - <TableHeaderColumn dataField="id" isKey> |
77 |
| - User |
78 |
| - </TableHeaderColumn> |
79 |
| - <TableHeaderColumn dataField="count">Count</TableHeaderColumn> |
80 |
| - </BootstrapTable> |
81 |
| - </Row> |
82 |
| -); |
| 40 | +export const Ranking: React.FC<Props> = (props) => { |
| 41 | + const [showRating, setShowRating] = useLocalStorage("showRating", false); |
| 42 | + const usernameFormatter = (cell: string) => { |
| 43 | + return <UserNameLabel userId={cell} showRating={showRating} />; |
| 44 | + }; |
| 45 | + return ( |
| 46 | + <Row> |
| 47 | + <Form inline> |
| 48 | + <h2>{props.title}</h2> |
| 49 | + <FormGroup className="ml-3"> |
| 50 | + <CustomInput |
| 51 | + type="switch" |
| 52 | + id="showRating" |
| 53 | + label="Show Rating" |
| 54 | + checked={showRating} |
| 55 | + onChange={(): void => setShowRating(!showRating)} |
| 56 | + /> |
| 57 | + </FormGroup> |
| 58 | + </Form> |
| 59 | + <BootstrapTable |
| 60 | + height="auto" |
| 61 | + data={refineRanking(props.ranking)} |
| 62 | + pagination |
| 63 | + striped |
| 64 | + hover |
| 65 | + search |
| 66 | + options={{ |
| 67 | + paginationPosition: "top", |
| 68 | + sizePerPage: 20, |
| 69 | + sizePerPageList: [ |
| 70 | + { |
| 71 | + text: "20", |
| 72 | + value: 20, |
| 73 | + }, |
| 74 | + { |
| 75 | + text: "50", |
| 76 | + value: 50, |
| 77 | + }, |
| 78 | + { |
| 79 | + text: "100", |
| 80 | + value: 100, |
| 81 | + }, |
| 82 | + { |
| 83 | + text: "200", |
| 84 | + value: 200, |
| 85 | + }, |
| 86 | + { |
| 87 | + text: "All", |
| 88 | + value: props.ranking.length, |
| 89 | + }, |
| 90 | + ], |
| 91 | + }} |
| 92 | + > |
| 93 | + <TableHeaderColumn dataField="rank">#</TableHeaderColumn> |
| 94 | + <TableHeaderColumn dataField="id" isKey dataFormat={usernameFormatter}> |
| 95 | + User |
| 96 | + </TableHeaderColumn> |
| 97 | + <TableHeaderColumn dataField="count">Count</TableHeaderColumn> |
| 98 | + </BootstrapTable> |
| 99 | + </Row> |
| 100 | + ); |
| 101 | +}; |
83 | 102 |
|
84 | 103 | const refineRankingWithOffset = (
|
85 | 104 | ranking: RankingEntry[],
|
@@ -140,9 +159,24 @@ export const RemoteRanking: React.FC<RemoteProps> = (props) => {
|
140 | 159 | });
|
141 | 160 | };
|
142 | 161 | const offset = (page - 1) * sizePerPage;
|
| 162 | + const [showRating, setShowRating] = useLocalStorage("showRating", false); |
| 163 | + const usernameFormatter = (cell: string) => { |
| 164 | + return <UserNameLabel userId={cell} showRating={showRating} />; |
| 165 | + }; |
143 | 166 | return (
|
144 | 167 | <Row>
|
145 |
| - <h2>{props.titleComponent}</h2> |
| 168 | + <Form inline> |
| 169 | + <h2>{props.titleComponent}</h2> |
| 170 | + <FormGroup className="ml-3"> |
| 171 | + <CustomInput |
| 172 | + type="switch" |
| 173 | + id="showRating" |
| 174 | + label="Show Rating" |
| 175 | + checked={showRating} |
| 176 | + onChange={(): void => setShowRating(!showRating)} |
| 177 | + /> |
| 178 | + </FormGroup> |
| 179 | + </Form> |
146 | 180 | <BootstrapTable
|
147 | 181 | height="auto"
|
148 | 182 | data={refineRankingWithOffset(
|
@@ -182,7 +216,7 @@ export const RemoteRanking: React.FC<RemoteProps> = (props) => {
|
182 | 216 | }}
|
183 | 217 | >
|
184 | 218 | <TableHeaderColumn dataField="rank">#</TableHeaderColumn>
|
185 |
| - <TableHeaderColumn dataField="id" isKey> |
| 219 | + <TableHeaderColumn dataField="id" isKey dataFormat={usernameFormatter}> |
186 | 220 | User
|
187 | 221 | </TableHeaderColumn>
|
188 | 222 | <TableHeaderColumn dataField="count">Count</TableHeaderColumn>
|
|
0 commit comments