fix pagespeed loading states, add network error fallback
This commit is contained in:
1 parent
460a597e12
commit
49c58ce2ae
2 files changed
+21
-5
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { networkService } from "../../../../main";
|
import { networkService } from "../../../../main";
|
||||||
import { logger } from "../../../../Utils/Logger";
|
import { createToast } from "../../../../Utils/toastUtils";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
const useMonitorFetch = ({ authToken, monitorId }) => {
|
const useMonitorFetch = ({ authToken, monitorId }) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -8,10 +8,10 @@ const useMonitorFetch = ({ authToken, monitorId }) => {
|
|||||||
const [monitor, setMonitor] = useState(undefined);
|
const [monitor, setMonitor] = useState(undefined);
|
||||||
const [audits, setAudits] = useState(undefined);
|
const [audits, setAudits] = useState(undefined);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchMonitor = async () => {
|
const fetchMonitor = async () => {
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
|
||||||
const res = await networkService.getStatsByMonitorId({
|
const res = await networkService.getStatsByMonitorId({
|
||||||
authToken: authToken,
|
authToken: authToken,
|
||||||
monitorId: monitorId,
|
monitorId: monitorId,
|
||||||
@@ -24,8 +24,8 @@ const useMonitorFetch = ({ authToken, monitorId }) => {
|
|||||||
setMonitor(res?.data?.data ?? undefined);
|
setMonitor(res?.data?.data ?? undefined);
|
||||||
setAudits(res?.data?.data?.checks?.[0]?.audits ?? undefined);
|
setAudits(res?.data?.data?.checks?.[0]?.audits ?? undefined);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(logger);
|
setNetworkError(true);
|
||||||
navigate("/not-found", { replace: true });
|
createToast({ body: error.message });
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const PageSpeedDetails = () => {
|
|||||||
const { monitorId } = useParams();
|
const { monitorId } = useParams();
|
||||||
const { authToken } = useSelector((state) => state.auth);
|
const { authToken } = useSelector((state) => state.auth);
|
||||||
|
|
||||||
const { monitor, audits, isLoading } = useMonitorFetch({
|
const { monitor, audits, isLoading, networkError } = useMonitorFetch({
|
||||||
authToken,
|
authToken,
|
||||||
monitorId,
|
monitorId,
|
||||||
});
|
});
|
||||||
@@ -39,10 +39,26 @@ const PageSpeedDetails = () => {
|
|||||||
seo: true,
|
seo: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handlers
|
||||||
const handleMetrics = (id) => {
|
const handleMetrics = (id) => {
|
||||||
setMetrics((prev) => ({ ...prev, [id]: !prev[id] }));
|
setMetrics((prev) => ({ ...prev, [id]: !prev[id] }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (networkError === true) {
|
||||||
|
return (
|
||||||
|
<GenericFallback>
|
||||||
|
<Typography
|
||||||
|
variant="h1"
|
||||||
|
marginY={theme.spacing(4)}
|
||||||
|
color={theme.palette.primary.contrastTextTertiary}
|
||||||
|
>
|
||||||
|
Network error
|
||||||
|
</Typography>
|
||||||
|
<Typography>Please check your connection</Typography>
|
||||||
|
</GenericFallback>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Empty view, displayed when loading is complete and there are no checks
|
// Empty view, displayed when loading is complete and there are no checks
|
||||||
if (!isLoading && monitor?.checks?.length === 0) {
|
if (!isLoading && monitor?.checks?.length === 0) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in new issue
Block a user