@@ -9,13 +9,11 @@ import { SUBMISSIONS_TABLE_QUERY } from "./query";
99
1010export function SubmissionsDataTable ( ) {
1111 const PAGE_SIZE = 10 ;
12- const [ after , setAfter ] = useState < string | null > ( null ) ;
13- const [ before , setBefore ] = useState < string | null > ( null ) ;
14- const [ direction , setDirection ] = useState < Direction > ( "backward" ) ;
12+ const [ cursors , setCursors ] = useState < ( string | null ) [ ] > ( [ null ] ) ;
13+ const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ;
1514
16- const variables = direction === "backward"
17- ? { first : PAGE_SIZE , after, last : undefined , before : undefined }
18- : { last : PAGE_SIZE , before, first : undefined , after : undefined } ;
15+ const after = cursors [ currentIndex ] ;
16+ const variables = { first : PAGE_SIZE , after } ;
1917
2018 const { data } = useSuspenseQuery ( SUBMISSIONS_TABLE_QUERY , {
2119 variables,
@@ -46,13 +44,15 @@ export function SubmissionsDataTable() {
4644 const handlePageChange = ( direction : Direction ) => {
4745 if ( ! pageInfo ) return ;
4846 if ( direction === "forward" && pageInfo . hasNextPage ) {
49- setAfter ( pageInfo . endCursor ?? null ) ;
50- setBefore ( null ) ;
51- setDirection ( "forward" ) ;
52- } else if ( direction === "backward" && pageInfo . hasPreviousPage ) {
53- setBefore ( pageInfo . startCursor ?? null ) ;
54- setAfter ( null ) ;
55- setDirection ( "backward" ) ;
47+ const nextCursor = pageInfo . endCursor ?? null ;
48+ setCursors ( prev => {
49+ const newCursors = prev . slice ( 0 , currentIndex + 1 ) ;
50+ newCursors . push ( nextCursor ) ;
51+ return newCursors ;
52+ } ) ;
53+ setCurrentIndex ( currentIndex + 1 ) ;
54+ } else if ( direction === "backward" && currentIndex > 0 ) {
55+ setCurrentIndex ( currentIndex - 1 ) ;
5656 }
5757 } ;
5858
@@ -62,7 +62,7 @@ export function SubmissionsDataTable() {
6262 data = { submissionList }
6363 totalCount = { data ?. submissions . totalCount ?? 0 }
6464 hasNextPage = { ! ! pageInfo ?. hasNextPage }
65- hasPreviousPage = { ! ! pageInfo ?. hasPreviousPage }
65+ hasPreviousPage = { currentIndex > 0 }
6666 onPageChange = { handlePageChange }
6767 />
6868 ) ;
0 commit comments