feat: adding skeleton to table body

This commit is contained in:
Caio Cabral committed 2024-10-18 14:49:48 -04:00
1 parent 478a8b7f13
commit a517edfea3
3 files changed
+67 -31

No files matched your search

@@ -0,0 +1,31 @@
import { Skeleton, TableBody, TableCell, TableRow } from "@mui/material";
const ROWS_NUMBER = 7;
const ROWS_ARRAY = Array.from({ length: ROWS_NUMBER }, (_, i) => i);
const TableBodySkeleton = () => {
return (
<TableBody>
{ROWS_ARRAY.map((row) => (
<TableRow key={row}>
<TableCell>
<Skeleton />
</TableCell>
<TableCell>
<Skeleton />
</TableCell>
<TableCell>
<Skeleton />
</TableCell>
<TableCell>
<Skeleton />
</TableCell>
<TableCell>
<Skeleton />
</TableCell>
</TableRow>
))}
</TableBody>
);
};
export { TableBodySkeleton };
@@ -34,6 +34,7 @@ import RightArrow from "../../../../assets/icons/right-arrow.svg?react";
import SelectorVertical from "../../../../assets/icons/selector-vertical.svg?react"; import SelectorVertical from "../../../../assets/icons/selector-vertical.svg?react";
import ActionsMenu from "../actionsMenu"; import ActionsMenu from "../actionsMenu";
import useUtils from "../../utils"; import useUtils from "../../utils";
import { TableBodySkeleton } from "./Skeleton";
/** /**
* Component for pagination actions (first, previous, next, last). * Component for pagination actions (first, previous, next, last).
@@ -114,10 +115,10 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
const { determineState } = useUtils(); const { determineState } = useUtils();
const { rowsPerPage } = useSelector((state) => state.ui.monitors); const { rowsPerPage } = useSelector((state) => state.ui.monitors);
const authState = useSelector((state) => state.auth);
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [monitors, setMonitors] = useState([]); const [monitors, setMonitors] = useState([]);
const [monitorCount, setMonitorCount] = useState(0); const [monitorCount, setMonitorCount] = useState(0);
const authState = useSelector((state) => state.auth);
const [updateTrigger, setUpdateTrigger] = useState(false); const [updateTrigger, setUpdateTrigger] = useState(false);
const [sort, setSort] = useState({}); const [sort, setSort] = useState({});
const prevFilter = useRef(filter); const prevFilter = useRef(filter);
@@ -168,16 +169,7 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
useEffect(() => { useEffect(() => {
fetchPage(); fetchPage();
}, [ }, [updateTrigger, authState, page, rowsPerPage, filter, sort, setLoading, fetchPage]);
updateTrigger,
authState,
page,
rowsPerPage,
filter,
sort,
setLoading,
fetchPage,
]);
// Listen for changes in filter, if new value reset the page // Listen for changes in filter, if new value reset the page
useEffect(() => { useEffect(() => {
@@ -263,8 +255,7 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
Status Status
<span <span
style={{ style={{
visibility: visibility: sort.field === "status" ? "visible" : "hidden",
sort.field === "status" ? "visible" : "hidden",
}} }}
> >
{sort.order === "asc" ? ( {sort.order === "asc" ? (
@@ -280,6 +271,7 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
<TableCell>Actions</TableCell> <TableCell>Actions</TableCell>
</TableRow> </TableRow>
</TableHead> </TableHead>
{monitors.length > 0 ? (
<TableBody> <TableBody>
{monitors.map((monitor) => { {monitors.map((monitor) => {
let uptimePercentage = ""; let uptimePercentage = "";
@@ -324,7 +316,10 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
}} }}
> >
<TableCell> <TableCell>
<Host key={monitor._id} params={params} /> <Host
key={monitor._id}
params={params}
/>
</TableCell> </TableCell>
<TableCell> <TableCell>
<StatusLabel <StatusLabel
@@ -337,9 +332,7 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
<BarChart checks={monitor.checks.slice().reverse()} /> <BarChart checks={monitor.checks.slice().reverse()} />
</TableCell> </TableCell>
<TableCell> <TableCell>
<span style={{ textTransform: "uppercase" }}> <span style={{ textTransform: "uppercase" }}>{monitor.type}</span>
{monitor.type}
</span>
</TableCell> </TableCell>
<TableCell> <TableCell>
<ActionsMenu <ActionsMenu
@@ -352,6 +345,9 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
); );
})} })}
</TableBody> </TableBody>
) : (
<TableBodySkeleton />
)}
</Table> </Table>
</TableContainer> </TableContainer>
<Stack <Stack
@@ -360,7 +356,11 @@ const MonitorTable = ({ isAdmin, filter, setLoading }) => {
justifyContent="space-between" justifyContent="space-between"
px={theme.spacing(4)} px={theme.spacing(4)}
> >
<Typography px={theme.spacing(2)} variant="body2" sx={{ opacity: 0.7 }}> <Typography
px={theme.spacing(2)}
variant="body2"
sx={{ opacity: 0.7 }}
>
Showing {getRange()} of {monitorCount} monitor(s) Showing {getRange()} of {monitorCount} monitor(s)
</Typography> </Typography>
<TablePagination <TablePagination
+18 -13
View File
@@ -20,9 +20,11 @@ const Monitors = ({ isAdmin }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const monitorState = useSelector((state) => state.uptimeMonitors); const monitorState = useSelector((state) => state.uptimeMonitors);
const authState = useSelector((state) => state.auth); const authState = useSelector((state) => state.auth);
const dispatch = useDispatch({});
//TODO create components, and lower these states.
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [isSearching, setIsSearching] = useState(false); const [isSearching, setIsSearching] = useState(false);
const dispatch = useDispatch({});
const debouncedFilter = useDebounce(search, 500); const debouncedFilter = useDebounce(search, 500);
const handleSearch = (value) => { const handleSearch = (value) => {
@@ -34,13 +36,17 @@ const Monitors = ({ isAdmin }) => {
dispatch(getUptimeMonitorsByTeamId(authState.authToken)); dispatch(getUptimeMonitorsByTeamId(authState.authToken));
}, [authState.authToken, dispatch]); }, [authState.authToken, dispatch]);
console.log(monitorState); //Why are we tying loading to monitors length?
const loading = true; const loading =
/* monitorState?.isLoading && monitorState?.isLoading; /* && monitorState?.monitorsSummary?.monitors?.length === 0 */
monitorState?.monitorsSummary?.monitors?.length === 0; */
const canCreateMonitor =
isAdmin && monitorState?.monitorsSummary?.monitors?.length !== 0;
const hasMonitors = monitorState?.monitorsSummary?.monitors?.length !== 0;
const noMonitors = monitorState?.monitorsSummary?.monitors?.length === 0;
const canAddMonitor = isAdmin && hasMonitors;
const needsAdmin = !isAdmin && noMonitors;
/* console.log({ loading });
console.log({ monitorState }); */
return ( return (
<Stack <Stack
className="monitors" className="monitors"
@@ -56,7 +62,7 @@ const Monitors = ({ isAdmin }) => {
gap={theme.spacing(6)} gap={theme.spacing(6)}
> >
<Greeting type="uptime" /> <Greeting type="uptime" />
{canCreateMonitor && ( {canAddMonitor && (
<Button <Button
variant="contained" variant="contained"
color="primary" color="primary"
@@ -70,15 +76,13 @@ const Monitors = ({ isAdmin }) => {
)} )}
</Stack> </Stack>
</Box> </Box>
{noMonitors && <Fallback isAdmin={isAdmin} />}
{needsAdmin && <p>Wait for an admin to add some monitors!</p>}
{loading ? ( {loading ? (
<SkeletonLayout /> <SkeletonLayout />
) : ( ) : (
<> <>
{isAdmin && monitorState?.monitorsSummary?.monitors?.length === 0 && ( {hasMonitors && (
<Fallback isAdmin={isAdmin} />
)}
{monitorState?.monitorsSummary?.monitors?.length !== 0 && (
<> <>
<Stack <Stack
gap={theme.spacing(8)} gap={theme.spacing(8)}
@@ -127,6 +131,7 @@ const Monitors = ({ isAdmin }) => {
borderColor={theme.palette.border.light} borderColor={theme.palette.border.light}
backgroundColor={theme.palette.background.accent} backgroundColor={theme.palette.background.accent}
> >
{/* TODO maybe we dont need the conditional here, since we already check for monitors before */}
{monitorState?.monitorsSummary?.monitorCounts?.total || 0} {monitorState?.monitorsSummary?.monitorCounts?.total || 0}
</Box> </Box>
<Box <Box