-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from martius-lab/svea/web-leaderboard
web: leaderboard
- Loading branch information
Showing
4 changed files
with
244 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import * as React from 'react'; | ||
import { useTheme } from '@mui/material'; | ||
import { Typography, Table, TableContainer, TableHead, TableBody, TableRow, TableCell, Paper, Box, TableFooter, TablePagination, IconButton } from "@mui/material"; | ||
import { FirstPage, KeyboardArrowLeft, KeyboardArrowRight, LastPage } from '@mui/icons-material'; | ||
import { LoaderFunctionArgs } from "@remix-run/node"; | ||
import { authenticator } from "~/services/auth.server"; | ||
import { getSession } from "~/services/session.server"; | ||
import { useLoaderData } from "@remix-run/react"; | ||
import { getRankedUsers } from "~/db/sqlite.data"; | ||
|
||
export async function loader({ request, params }: LoaderFunctionArgs) { | ||
const user = await authenticator.isAuthenticated(request, { | ||
failureRedirect: "/login", | ||
}); | ||
|
||
const session = await getSession(request.headers.get("Cookie")); | ||
|
||
if (params.name !== user.name) { | ||
|
||
session.flash("popup", { message: "You don't have permission to access that page", severity: "error" }); | ||
} | ||
|
||
const users = await getRankedUsers(); | ||
return { | ||
users: users, loggedInUsername: user.name | ||
}; | ||
|
||
|
||
} | ||
|
||
interface TablePaginationActionsProps { | ||
count: number; | ||
page: number; | ||
rowsPerPage: number; | ||
onPageChange: ( | ||
event: React.MouseEvent<HTMLButtonElement>, | ||
newPage: number, | ||
) => void; | ||
} | ||
|
||
function TablePaginationActions(props: TablePaginationActionsProps) { | ||
const theme = useTheme(); | ||
const { count, page, rowsPerPage, onPageChange } = props; | ||
|
||
const handleFirstPageButtonClick = ( | ||
event: React.MouseEvent<HTMLButtonElement>, | ||
) => { | ||
onPageChange(event, 0); | ||
}; | ||
|
||
const handleBackButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, page - 1); | ||
}; | ||
|
||
const handleNextButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, page + 1); | ||
}; | ||
|
||
const handleLastPageButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, Math.max(0, Math.ceil(count / rowsPerPage) - 1)); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ flexShrink: 0, ml: 2.5 }}> | ||
<IconButton | ||
onClick={handleFirstPageButtonClick} | ||
disabled={page === 0} | ||
aria-label="first page" | ||
> | ||
{theme.direction === 'rtl' ? <LastPage /> : <FirstPage />} | ||
</IconButton> | ||
<IconButton | ||
onClick={handleBackButtonClick} | ||
disabled={page === 0} | ||
aria-label="previous page" | ||
> | ||
{theme.direction === 'rtl' ? <KeyboardArrowRight /> : <KeyboardArrowLeft />} | ||
</IconButton> | ||
<IconButton | ||
onClick={handleNextButtonClick} | ||
disabled={page >= Math.ceil(count / rowsPerPage) - 1} | ||
aria-label="next page" | ||
> | ||
{theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />} | ||
</IconButton> | ||
<IconButton | ||
onClick={handleLastPageButtonClick} | ||
disabled={page >= Math.ceil(count / rowsPerPage) - 1} | ||
aria-label="last page" | ||
> | ||
{theme.direction === 'rtl' ? <FirstPage /> : <LastPage />} | ||
</IconButton> | ||
</Box> | ||
); | ||
} | ||
|
||
function createData(rank: number, name: string) { | ||
return { rank, name }; | ||
} | ||
|
||
|
||
export default function Leaderboard() { | ||
const [page, setPage] = React.useState(0); | ||
const [rowsPerPage, setRowsPerPage] = React.useState(10); | ||
const usersData = useLoaderData<typeof loader>(); | ||
const { users, loggedInUsername } = usersData; | ||
const rows = users.map((user, index) => createData(index + 1, user.username)); | ||
// Avoid a layout jump when reaching the last page with empty rows. | ||
const emptyRows = | ||
page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0; | ||
|
||
const handleChangePage = ( | ||
event: React.MouseEvent<HTMLButtonElement> | null, | ||
newPage: number, | ||
) => { | ||
setPage(newPage); | ||
}; | ||
|
||
const handleChangeRowsPerPage = ( | ||
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
) => { | ||
setRowsPerPage(parseInt(event.target.value, 10)); | ||
setPage(0); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Typography variant="h4" gutterBottom> | ||
Leaderboard | ||
</Typography> | ||
<TableContainer component={Paper}> | ||
<Table sx={{ minWidth: 100 }} aria-label="custom pagination table"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell style={{ fontWeight: 'bold' }}>Ranking</TableCell> | ||
<TableCell style={{ fontWeight: 'bold' }}>Username</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{(rowsPerPage > 0 | ||
? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) | ||
: rows | ||
).map((row) => ( | ||
<TableRow key={row.rank} style={{ backgroundColor: row.name === loggedInUsername ? 'lightblue' : 'inherit' }}> | ||
<TableCell component="th" scope="row"> | ||
{row.rank} | ||
</TableCell> | ||
<TableCell style={{ width: '50%' }} align="left"> | ||
{row.name} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
{emptyRows > 0 && ( | ||
<TableRow style={{ height: 53 * emptyRows }}> | ||
<TableCell colSpan={6} /> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
<TableFooter> | ||
<TableRow> | ||
<TablePagination | ||
rowsPerPageOptions={[5, 10, 25, 50, { label: 'All', value: -1 }]} | ||
colSpan={3} | ||
count={rows.length} | ||
rowsPerPage={rowsPerPage} | ||
page={page} | ||
slotProps={{ | ||
select: { | ||
inputProps: { | ||
'aria-label': 'rows per page', | ||
}, | ||
native: true, | ||
}, | ||
}} | ||
onPageChange={handleChangePage} | ||
onRowsPerPageChange={handleChangeRowsPerPage} | ||
ActionsComponent={TablePaginationActions} | ||
/> | ||
</TableRow> | ||
</TableFooter> | ||
</Table> | ||
</TableContainer> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters