Skip to content

Commit 2a50a47

Browse files
committed
fix: pagination and totals mock
1 parent 746791c commit 2a50a47

File tree

3 files changed

+141
-18
lines changed

3 files changed

+141
-18
lines changed

packages/main/plugins/Cardinality/TotalsPanel/CardinalityTotals.tsx

+75-13
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,45 @@
22
* Cardinality totals
33
* A view for the delete actions on the cardinality main view
44
*/
5-
import { useCallback, useState } from "react";
5+
import { useCallback, useEffect, useState } from "react";
66
import { totalsMock } from "../api/mock";
77
import { cx } from "@emotion/css";
88
import { PROCESS_HEADERS } from "./consts";
99
import { TotalRowStyle } from "./style";
1010
import useTheme from "@ui/theme/useTheme";
1111
import { TotalsRow } from "./TotalsRow";
12-
12+
import { getMaintenance, useMaintenance } from "./useMaintenance";
1313
import "./array_helper.mjs";
1414

1515
export default function CardinalityTotals({ isLoading }) {
16+
const Maintainance = useMaintenance();
17+
1618
const theme = useTheme();
17-
const [totals, setTotals] = useState(totalsMock);
19+
const [totals, setTotals] = useState(Maintainance ?? totalsMock);
1820
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]);
1941

2042
const sortByProperty = useCallback(
2143
(column: string) => {
22-
2344
const numberCols = [
2445
"series_created",
2546
"series_dropped",
@@ -30,12 +51,16 @@ export default function CardinalityTotals({ isLoading }) {
3051

3152
const columnName = column.split(" ").join("_").toLocaleLowerCase();
3253

33-
setTotals((prev) => {
34-
let prevCols = [...prev];
54+
setTotals(() => {
55+
let items = [...Maintainance];
56+
3557
if (numberCols.includes(columnName)) {
36-
return prevCols.sortColByNumber(columnName, sort);
58+
items.sortColByNumber(columnName, sort);
59+
return paginateTotals(items);
3760
}
38-
return prevCols.sortColByString(columnName, sort);
61+
62+
items.sortColByString(columnName, sort);
63+
return paginateTotals(items);
3964
});
4065

4166
setSort((prev) => (prev === "asc" ? "desc" : "asc"));
@@ -45,10 +70,10 @@ export default function CardinalityTotals({ isLoading }) {
4570

4671
return (
4772
<div className={cx(TotalRowStyle(theme))}>
48-
<div className="total-rows-header"
49-
>
73+
<div className="total-rows-header">
5074
Fingerprints in Maintainance mode
5175
</div>
76+
5277
<div className="table">
5378
<div className="table-header">
5479
{PROCESS_HEADERS?.map((header) => (
@@ -61,11 +86,12 @@ export default function CardinalityTotals({ isLoading }) {
6186
</div>
6287
))}
6388
</div>
64-
<>
89+
90+
<div className="table-body">
6591
{totals?.length ? (
6692
totals?.map((total, key) => (
6793
<TotalsRow
68-
key={key}
94+
key={key}
6995
isLoading={isLoading}
7096
headers={PROCESS_HEADERS}
7197
total={total}
@@ -74,8 +100,44 @@ export default function CardinalityTotals({ isLoading }) {
74100
) : (
75101
<> no totals </>
76102
)}
77-
</>
103+
</div>
78104
</div>
105+
<TotalsPagination
106+
page={page}
107+
totalPages={Maintainance.length / rowsPerPage}
108+
setPage={setPage}
109+
rowsPerPage={rowsPerPage}
110+
setRowsPerPage={setRowsPerPage}
111+
/>
79112
</div>
80113
);
81114
}
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+
};

packages/main/plugins/Cardinality/TotalsPanel/style.tsx

+40-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export const TotalRowStyle = (theme: QrynTheme) => css`
1313
border-radius: 3px;
1414
background: ${theme.shadow};
1515
}
16+
.table-container {
17+
display: flex;
18+
flex-direction: column;
19+
flex: 1;
20+
width: 100%;
21+
overflow-y: auto;
22+
max-height: 600px;
23+
}
1624
.table {
1725
display: table;
1826
width: 100%;
@@ -27,15 +35,20 @@ export const TotalRowStyle = (theme: QrynTheme) => css`
2735
letter-spacing: 1px;
2836
cursor: pointer;
2937
margin: 1px;
30-
padding-top:20px;
31-
padding-bottom:4px;
38+
padding-top: 20px;
39+
padding-bottom: 4px;
3240
&:hover {
3341
background: ${theme.deep};
3442
}
3543
}
3644
}
37-
.table-row {
45+
.table-body {
3846
display: table-row-group;
47+
max-height: 600px;
48+
overflow-y: auto;
49+
}
50+
.table-row {
51+
display: table-row;
3952
&:hover {
4053
background: ${theme.deep};
4154
}
@@ -47,14 +60,36 @@ export const TotalRowStyle = (theme: QrynTheme) => css`
4760
padding: 10px;
4861
max-width: 10%;
4962
}
63+
.table-footer {
64+
display: flex;
65+
font-size: 10px;
66+
text-transform: uppercase;
67+
align-items:center;
68+
gap: 4px;
69+
padding: 4px 0px;
70+
margin: 0px 4px;
71+
border-radius: 3px;
72+
background: ${theme.shadow};
73+
74+
button {
75+
padding: 4px 12px;
76+
color: white;
77+
border: none;
78+
background: ${theme.primary};
79+
border-radius: 3px;
80+
border-color: ${theme.primaryAccent};
81+
cursor: pointer;
82+
outline: none;
83+
}
84+
}
5085
5186
button {
5287
padding: 4px 12px;
5388
color: white;
5489
border: none;
55-
background: theme.primary;
90+
background: ${theme.primary};
5691
border-radius: 3px;
57-
border-color: theme.primaryDark;
92+
border-color: ${theme.primaryAccent};
5893
cursor: pointer;
5994
outline: none;
6095
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useEffect, useState } from "react";
2+
3+
// this is for test purposes
4+
export async function getMaintenance() {
5+
return await fetch("http://localhost:8081/api/v1/maintenance")
6+
}
7+
8+
9+
export function useMaintenance () {
10+
11+
const [maintenance, setMaintenance] = useState([]);
12+
13+
useEffect(() => {
14+
getMaintenance()
15+
.then((res) => res.json())
16+
.then((data) => {
17+
18+
setMaintenance(data);
19+
});
20+
},[]);
21+
22+
23+
24+
25+
return maintenance;
26+
}

0 commit comments

Comments
 (0)