Skip to content

Commit

Permalink
Don't reroute to 404 on error (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden authored Jun 17, 2024
1 parent dd8ccc6 commit f00e504
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
14 changes: 4 additions & 10 deletions frontend/src/pages/Robots.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ImageComponent from "components/files/ViewImage";
import { useAlertQueue } from "hooks/alerts";
import { api, Robot } from "hooks/api";
import { useAuthentication } from "hooks/auth";
import { useEffect, useState } from "react";
Expand All @@ -18,7 +19,7 @@ const Robots = () => {
const auth_api = new api(auth.api);
const [robotsData, setRobot] = useState<Robot[] | null>(null);
const [idMap, setIdMap] = useState<Map<string, string>>(new Map());
const [error, setError] = useState<string | null>(null);
const { addAlert } = useAlertQueue();
useEffect(() => {
const fetch_robots = async () => {
try {
Expand All @@ -31,23 +32,16 @@ const Robots = () => {
setIdMap(await auth_api.getUserBatch(Array.from(ids)));
} catch (err) {
if (err instanceof Error) {
setError(err.message);
addAlert(err.message, "error");
} else {
setError("An unexpected error occurred");
addAlert("An unexpected error occurred", "error");
}
}
};
fetch_robots();
}, []);
const navigate = useNavigate();

useEffect(() => {
if (error) {
console.log(error);
navigate("/404"); // Redirect to a 404 page
}
}, [error, navigate]);

if (!robotsData) {
return (
<Container
Expand Down
13 changes: 4 additions & 9 deletions frontend/src/pages/YourParts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ImageComponent from "components/files/ViewImage";
import { useAlertQueue } from "hooks/alerts";
import { api, Part } from "hooks/api";
import { useAuthentication } from "hooks/auth";
import { useEffect, useState } from "react";
Expand All @@ -17,17 +18,17 @@ const YourParts = () => {
const auth = useAuthentication();
const auth_api = new api(auth.api);
const [partsData, setParts] = useState<Part[] | null>(null);
const [error, setError] = useState<string | null>(null);
const { addAlert } = useAlertQueue();
useEffect(() => {
const fetch_parts = async () => {
try {
const partsQuery = await auth_api.getYourParts();
setParts(partsQuery);
} catch (err) {
if (err instanceof Error) {
setError(err.message);
addAlert(err.message, "error");
} else {
setError("An unexpected error occurred");
addAlert("An unexpected error occurred", "error");
}
}
};
Expand All @@ -36,12 +37,6 @@ const YourParts = () => {

const navigate = useNavigate();

useEffect(() => {
if (error) {
navigate("/404"); // Redirect to a 404 page
}
}, [error, navigate]);

if (!partsData) {
return (
<Container
Expand Down
14 changes: 4 additions & 10 deletions frontend/src/pages/YourRobots.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ImageComponent from "components/files/ViewImage";
import { useAlertQueue } from "hooks/alerts";
import { api, Robot } from "hooks/api";
import { useAuthentication } from "hooks/auth";
import { useEffect, useState } from "react";
Expand All @@ -17,7 +18,7 @@ const YourRobots = () => {
const auth = useAuthentication();
const auth_api = new api(auth.api);
const [robotsData, setRobot] = useState<Robot[] | null>(null);
const [error, setError] = useState<string | null>(null);
const { addAlert } = useAlertQueue();

useEffect(() => {
const fetch_your_robots = async () => {
Expand All @@ -26,23 +27,16 @@ const YourRobots = () => {
setRobot(robotsQuery);
} catch (err) {
if (err instanceof Error) {
setError(err.message);
addAlert(err.message, "error");
} else {
setError("An unexpected error occurred");
addAlert("An unexpected error occurred", "error");
}
}
};
fetch_your_robots();
}, []);
const navigate = useNavigate();

useEffect(() => {
if (error) {
console.log(error);
navigate("/404"); // Redirect to a 404 page
}
}, [error, navigate]);

if (!robotsData) {
return (
<Container
Expand Down

0 comments on commit f00e504

Please sign in to comment.