11import { Auth , Button } from "@supabase/ui" ;
22import type { NextPage } from "next" ;
33import Head from "next/head" ;
4- import { useQuery } from "urql " ;
4+ import React from "react " ;
55import { gql } from "../gql" ;
66import { Container } from "../lib/container" ;
77import { FeedItem } from "../lib/feed-item" ;
88import { Loading } from "../lib/loading" ;
99import { MainSection } from "../lib/main-section" ;
1010import { noopUUID } from "../lib/noop-uuid" ;
11+ import { usePaginatedQuery } from "../lib/use-paginated-query" ;
1112
1213const IndexRouteQuery = gql ( /* GraphQL */ `
13- query IndexRouteQuery($profileId: UUID!) {
14+ query IndexRouteQuery($profileId: UUID!, $after: Cursor ) {
1415 feed: postCollection(
1516 orderBy: [
1617 { voteRank: AscNullsFirst }
1718 { score: DescNullsFirst }
1819 { createdAt: DescNullsFirst }
1920 ]
2021 first: 15
22+ after: $after
2123 ) {
2224 pageInfo {
2325 hasNextPage
26+ endCursor
2427 }
2528 edges {
2629 cursor
@@ -35,10 +38,25 @@ const IndexRouteQuery = gql(/* GraphQL */ `
3538
3639const Home : NextPage = ( ) => {
3740 const { user } = Auth . useUser ( ) ;
38- const [ indexQuery ] = useQuery ( {
41+ const [ lastCursor , setLastCursor ] = React . useState < string | undefined > (
42+ undefined
43+ ) ;
44+ const [ indexQuery ] = usePaginatedQuery ( {
3945 query : IndexRouteQuery ,
4046 variables : {
4147 profileId : user ?. id ?? noopUUID ,
48+ after : lastCursor ,
49+ } ,
50+ mergeResult ( oldData , newData ) {
51+ return {
52+ ...oldData ,
53+ ...newData ,
54+ feed : {
55+ ...oldData . feed ! ,
56+ ...newData . feed ! ,
57+ edges : [ ...oldData . feed ! . edges , ...newData . feed ! . edges ] ,
58+ } ,
59+ } ;
4260 } ,
4361 } ) ;
4462
@@ -54,15 +72,23 @@ const Home: NextPage = () => {
5472 < section className = "text-gray-600 body-font overflow-hidden w-full" >
5573 < div className = "container px-3 py-24 mx-auto" >
5674 < div className = "-my-8" >
57- { indexQuery . fetching && < Loading /> }
5875 { indexQuery ?. data ?. feed ?. edges . map ( ( edge ) => (
5976 < FeedItem post = { edge . node ! } key = { edge . cursor } />
6077 ) ) }
78+ { indexQuery . fetching ? < Loading /> : null }
6179 </ div >
6280 </ div >
6381 { indexQuery . data ?. feed ?. pageInfo . hasNextPage ? (
6482 < div className = "flex justify-center content-center" >
65- < Button > Load more.</ Button >
83+ < Button
84+ onClick = { ( ) => {
85+ setLastCursor (
86+ indexQuery . data ?. feed ?. pageInfo . endCursor ?? undefined
87+ ) ;
88+ } }
89+ >
90+ Load more.
91+ </ Button >
6692 </ div >
6793 ) : null }
6894 </ section >
0 commit comments