11import { useEffect , useState } from "react" ;
22import { useAtomValue , useSetAtom } from "jotai" ;
3- import { useParams , Link , useNavigate , useLocation } from "react-router-dom" ;
3+ import {
4+ useParams ,
5+ Link ,
6+ useNavigate ,
7+ useLocation ,
8+ useSearchParams ,
9+ } from "react-router-dom" ;
410import { Notification , Icon , Row , Col } from "@canonical/react-components" ;
511
612import { useRemodels } from "../../hooks" ;
@@ -17,8 +23,16 @@ import type { Remodel, RemodelResponse, ApiResponse } from "../../types/shared";
1723
1824function Remodel ( ) : React . JSX . Element {
1925 const { id, modelId } = useParams ( ) ;
26+ const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
2027 const location = useLocation ( ) ;
2128 const brandId = useAtomValue ( brandIdState ) ;
29+ const [ currentCursor , setCurrentCursor ] = useState < string | null > ( null ) ;
30+ const [ nextCursor , setNextCursor ] = useState < string | null > ( null ) ;
31+ const [ cursorHistory , setCursorHistory ] = useState < Array < string | null > > ( [ ] ) ;
32+
33+ const pageSizeParam = searchParams . get ( "page-size" ) ;
34+ const pageSize = pageSizeParam ? parseInt ( pageSizeParam ) : 25 ;
35+
2236 const {
2337 isLoading,
2438 isError,
@@ -28,6 +42,11 @@ function Remodel(): React.JSX.Element {
2842 } : UseQueryResult < ApiResponse < RemodelResponse > , Error > = useRemodels (
2943 brandId ,
3044 modelId ,
45+ {
46+ fromModel : modelId ,
47+ pageSize : pageSize ,
48+ page : currentCursor ,
49+ } ,
3150 ) ;
3251 const setRemodels = useSetAtom ( remodelsListState ) ;
3352 const [ showNotification , setShowNotification ] = useState ( false ) ;
@@ -36,13 +55,33 @@ function Remodel(): React.JSX.Element {
3655 const brandStore = useAtomValue ( brandStoreState ( id ) ) ;
3756 const navigate = useNavigate ( ) ;
3857
58+ const handlePageForward = ( ) => {
59+ setCursorHistory ( ( prev ) => {
60+ return [ ...prev , currentCursor ] ;
61+ } ) ;
62+ setCurrentCursor ( nextCursor ) ;
63+ } ;
64+
65+ const handlePageBack = ( ) => {
66+ const lastCursor = cursorHistory [ cursorHistory . length - 1 ] ;
67+ setCurrentCursor ( lastCursor ) ;
68+ setCursorHistory ( ( prev ) => {
69+ return prev . filter ( ( c ) => c !== lastCursor ) ;
70+ } ) ;
71+ } ;
72+
3973 brandStore
4074 ? setPageTitle ( `Remodels in ${ brandStore . name } ` )
4175 : setPageTitle ( "Remodels" ) ;
4276
4377 useEffect ( ( ) => {
44- if ( ! isLoading && ! isError && data ) {
78+ if ( isLoading || isError ) {
79+ return ;
80+ }
81+
82+ if ( data ) {
4583 setRemodels ( data . data ?. allowlist || [ ] ) ;
84+ setNextCursor ( data . data ?. [ "next-cursor" ] || null ) ;
4685 }
4786 } , [ isLoading , isError , data , brandId , id ] ) ;
4887
@@ -76,7 +115,18 @@ function Remodel(): React.JSX.Element {
76115 </ Col >
77116 </ Row >
78117 < div className = "u-flex-column u-flex-grow" >
79- < RemodelTable />
118+ { data && (
119+ < RemodelTable
120+ handlePageForward = { handlePageForward }
121+ handlePageBack = { handlePageBack }
122+ forwardDisabled = { ! nextCursor }
123+ backDisabled = {
124+ cursorHistory . length < 1 || currentCursor === null
125+ }
126+ pageSize = { pageSize }
127+ setSearchParams = { setSearchParams }
128+ />
129+ ) }
80130 </ div >
81131 </ >
82132 ) }
0 commit comments