iniital refactor

This commit is contained in:
Alex Holliday committed 2024-08-08 14:56:26 -07:00
1 parent b4dcccf6f0
commit 60996da401
1 file changed
+6 -54
+6 -54
View File
@@ -42,6 +42,7 @@ const Incidents = () => {
const theme = useTheme(); const theme = useTheme();
const navigate = useNavigate(); const navigate = useNavigate();
const authState = useSelector((state) => state.auth); const authState = useSelector((state) => state.auth);
const [monitors, setMonitors] = useState({}); const [monitors, setMonitors] = useState({});
const [selectedMonitor, setSelectedMonitor] = useState("0"); const [selectedMonitor, setSelectedMonitor] = useState("0");
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@@ -49,21 +50,11 @@ const Incidents = () => {
// TODO do something with these filters // TODO do something with these filters
const [filter, setFilter] = useState("all"); const [filter, setFilter] = useState("all");
let data = {
cols: [
{ id: 1, name: "Status" },
{ id: 2, name: "Timestamp" },
{ id: 3, name: "Monitor" },
{ id: 4, name: "Message" },
],
rows: [],
};
useEffect(() => { useEffect(() => {
const fetchIncidents = async () => { const fetchMonitors = async () => {
setLoading(true); setLoading(true);
const res = await axiosInstance.get( const res = await axiosInstance.get(
`/monitors/user/${authState.user._id}?status=false`, `/monitors/user/${authState.user._id}?status=false&limit=1`,
{ {
headers: { headers: {
Authorization: `Bearer ${authState.authToken}`, Authorization: `Bearer ${authState.authToken}`,
@@ -82,58 +73,19 @@ const Incidents = () => {
setLoading(false); setLoading(false);
}; };
fetchIncidents(); fetchMonitors();
}, [authState]); }, [authState]);
const handleSelect = (event) => { const handleSelect = (event) => {
setSelectedMonitor(event.target.value); setSelectedMonitor(event.target.value);
}; };
let incidents = [];
let hasAnyIncidents = false;
let hasSpecificIncidents = false;
if (selectedMonitor === "0") {
Object.values(monitors).forEach((monitor) => {
incidents = incidents.concat(monitor.checks);
});
if (incidents.length !== 0) hasAnyIncidents = true;
hasSpecificIncidents = hasAnyIncidents;
} else {
const monitor = monitors[selectedMonitor];
incidents = monitor.checks;
if (incidents.length !== 0) hasSpecificIncidents = true;
hasAnyIncidents = true;
}
incidents = incidents
.sort((a, b) => {
return new Date(b.createdAt) - new Date(a.createdAt);
})
.filter((incident) => {
if (filter === "all") return true;
else if (filter === "resolve") return incident.statusCode === 5000;
else if (filter === "down") return incident.status === false;
})
.map((incident, idx) => {
return {
id: idx,
data: [
{
id: idx + 0,
data: <StatusLabel status="down" text="Down" />,
},
{ id: idx + 1, data: new Date(incident.createdAt).toLocaleString() },
{ id: idx + 2, data: monitors[incident.monitorId].name },
{ id: idx + 3, data: incident.statusCode },
],
};
});
data.rows = incidents;
return ( return (
<> <>
<Stack className="incidents" gap={theme.gap.large}> <Stack className="incidents" gap={theme.gap.large}>
{loading ? ( {loading ? (
<SkeletonLayout /> <SkeletonLayout />
) : hasAnyIncidents ? ( ) : false ? (
<> <>
<Stack direction="row" alignItems="center" gap={theme.gap.medium}> <Stack direction="row" alignItems="center" gap={theme.gap.medium}>
<Typography component="h1">Incident history for: </Typography> <Typography component="h1">Incident history for: </Typography>
@@ -178,7 +130,7 @@ const Incidents = () => {
)} )}
</Stack> </Stack>
{hasAnyIncidents && hasSpecificIncidents ? ( {hasAnyIncidents && hasSpecificIncidents ? (
<BasicTable data={data} paginated={true} rowsPerPage={12} /> <div>thing</div>
) : ( ) : (
<Typography alignSelf="center" mt={theme.gap.xxl}> <Typography alignSelf="center" mt={theme.gap.xxl}>
The monitor you have selected, has no recorded incidents yet. The monitor you have selected, has no recorded incidents yet.