@@ -2,6 +2,8 @@ import React from 'react';
22import axios from 'axios' ;
33
44import { withStyles , makeStyles } from '@material-ui/core/styles' ;
5+ import Box from '@material-ui/core/Box' ;
6+ import Button from '@material-ui/core/Button' ;
57import Table from '@material-ui/core/Table' ;
68import TableBody from '@material-ui/core/TableBody' ;
79import TableCell from '@material-ui/core/TableCell' ;
@@ -43,8 +45,8 @@ const useStyles = makeStyles({
4345} ) ;
4446
4547
46- function getModels ( auth ) {
47- const url = baseUrl + " /models/?size=10000" ;
48+ function getModels ( auth , from_index , size ) {
49+ const url = ` ${ baseUrl } /models/?size=${ size } &from_index= ${ from_index } ` ;
4850 const config = {
4951 headers : {
5052 'Authorization' : 'Bearer ' + auth . token ,
@@ -239,22 +241,29 @@ export default function ModelTable(props) {
239241 const [ order , setOrder ] = React . useState ( 'desc' ) ;
240242 const [ orderBy , setOrderBy ] = React . useState ( 'date_created' ) ;
241243 const [ sortType , setSortType ] = React . useState ( 'string' ) ;
244+ const [ page , setPage ] = React . useState ( 1 ) ;
245+
246+ const modelsPerPage = 20 ;
242247
243248 React . useEffect ( ( ) => {
244249 setLoading ( true ) ;
245- getModels ( props . auth )
250+ getModels ( props . auth , modelsPerPage * ( page - 1 ) , modelsPerPage )
246251 . then ( res => {
247252 console . log ( "Got models" ) ;
248253
249- setModels ( addAdditionalFields ( res . data ) ) ;
254+ setModels ( models . concat ( addAdditionalFields ( res . data ) ) ) ;
250255 setLoading ( false ) ;
251256 } )
252257 . catch ( err => {
253258
254259 setErrorMessage ( 'Error loading models: ' , err . message ) ;
255260 setLoading ( false ) ;
256261 } ) ;
257- } , [ ] ) ;
262+ } , [ page ] ) ;
263+
264+ const handleLoadMore = ( ) => {
265+ setPage ( page + 1 ) ;
266+ }
258267
259268 const handleRequestSort = ( event , property , type ) => {
260269 const isAsc = orderBy === property && order === 'asc' ;
@@ -271,7 +280,7 @@ export default function ModelTable(props) {
271280 const SortableColumnHeader = ( col ) => {
272281 if ( col . sortAttr ) {
273282 return (
274- < TableCell sortDirection = { orderBy === col . sortAttr ? order : false } >
283+ < TableCell sortDirection = { orderBy === col . sortAttr ? order : false } key = { col . label } >
275284 < TableSortLabel
276285 active = { orderBy === col . sortAttr }
277286 direction = { orderBy === col . sortAttr ? order : 'asc' }
@@ -288,7 +297,7 @@ export default function ModelTable(props) {
288297 )
289298 } else {
290299 return (
291- < TableCell > < b > { col . label } </ b > </ TableCell >
300+ < TableCell key = { col . label } > < b > { col . label } </ b > </ TableCell >
292301 )
293302 }
294303 }
@@ -332,6 +341,7 @@ export default function ModelTable(props) {
332341 )
333342 } else {
334343 return (
344+ < React . Fragment >
335345 < TableContainer component = { Paper } >
336346 < Table className = { classes . table } size = "small" aria-label = "simple table" >
337347 < TableHead >
@@ -378,7 +388,12 @@ export default function ModelTable(props) {
378388 ) ) }
379389 </ TableBody >
380390 </ Table >
391+
381392 </ TableContainer >
393+ < Box textAlign = 'center' my = { 1 } >
394+ < Button color = "primary" onClick = { handleLoadMore } > Load more</ Button >
395+ </ Box >
396+ </ React . Fragment >
382397 ) ;
383398 }
384399}
0 commit comments