Implement pagination for registry records table #60
@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import React, { useContext } from 'react';
|
import React, { useContext, useState } from 'react';
|
||||||
import { useQuery } from '@apollo/react-hooks';
|
import { useQuery } from '@apollo/react-hooks';
|
||||||
import { makeStyles } from '@material-ui/core';
|
import { makeStyles } from '@material-ui/core';
|
||||||
import ButtonGroup from '@material-ui/core/ButtonGroup';
|
import ButtonGroup from '@material-ui/core/ButtonGroup';
|
||||||
@ -11,6 +11,8 @@ import Button from '@material-ui/core/Button';
|
|||||||
import TableHead from '@material-ui/core/TableHead';
|
import TableHead from '@material-ui/core/TableHead';
|
||||||
import TableRow from '@material-ui/core/TableRow';
|
import TableRow from '@material-ui/core/TableRow';
|
||||||
import TableBody from '@material-ui/core/TableBody';
|
import TableBody from '@material-ui/core/TableBody';
|
||||||
|
import TableFooter from '@material-ui/core/TableFooter';
|
||||||
|
import TablePagination from '@material-ui/core/TablePagination';
|
||||||
|
|
||||||
import WNS_RECORDS from '../../../gql/wns_records.graphql';
|
import WNS_RECORDS from '../../../gql/wns_records.graphql';
|
||||||
|
|
||||||
@ -68,6 +70,8 @@ export const RecordType = ({ type = types[0].key, onChange }) => {
|
|||||||
const RegistryRecords = ({ type }) => {
|
const RegistryRecords = ({ type }) => {
|
||||||
const { config } = useContext(ConsoleContext);
|
const { config } = useContext(ConsoleContext);
|
||||||
const [sorter, sortBy] = useSorter('createTime', false);
|
const [sorter, sortBy] = useSorter('createTime', false);
|
||||||
|
const [page, setPage] = useState(0);
|
||||||
|
const [rowsPerPage, setRowsPerPage] = useState(5);
|
||||||
const { data } = useQueryStatusReducer(useQuery(WNS_RECORDS, {
|
const { data } = useQueryStatusReducer(useQuery(WNS_RECORDS, {
|
||||||
pollInterval: config.api.intervalQuery,
|
pollInterval: config.api.intervalQuery,
|
||||||
variables: { attributes: { type } }
|
variables: { attributes: { type } }
|
||||||
@ -79,6 +83,17 @@ const RegistryRecords = ({ type }) => {
|
|||||||
|
|
||||||
const records = JSON.parse(data.wns_records.json);
|
const records = JSON.parse(data.wns_records.json);
|
||||||
|
|
||||||
|
const handleChangePage = (event, newPage) => {
|
||||||
|
setPage(newPage);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChangeRowsPerPage = (event) => {
|
||||||
|
setRowsPerPage(parseInt(event.target.value, 10));
|
||||||
|
setPage(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const paginatedRecords = records.sort(sorter).slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Table>
|
<Table>
|
||||||
<TableHead>
|
<TableHead>
|
||||||
@ -93,8 +108,7 @@ const RegistryRecords = ({ type }) => {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{records.sort(sorter)
|
{paginatedRecords.map((record) => {
|
||||||
.map((record) => {
|
|
||||||
const { id, names, createTime, attributes: { type, name: displayName, fileName, version, description, service, package: pkg } } = record;
|
const { id, names, createTime, attributes: { type, name: displayName, fileName, version, description, service, package: pkg } } = record;
|
||||||
|
|
||||||
let pkgLink;
|
let pkgLink;
|
||||||
@ -139,9 +153,21 @@ const RegistryRecords = ({ type }) => {
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
);
|
);
|
||||||
}
|
})}
|
||||||
)}
|
|
||||||
</TableBody>
|
</TableBody>
|
||||||
|
<TableFooter>
|
||||||
|
<TableRow>
|
||||||
|
<TablePagination
|
||||||
|
component="td"
|
||||||
|
rowsPerPageOptions={[5, 10, 25]}
|
||||||
|
count={records.length}
|
||||||
|
rowsPerPage={rowsPerPage}
|
||||||
|
page={page}
|
||||||
|
onPageChange={handleChangePage}
|
||||||
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
||||||
|
/>
|
||||||
|
</TableRow>
|
||||||
|
</TableFooter>
|
||||||
</Table>
|
</Table>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user