Skip to content

Commit

Permalink
finish failing gracefully for all pages
Browse files Browse the repository at this point in the history
also refactor out isFulfilled core logic
  • Loading branch information
chennisden committed Jun 10, 2024
1 parent 00f1959 commit ffd51f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
8 changes: 1 addition & 7 deletions frontend/src/pages/Parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useAuthentication } from "hooks/auth";
import { useEffect, useState } from "react";
import { Breadcrumb, Card, Col, Row, Spinner } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
import { isFulfilled } from "utils/isfullfiled";

const Parts = () => {
const auth = useAuthentication();
Expand All @@ -13,13 +14,6 @@ const Parts = () => {
const [error, setError] = useState<string | null>(null);
const { addAlert } = useAlertQueue();

// Type guard to check if a result is a fulfilled result
function isFulfilled<T>(
result: PromiseSettledResult<T>,
): result is PromiseFulfilledResult<T> {
return result.status === "fulfilled" && result.value != null;
}

useEffect(() => {
const fetch_parts = async () => {
try {
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/pages/RobotDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
} from "react-bootstrap";
import Markdown from "react-markdown";
import { Link, useNavigate, useParams } from "react-router-dom";
import { useAlertQueue } from "hooks/alerts";
import { isFulfilled } from "utils/isfullfiled";

interface RobotDetailsResponse {
name: string;
Expand All @@ -29,6 +31,7 @@ interface ExtendedBom {
}

const RobotDetails = () => {
const { addAlert } = useAlertQueue();
const auth = useAuthentication();
const auth_api = new api(auth.api);
const [userId, setUserId] = useState<string | null>(null);
Expand Down Expand Up @@ -61,7 +64,7 @@ const RobotDetails = () => {
quantity: part.quantity,
};
});
setParts(await Promise.all(parts));
setParts((await Promise.allSettled(parts)).filter(isFulfilled).map((result) => result.value as ExtendedBom));
} catch (err) {
if (err instanceof Error) {
setError(err.message);
Expand All @@ -85,9 +88,9 @@ const RobotDetails = () => {

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

if (!robot) {
return <Spinner animation="border" />;
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/pages/Robots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useAuthentication } from "hooks/auth";
import { useEffect, useState } from "react";
import { Breadcrumb, Card, Col, Row, Spinner } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
import { isFulfilled } from "utils/isfullfiled";

const Robots = () => {
const auth = useAuthentication();
Expand All @@ -19,12 +20,12 @@ const Robots = () => {
robotsQuery.forEach((robot) => {
ids.add(robot.owner);
});
const idMap = await Promise.all(
const idMap = await Promise.allSettled(
Array.from(ids).map(async (id) => {
return [id, await auth_api.getUserById(id)];
}),
);
setIdMap(new Map(idMap.map(([key, value]) => [key, value])));
setIdMap(new Map(idMap.filter(isFulfilled).map((result) => result.value as [string, string])));
} catch (err) {
if (err instanceof Error) {
setError(err.message);
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/utils/isfullfiled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Type guard to check if a result is a fulfilled result
export function isFulfilled<T>(
result: PromiseSettledResult<T>,
): result is PromiseFulfilledResult<T> {
return result.status === "fulfilled" && result.value != null;
}

0 comments on commit ffd51f3

Please sign in to comment.