11"use client" ;
22import Header from "@/components/Header/header" ;
3- import { Col , Layout , message , Row , Table } from "antd" ;
3+ import { Col , Layout , message , PaginationProps , Row , Table } from "antd" ;
44import { Content } from "antd/es/layout/layout" ;
55import { CodeOutlined , HistoryOutlined } from "@ant-design/icons" ;
66import "./styles.scss" ;
@@ -24,6 +24,13 @@ interface Submission {
2424 historyDocRefId : string ;
2525}
2626
27+ interface TablePagination {
28+ totalCount : number ;
29+ totalPages : number ;
30+ currentPage : number ;
31+ limit : number ;
32+ }
33+
2734export default function QuestionPage ( ) {
2835 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ; // Store the states related to table's loading
2936
@@ -59,6 +66,12 @@ export default function QuestionPage() {
5966 const [ currentSubmissionId , setCurrentSubmissionId ] = useState <
6067 string | undefined
6168 > ( undefined ) ;
69+ const [ paginationParams , setPaginationParams ] = useState < TablePagination > ( {
70+ totalCount : 0 ,
71+ totalPages : 0 ,
72+ currentPage : 1 ,
73+ limit : 3 ,
74+ } ) ;
6275
6376 const state = EditorState . create ( {
6477 doc : "" ,
@@ -71,6 +84,33 @@ export default function QuestionPage() {
7184 ] ,
7285 } ) ;
7386
87+ // Handler for change in page jumper
88+ const onPageJump : PaginationProps [ "onChange" ] = ( pageNumber ) => {
89+ setPaginationParams ( ( prev ) => {
90+ loadQuestionHistories ( pageNumber , paginationParams . limit ) ;
91+ return { ...paginationParams , currentPage : pageNumber } ;
92+ } ) ;
93+ } ;
94+
95+ async function loadQuestionHistories ( currentPage : number , limit : number ) {
96+ if ( username === undefined ) return ;
97+ setIsHistoryLoading ( true ) ;
98+ GetUserQuestionHistories ( username , docRefId , currentPage , limit )
99+ . then ( ( data : any ) => {
100+ setUserQuestionHistories ( data . histories ) ;
101+ setPaginationParams ( {
102+ ...paginationParams ,
103+ totalCount : data . totalCount ,
104+ totalPages : data . totalPages ,
105+ currentPage : data . currentPage ,
106+ limit : data . limit ,
107+ } ) ;
108+ } )
109+ . finally ( ( ) => {
110+ setIsHistoryLoading ( false ) ;
111+ } ) ;
112+ }
113+
74114 // When code editor page is initialised, fetch the particular question, and display in code editor
75115 useEffect ( ( ) => {
76116 if ( ! isLoading ) {
@@ -90,15 +130,7 @@ export default function QuestionPage() {
90130 } , [ docRefId ] ) ;
91131
92132 useEffect ( ( ) => {
93- if ( username === undefined ) return ;
94- GetUserQuestionHistories ( username , docRefId )
95- . then ( ( data : any ) => {
96- console . log ( data ) ;
97- setUserQuestionHistories ( data ) ;
98- } )
99- . finally ( ( ) => {
100- setIsHistoryLoading ( false ) ;
101- } ) ;
133+ loadQuestionHistories ( paginationParams . currentPage , paginationParams . limit ) ;
102134 } , [ username ] ) ;
103135
104136 useEffect ( ( ) => {
@@ -108,15 +140,14 @@ export default function QuestionPage() {
108140 } , [ ] ) ;
109141
110142 useEffect ( ( ) => {
143+ // Only show history if a history is selected
111144 if ( currentSubmissionId === undefined ) return ;
112145
113146 const view = new EditorView ( {
114147 state,
115148 parent : editorRef . current || undefined ,
116149 } ) ;
117150
118- // TODO: get from a specific history which was selected.
119- // Show latest history by default, or load specific history
120151 GetHistory ( currentSubmissionId ) . then ( ( data : any ) => {
121152 const submittedAt = new Date ( data . createdAt ) ;
122153 setSubmission ( {
@@ -202,8 +233,14 @@ export default function QuestionPage() {
202233 style : { cursor : "pointer" } ,
203234 } ;
204235 } }
205- scroll = { { y : "max-content" } }
206236 loading = { isHistoryLoading }
237+ pagination = { {
238+ size : "small" ,
239+ current : paginationParams . currentPage ,
240+ total : paginationParams . totalCount ,
241+ pageSize : paginationParams . limit ,
242+ onChange : onPageJump ,
243+ } }
207244 />
208245 </ div >
209246 </ div >
0 commit comments