From 1419b20591b8845de7deca59e79aa7bf5363ae45 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Tue, 20 Aug 2024 17:27:07 -0400 Subject: [PATCH] Added action to change rows per page --- Client/src/Components/BasicTable/index.jsx | 99 ++++++++++++---------- Client/src/Features/UI/uiSlice.js | 6 +- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/Client/src/Components/BasicTable/index.jsx b/Client/src/Components/BasicTable/index.jsx index 435ce77bd..af71ee3a5 100644 --- a/Client/src/Components/BasicTable/index.jsx +++ b/Client/src/Components/BasicTable/index.jsx @@ -14,6 +14,8 @@ import { Typography, Stack, } from "@mui/material"; +import { useDispatch, useSelector } from "react-redux"; +import { setRowsPerPage } from "../../Features/UI/uiSlice"; import LeftArrowDouble from "../../assets/icons/left-arrow-double.svg?react"; import RightArrowDouble from "../../assets/icons/right-arrow-double.svg?react"; import LeftArrow from "../../assets/icons/left-arrow.svg?react"; @@ -142,8 +144,10 @@ const TablePaginationActions = (props) => { const BasicTable = ({ data, paginated, reversed, rows = 5 }) => { const theme = useTheme(); + const dispatch = useDispatch(); + const { table } = useSelector((state) => state.ui); + let rowsPerPage = table.rowsPerPage; const [page, setPage] = useState(0); - const [rowsPerPage, setRowsPerPage] = useState(rows); useEffect(() => { setPage(0); @@ -154,7 +158,7 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => { }; const handleChangeRowsPerPage = (event) => { - setRowsPerPage(parseInt(event.target.value, 10)); + dispatch(setRowsPerPage(parseInt(event.target.value, 10))); setPage(0); }; @@ -211,50 +215,59 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => { - {paginated && - - Showing {getRange()} of {data.rows.length} monitor(s) - - - `Page ${page + 1} of ${Math.max(0, Math.ceil(count / rowsPerPage))}` - } - slotProps={{ - select: { - MenuProps: { - keepMounted: true, - PaperProps: { - className: "pagination-dropdown", + {paginated && ( + + + Showing {getRange()} of {data.rows.length} monitor(s) + + + `Page ${page + 1} of ${Math.max( + 0, + Math.ceil(count / rowsPerPage) + )}` + } + slotProps={{ + select: { + MenuProps: { + keepMounted: true, + PaperProps: { + className: "pagination-dropdown", + }, + transformOrigin: { vertical: "bottom", horizontal: "left" }, + anchorOrigin: { vertical: "top", horizontal: "left" }, + sx: { mt: "-4px" }, }, - transformOrigin: { vertical: "bottom", horizontal: "left" }, - anchorOrigin: { vertical: "top", horizontal: "left" }, - sx: { mt: "-4px" }, - }, - inputProps: { id: "pagination-dropdown" }, - IconComponent: SelectorVertical, - sx: { - ml: theme.gap.small, - mr: theme.gap.large, - minWidth: theme.gap.xl, - textAlign: "left", - "&.Mui-focused > div": { - backgroundColor: theme.palette.otherColors.white, + inputProps: { id: "pagination-dropdown" }, + IconComponent: SelectorVertical, + sx: { + ml: theme.gap.small, + mr: theme.gap.large, + minWidth: theme.gap.xl, + textAlign: "left", + "&.Mui-focused > div": { + backgroundColor: theme.palette.otherColors.white, + }, }, }, - }, - }} - sx={{ mt: theme.gap.medium }} - /> - } + }} + sx={{ mt: theme.gap.medium }} + /> + + )} ); }; diff --git a/Client/src/Features/UI/uiSlice.js b/Client/src/Features/UI/uiSlice.js index 17523977a..a68492d5c 100644 --- a/Client/src/Features/UI/uiSlice.js +++ b/Client/src/Features/UI/uiSlice.js @@ -13,8 +13,8 @@ const uiSlice = createSlice({ name: "ui", initialState, reducers: { - setTablePreferences: (state, action) => { - state.table = { ...action }; + setRowsPerPage: (state, action) => { + state.table.rowsPerPage = action.payload; }, toggleSidebar: (state) => { state.sidebar.collapsed = !state.sidebar.collapsed; @@ -23,4 +23,4 @@ const uiSlice = createSlice({ }); export default uiSlice.reducer; -export const { setTablePreferences, toggleSidebar } = uiSlice.actions; +export const { setRowsPerPage, toggleSidebar } = uiSlice.actions;