Skip to content

Commit

Permalink
vinvoor: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Sep 10, 2024
1 parent 09af9cd commit ab8bc90
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 43 deletions.
6 changes: 3 additions & 3 deletions vinvoor/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "./themes/background.css";
import { useUser } from "./hooks/useUser";

export const App = () => {
const { data: user, isLoading, isError } = useUser();
const userQuery = useUser();
const outlet = useOutlet();

const [backgroundSix] = useState(() => randomInt(0, 50) === 1);
Expand All @@ -24,8 +24,8 @@ export const App = () => {
my: "2%",
}}
>
<LoadingSkeleton isLoading={isLoading} isError={isError}>
{user && Object.keys(user).length > 0 ? (
<LoadingSkeleton queries={[userQuery]}>
{Object.keys(userQuery.data ?? {}).length > 0 ? (
outlet !== null ? (
<Outlet />
) : (
Expand Down
6 changes: 3 additions & 3 deletions vinvoor/src/cards/Cards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { CardsEmpty } from "./CardsEmpty";
import { CardsTable } from "./CardsTable";

export const Cards = () => {
const { data: cards, isLoading, isError } = useCards();
const cardsQuery = useCards();

return (
<LoadingSkeleton isLoading={isLoading} isError={isError}>
{cards?.length ? <CardsTable /> : <CardsEmpty />}
<LoadingSkeleton queries={[cardsQuery]}>
{cardsQuery.data?.length ? <CardsTable /> : <CardsEmpty />}
</LoadingSkeleton>
);
};
16 changes: 11 additions & 5 deletions vinvoor/src/components/LoadingSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { Skeleton, SkeletonProps } from "@mui/material";
import { UseQueryResult } from "@tanstack/react-query";
import { FC, ReactNode } from "react";
import { isResponseNot200Error } from "../util/fetch";

interface LoadingSkeletonProps extends SkeletonProps {
isLoading: boolean;
isError: boolean;
queries: UseQueryResult<unknown, Error>[];
children: ReactNode;
}

export const LoadingSkeleton: FC<LoadingSkeletonProps> = ({
isLoading,
isError,
queries,
children,
...props
}) => {
const isError = queries.some(query => query.isError);
if (isError)
throw new Error("Error fetching data, unable to reach the server");
throw (
queries.find(query => isResponseNot200Error(query.error))?.error ??
new Error("Error fetching data, unable to reach the server")
);

const isLoading = queries.some(query => query.isLoading);

return isLoading ? <Skeleton {...props} /> : <>{children}</>;
};
4 changes: 2 additions & 2 deletions vinvoor/src/leaderboard/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { LeaderboardTableToolbar } from "./LeaderboardTableToolbar";
import { useLeaderboardItems } from "../hooks/useLeaderboard";

export const Leaderboard = () => {
const { isLoading, isError } = useLeaderboardItems();
const leaderboardQuery = useLeaderboardItems();

return (
<LoadingSkeleton isLoading={isLoading} isError={isError}>
<LoadingSkeleton queries={[leaderboardQuery]}>
<Paper elevation={4}>
<LeaderboardTableToolbar />
<Divider sx={{ borderColor: "primary.main", borderBottomWidth: 3 }} />
Expand Down
2 changes: 1 addition & 1 deletion vinvoor/src/navbar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const navBarPages: PageIcon[] = [
];

const userMenuPages: PageIcon[] = [
{ page: "Settings", icon: <CogOutline sx={{ mr: ".3rem" }} /> },
// { page: "Settings", icon: <CogOutline sx={{ mr: ".3rem" }} /> },
];

export const NavBar = () => {
Expand Down
6 changes: 3 additions & 3 deletions vinvoor/src/overview/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Streak } from "./streak/Streak";
import { useScans } from "../hooks/useScan";

export const Overview = () => {
const { data: scans, isLoading, isError } = useScans();
const scansQuery = useScans();
const [checked, setChecked] = useState<boolean>(false);
const daysRef = useRef<HTMLDivElement>(null);
const [paperHeight, setPaperHeight] = useState<number>(0);
Expand All @@ -27,8 +27,8 @@ export const Overview = () => {
});

return (
<LoadingSkeleton isLoading={isLoading} isError={isError}>
{scans?.length ? (
<LoadingSkeleton queries={[scansQuery]}>
{scansQuery.data?.length ? (
<Grid container spacing={2} justifyContent="space-between">
<Grid item xs={8} md={4} lg={3}>
<CheckIn />
Expand Down
9 changes: 3 additions & 6 deletions vinvoor/src/scans/Scans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import { useScans } from "../hooks/useScan";
import { useCards } from "../hooks/useCard";

export const Scans = () => {
const { isLoading: isLoadingScans, isError: isErrorScans } = useScans();
const { isLoading: isLoadingCards, isError: isErrorCards } = useCards();
const scansQuery = useScans();
const cardsQuery = useCards();

return (
<LoadingSkeleton
isLoading={isLoadingScans || isLoadingCards}
isError={isErrorScans || isErrorCards}
>
<LoadingSkeleton queries={[scansQuery, cardsQuery]}>
<Paper elevation={4}>
<TableContainer>
<Table>
Expand Down
4 changes: 2 additions & 2 deletions vinvoor/src/settings/SettingsOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useSettings } from "../hooks/useSettings";
import { Settings } from "./Settings";

export const SettingsOverview = () => {
const { isLoading, isError } = useSettings();
const settingsQuery = useSettings();

return (
<LoadingSkeleton isLoading={isLoading} isError={isError}>
<LoadingSkeleton queries={[settingsQuery]}>
<Settings />
</LoadingSkeleton>
);
Expand Down
4 changes: 2 additions & 2 deletions vinvoor/src/settings/admin/days/Days.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { DaysTable } from "./DaysTable";
import { useDays } from "../../../hooks/useDays";

export const Days = () => {
const { isLoading, isError } = useDays();
const daysQuery = useDays();

return (
<LoadingSkeleton isLoading={isLoading} isError={isError}>
<LoadingSkeleton queries={[daysQuery]}>
<Grid
container
justifyContent="space-between"
Expand Down
32 changes: 16 additions & 16 deletions vinvoor/src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ interface AdjustableSettings {
}

export const adjustableSettings: AdjustableSettings[] = [
{
id: "scanInOut",
name: "Scan in and out",
description:
"A second scan on the same day will be interpreted as a scan out",
},
{
id: "leaderboard",
name: "Leaderboard",
description: "Show yourself on the leaderboard",
},
{
id: "public",
name: "Public",
description: "Let others see you!",
},
// {
// id: "scanInOut",
// name: "Scan in and out",
// description:
// "A second scan on the same day will be interpreted as a scan out",
// },
// {
// id: "leaderboard",
// name: "Leaderboard",
// description: "Show yourself on the leaderboard",
// },
// {
// id: "public",
// name: "Public",
// description: "Let others see you!",
// },
];

0 comments on commit ab8bc90

Please sign in to comment.