add pagination

This commit is contained in:
Barry Gitarts 2019-09-19 06:28:51 -04:00 committed by Barry G
parent 256544b077
commit f36bb0a272
2 changed files with 30 additions and 4 deletions

View File

@ -1,9 +1,10 @@
import React, { Fragment, useContext } from 'react' import React, { Fragment, useContext, useState } from 'react'
import useWindowSize from '@rehooks/window-size' import useWindowSize from '@rehooks/window-size'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import Typography from '@material-ui/core/Typography' import Typography from '@material-ui/core/Typography'
import Divider from '@material-ui/core/Divider' import Divider from '@material-ui/core/Divider'
import Fab from '@material-ui/core/Fab' import Fab from '@material-ui/core/Fab'
import Button from '@material-ui/core/Button'
import AddIcon from '@material-ui/icons/Add' import AddIcon from '@material-ui/icons/Add'
import classnames from 'classnames' import classnames from 'classnames'
import { useQuery } from '@apollo/react-hooks' import { useQuery } from '@apollo/react-hooks'
@ -119,8 +120,17 @@ function TableCards({ profiles, classes }) {
function ListProjects() { function ListProjects() {
const classes = useStyles() const classes = useStyles()
const [offset, setOffset] = useState(2)
const windowSize = useWindowSize() const windowSize = useWindowSize()
const { loading, error, data } = useQuery(getProjects) const { loading, error, data, fetchMore } = useQuery(
getProjects,
{
variables: {
offset: 0,
limit: 5
}
}
)
if (loading) return <Loading /> if (loading) return <Loading />
if (error) return <div>{`Error! ${error.message}`}</div> if (error) return <div>{`Error! ${error.message}`}</div>
const { profiles } = data const { profiles } = data
@ -128,6 +138,7 @@ function ListProjects() {
const isSmall = innerWidth < 800 const isSmall = innerWidth < 800
const fabStyle = isSmall ? classnames(classes.fabLink, classes.fabSmall) : classes.fabLink const fabStyle = isSmall ? classnames(classes.fabLink, classes.fabSmall) : classes.fabLink
const titleStyle = isSmall ? classes.tableTitleSmall : classes.tableTitle const titleStyle = isSmall ? classes.tableTitleSmall : classes.tableTitle
console.log({data})
return ( return (
<div className={classes.main}> <div className={classes.main}>
<Typography className={classnames(classes.title, classes.fullWidth)}> <Typography className={classnames(classes.title, classes.fullWidth)}>
@ -142,6 +153,21 @@ function ListProjects() {
{!isSmall && <TableHeader classes={classes} />} {!isSmall && <TableHeader classes={classes} />}
{!isSmall ? <TableRows profiles={profiles} classes={classes} /> : <TableCards profiles={profiles} classes={classes} />} {!isSmall ? <TableRows profiles={profiles} classes={classes} /> : <TableCards profiles={profiles} classes={classes} />}
<Divider className={classes.divider} /> <Divider className={classes.divider} />
<Button onClick={() => {
fetchMore({
variables: {
offset
},
updateQuery: (prev, { fetchMoreResult }) => {
console.log({prev, fetchMoreResult})
if (!fetchMoreResult) return prev;
return Object.assign({}, prev, {
profiles: [...fetchMoreResult.profiles]
});
}
})
setOffset(offset+offset)
}}>Next</Button>
<Link to={`/create-project`} className={fabStyle}> <Link to={`/create-project`} className={fabStyle}>
<Fab className={classes.fab}> <Fab className={classes.fab}>
<AddIcon className={classes.addIcon}/> <AddIcon className={classes.addIcon}/>

View File

@ -51,8 +51,8 @@ query Profile($id: ID!) {
export const getProjects = gql` export const getProjects = gql`
${pledgesInfosFields} ${pledgesInfosFields}
query Projects($type: String! = "PROJECT"){ query Projects($type: String! = "PROJECT", $limit: Int, $offset: Int){
profiles(where: {type: $type, projectInfo_not: null}) { profiles(where: {type: $type, projectInfo_not: null}, first: $limit, skip: $offset) {
id id
addr addr
canceled canceled