2
2
* Cardinality totals
3
3
* A view for the delete actions on the cardinality main view
4
4
*/
5
- import { useCallback , useState } from "react" ;
5
+ import { useCallback , useEffect , useState } from "react" ;
6
6
import { totalsMock } from "../api/mock" ;
7
7
import { cx } from "@emotion/css" ;
8
8
import { PROCESS_HEADERS } from "./consts" ;
9
9
import { TotalRowStyle } from "./style" ;
10
10
import useTheme from "@ui/theme/useTheme" ;
11
11
import { TotalsRow } from "./TotalsRow" ;
12
-
12
+ import { getMaintenance , useMaintenance } from "./useMaintenance" ;
13
13
import "./array_helper.mjs" ;
14
14
15
15
export default function CardinalityTotals ( { isLoading } ) {
16
+ const Maintainance = useMaintenance ( ) ;
17
+
16
18
const theme = useTheme ( ) ;
17
- const [ totals , setTotals ] = useState ( totalsMock ) ;
19
+ const [ totals , setTotals ] = useState ( Maintainance ?? totalsMock ) ;
18
20
const [ sort , setSort ] = useState ( "asc" ) ;
21
+ const [ page , setPage ] = useState ( 0 ) ;
22
+ const [ rowsPerPage , setRowsPerPage ] = useState ( 10 ) ;
23
+
24
+ function paginateTotals ( data ) {
25
+ const startIndex = page * rowsPerPage ;
26
+ const endIndex = startIndex + rowsPerPage ;
27
+ return data . slice ( startIndex , endIndex ) ;
28
+ }
29
+
30
+ useEffect ( ( ) => {
31
+ getMaintenance ( )
32
+ . then ( ( res ) => res . json ( ) )
33
+ . then ( ( data ) => {
34
+ setTotals ( paginateTotals ( data ) ) ;
35
+ } ) ;
36
+ } , [ ] ) ;
37
+
38
+ useEffect ( ( ) => {
39
+ setTotals ( paginateTotals ( Maintainance ) ) ;
40
+ } , [ page ] ) ;
19
41
20
42
const sortByProperty = useCallback (
21
43
( column : string ) => {
22
-
23
44
const numberCols = [
24
45
"series_created" ,
25
46
"series_dropped" ,
@@ -30,12 +51,16 @@ export default function CardinalityTotals({ isLoading }) {
30
51
31
52
const columnName = column . split ( " " ) . join ( "_" ) . toLocaleLowerCase ( ) ;
32
53
33
- setTotals ( ( prev ) => {
34
- let prevCols = [ ...prev ] ;
54
+ setTotals ( ( ) => {
55
+ let items = [ ...Maintainance ] ;
56
+
35
57
if ( numberCols . includes ( columnName ) ) {
36
- return prevCols . sortColByNumber ( columnName , sort ) ;
58
+ items . sortColByNumber ( columnName , sort ) ;
59
+ return paginateTotals ( items ) ;
37
60
}
38
- return prevCols . sortColByString ( columnName , sort ) ;
61
+
62
+ items . sortColByString ( columnName , sort ) ;
63
+ return paginateTotals ( items ) ;
39
64
} ) ;
40
65
41
66
setSort ( ( prev ) => ( prev === "asc" ? "desc" : "asc" ) ) ;
@@ -45,10 +70,10 @@ export default function CardinalityTotals({ isLoading }) {
45
70
46
71
return (
47
72
< div className = { cx ( TotalRowStyle ( theme ) ) } >
48
- < div className = "total-rows-header"
49
- >
73
+ < div className = "total-rows-header" >
50
74
Fingerprints in Maintainance mode
51
75
</ div >
76
+
52
77
< div className = "table" >
53
78
< div className = "table-header" >
54
79
{ PROCESS_HEADERS ?. map ( ( header ) => (
@@ -61,11 +86,12 @@ export default function CardinalityTotals({ isLoading }) {
61
86
</ div >
62
87
) ) }
63
88
</ div >
64
- < >
89
+
90
+ < div className = "table-body" >
65
91
{ totals ?. length ? (
66
92
totals ?. map ( ( total , key ) => (
67
93
< TotalsRow
68
- key = { key }
94
+ key = { key }
69
95
isLoading = { isLoading }
70
96
headers = { PROCESS_HEADERS }
71
97
total = { total }
@@ -74,8 +100,44 @@ export default function CardinalityTotals({ isLoading }) {
74
100
) : (
75
101
< > no totals </ >
76
102
) }
77
- </ >
103
+ </ div >
78
104
</ div >
105
+ < TotalsPagination
106
+ page = { page }
107
+ totalPages = { Maintainance . length / rowsPerPage }
108
+ setPage = { setPage }
109
+ rowsPerPage = { rowsPerPage }
110
+ setRowsPerPage = { setRowsPerPage }
111
+ />
79
112
</ div >
80
113
) ;
81
114
}
115
+
116
+ export const TotalsPagination = ( {
117
+ page,
118
+ totalPages,
119
+ setPage,
120
+ rowsPerPage,
121
+ setRowsPerPage,
122
+ } ) => {
123
+ return (
124
+ < div className = "table-footer" >
125
+ < button onClick = { ( ) => setPage ( ( ) => 0 ) } > First</ button >
126
+ < button onClick = { ( ) => setPage ( ( ) => Math . max ( 0 , page - 1 ) ) } >
127
+ Prev
128
+ </ button >
129
+ < button
130
+ onClick = { ( ) =>
131
+ setPage ( ( ) => Math . min ( totalPages - 1 , page + 1 ) )
132
+ }
133
+ >
134
+ Next
135
+ </ button >
136
+ < button onClick = { ( ) => setPage ( totalPages - 1 ) } > Last</ button >
137
+
138
+ < p >
139
+ Page { page + 1 } of { totalPages }
140
+ </ p >
141
+ </ div >
142
+ ) ;
143
+ } ;
0 commit comments