1
- import React from "react" ;
1
+ import React , { useState , useEffect , useMemo } from "react" ;
2
2
import pad from "../assets/images/pad.svg" ;
3
3
import { useNavigate } from "react-router-dom" ;
4
4
5
- const ReportTable = ( { ReportName, List, actions } ) => {
5
+ const ReportTable = ( {
6
+ ReportName,
7
+ List,
8
+ actions,
9
+ setIsNextRecord,
10
+ isMoreDocs
11
+ } ) => {
6
12
const navigate = useNavigate ( ) ;
13
+ const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
14
+ const docPerPage = 10 ;
15
+ // const pageNumbers = [];
16
+
17
+ // For loop is used to calculate page numbers visible below table
18
+ // Initialize pageNumbers using useMemo to avoid unnecessary re-creation
19
+ const pageNumbers = useMemo ( ( ) => {
20
+ const calculatedPageNumbers = [ ] ;
21
+ for ( let i = 1 ; i <= Math . ceil ( List . length / docPerPage ) ; i ++ ) {
22
+ calculatedPageNumbers . push ( i ) ;
23
+ }
24
+ return calculatedPageNumbers ;
25
+ } , [ List ] ) ;
26
+ // below useEffect reset currenpage to 1 if user change route
27
+ useEffect ( ( ) => {
28
+ return ( ) => setCurrentPage ( 1 ) ;
29
+ } , [ ] ) ;
30
+
31
+ // `formatRow` is used to show data in poper manner like
32
+ // if data is of array type then it will join array items with ","
33
+ // if data is of object type then it Name values will be show in row
34
+ // if no data available it will show hyphen "-"
7
35
const formatRow = ( row ) => {
8
36
if ( Array . isArray ( row ) ) {
9
37
let updateArr = row . map ( ( x ) => x . Name ) ;
@@ -14,31 +42,35 @@ const ReportTable = ({ ReportName, List, actions }) => {
14
42
return "-" ;
15
43
}
16
44
} ;
45
+ // below useEffect is used to render next record if IsMoreDoc is true
46
+ // second last value of pageNumber array is same as currentPage
47
+ useEffect ( ( ) => {
48
+ if ( isMoreDocs && pageNumbers [ pageNumbers . length - 1 ] === currentPage ) {
49
+ setIsNextRecord ( true ) ;
50
+ }
51
+ } , [ isMoreDocs , pageNumbers , currentPage , setIsNextRecord ] ) ;
52
+
53
+ // `handlemicroapp` is used to open microapp
17
54
const handlemicroapp = ( item , url ) => {
18
55
localStorage . removeItem ( "rowlevel" ) ;
19
- // const params = new URLSearchParams(url);
20
- // let arr = [];
21
- // for (let [key, value] of params.entries()) {
22
- // if (key === "remoteUrl") {
23
- // arr.push(
24
- // `${key}=${window.btoa(
25
- // window.location.origin + "/mfbuild/remoteEntry.js"
26
- // )}`
27
- // );
28
- // } else {
29
- // arr.push(`${key}=${value}`);
30
- // }
31
- // }
32
- // const remoteUrl = arr.join("&");
33
- // console.log("arr", remoteUrl);
34
-
35
56
navigate ( "/rpmf/" + url ) ;
36
57
localStorage . setItem ( "rowlevel" , JSON . stringify ( item ) ) ;
37
58
// localStorage.setItem("rowlevelMicro");
38
59
} ;
39
60
const handlebtn = ( ) => {
40
61
console . log ( "clicked" ) ;
41
62
} ;
63
+
64
+ // Get current list
65
+ const indexOfLastDoc = currentPage * docPerPage ;
66
+ const indexOfFirstDoc = indexOfLastDoc - docPerPage ;
67
+ // `currentLists` is total record render on current page
68
+ const currentLists = List . slice ( indexOfFirstDoc , indexOfLastDoc ) ;
69
+
70
+ // Change page
71
+ const paginateFront = ( ) => setCurrentPage ( currentPage + 1 ) ;
72
+ const paginateBack = ( ) => setCurrentPage ( currentPage - 1 ) ;
73
+
42
74
return (
43
75
< div className = "p-2 overflow-x-scroll w-full bg-white rounded-md" >
44
76
< h2 className = "text-[23px] font-light my-2" > { ReportName } </ h2 >
@@ -60,7 +92,7 @@ const ReportTable = ({ ReportName, List, actions }) => {
60
92
< tbody className = "text-[12px]" >
61
93
{ List ?. length > 0 ? (
62
94
< >
63
- { List . map ( ( item , index ) => (
95
+ { currentLists . map ( ( item , index ) => (
64
96
< tr className = "border-t-[1px]" key = { index } >
65
97
< td className = "px-4 py-2" > { index + 1 } </ td >
66
98
< td className = "px-4 py-2 font-semibold" > { item ?. Name } </ td >
@@ -121,8 +153,46 @@ const ReportTable = ({ ReportName, List, actions }) => {
121
153
) }
122
154
</ tbody >
123
155
</ table >
156
+ < div className = "flex flex-wrap items-center gap-2 p-2 border-t-[1px]" >
157
+ { List . length > 10 && (
158
+ < >
159
+ { currentPage > 1 && (
160
+ < button
161
+ onClick = { ( ) => paginateBack ( ) }
162
+ className = "bg-blue-400 text-white rounded px-3 py-2 focus:outline-none"
163
+ >
164
+ Prev
165
+ </ button >
166
+ ) }
167
+ </ >
168
+ ) }
169
+ { pageNumbers . map ( ( x ) => (
170
+ < button
171
+ key = { x }
172
+ onClick = { ( ) => setCurrentPage ( x ) }
173
+ className = " bg-sky-400 text-white rounded px-3 py-2 focus:outline-none"
174
+ >
175
+ { x }
176
+ </ button >
177
+ ) ) }
178
+ { isMoreDocs && (
179
+ < button className = "text-black rounded px-1 py-2" > ...</ button >
180
+ ) }
181
+ { List . length > 10 && (
182
+ < >
183
+ { pageNumbers . includes ( currentPage + 1 ) && (
184
+ < button
185
+ onClick = { ( ) => paginateFront ( ) }
186
+ className = "bg-blue-400 text-white rounded px-3 py-2 focus:outline-none"
187
+ >
188
+ Next
189
+ </ button >
190
+ ) }
191
+ </ >
192
+ ) }
193
+ </ div >
124
194
{ List ?. length <= 0 && (
125
- < div className = "flex flex-col items-center justify-center w-full bg-white roundedm py-4" >
195
+ < div className = "flex flex-col items-center justify-center w-full bg-white rounded py-4" >
126
196
< div className = "w-[60px] h-[60px] overflow-hidden" >
127
197
< img className = "w-full h-full object-contain" src = { pad } alt = "img" />
128
198
</ div >
0 commit comments