bananas-commerce-admin
Version:
What's this, an admin for apes?
59 lines • 3.3 kB
JavaScript
import React from "react";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Card from "@mui/material/Card";
import CardActions from "@mui/material/CardActions";
import CardContent from "@mui/material/CardContent";
import { useTheme } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import { ApiLoadFailedError } from "../api";
import { useI18n } from "../contexts/I18nContext";
import { useUser } from "../contexts/UserContext";
import { PageLoadFailedError } from "./PageLoader";
const errorMessages = {
403: "You are authenticated as %(username)s, but are not authorized to access this page. Would you like to login to a different account?",
404: "We're sorry, but the requested page could not be found.",
};
const GenericErrorScreen = ({ header, description, children, }) => {
const theme = useTheme();
return (React.createElement(Box, { sx: {
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100%",
padding: 3,
} },
React.createElement(Card, { sx: {
boxShadow: 0,
borderRadius: 3,
borderWidth: "1px",
borderStyle: "solid",
borderColor: theme.palette.divider,
maxWidth: 800,
} },
React.createElement(CardContent, { sx: { ":last-child": { padding: 2 } } },
React.createElement(Typography, { variant: "h6" }, header),
description && React.createElement(Typography, null, description)),
children && React.createElement(CardActions, { sx: { px: 2, pb: 2 } }, children))));
};
const ErrorScreen = ({ error }) => {
if (error instanceof ApiLoadFailedError) {
return (React.createElement(GenericErrorScreen, { description: "There was an error loading the API schema. This may mean that the server is down or that your connection is bad. Please try again later and contact support if this problem persists.", header: "Failed to load essential data" },
React.createElement(Button, { color: "secondary", sx: { ml: "auto" }, variant: "contained", onClick: () => location.reload() }, "Reload")));
}
if (error instanceof Error) {
return React.createElement(OnlineErrorScreen, { error: error });
}
return React.createElement(OnlineErrorScreen, { error: new Error(error) });
};
const OnlineErrorScreen = ({ error }) => {
const { t } = useI18n();
const { user, logout } = useUser();
return (React.createElement(GenericErrorScreen, { description: error instanceof PageLoadFailedError
? t(error.response.status >= 500
? "There's been an error. It's been reported to the site administrators via email and should be fixed shortly. Thanks for your patience."
: errorMessages[error.response.status], user)
: undefined, header: `${error.name}: ${error.message}` }, error instanceof PageLoadFailedError && error.response.status === 403 && (React.createElement(Button, { color: "secondary", sx: { ml: "auto" }, variant: "contained", onClick: () => logout() }, t("Log in again")))));
};
export default ErrorScreen;
//# sourceMappingURL=ErrorScreen.js.map