|
| 1 | +import { useState } from 'react'; |
| 2 | +import { FallbackProps } from 'react-error-boundary'; |
| 3 | +import { getConfig } from '@edx/frontend-platform'; |
| 4 | +import { useIntl } from '@edx/frontend-platform/i18n'; |
| 5 | +import { |
| 6 | + Button, Container, Hyperlink, Row, |
| 7 | +} from '@openedx/paragon'; |
| 8 | +import { CustomErrors, ERROR_STATUS } from '@src/constants'; |
| 9 | + |
| 10 | +import messages from './messages'; |
| 11 | + |
| 12 | +const getErrorConfig = ({ errorMessage, errorStatus }) => { |
| 13 | + if (errorMessage === CustomErrors.NO_ACCESS || ERROR_STATUS.NO_ACCESS.includes(errorStatus)) { |
| 14 | + return ({ |
| 15 | + title: messages['error.page.title.noAccess'], |
| 16 | + description: messages['error.page.message.noAccess'], |
| 17 | + statusCode: errorStatus || ERROR_STATUS.NO_ACCESS[0], |
| 18 | + showBackButton: true, |
| 19 | + }); |
| 20 | + } |
| 21 | + if (errorMessage === CustomErrors.NOT_FOUND || ERROR_STATUS.NOT_FOUND.includes(errorStatus)) { |
| 22 | + return ({ |
| 23 | + title: messages['error.page.title.notFound'], |
| 24 | + description: messages['error.page.message.notFound'], |
| 25 | + statusCode: errorStatus || ERROR_STATUS.NOT_FOUND[0], |
| 26 | + showBackButton: true, |
| 27 | + }); |
| 28 | + } |
| 29 | + if (errorMessage === CustomErrors.SERVER_ERROR || ERROR_STATUS.SERVER_ERROR.includes(errorStatus)) { |
| 30 | + return ({ |
| 31 | + title: messages['error.page.title.server'], |
| 32 | + description: messages['error.page.message.server'], |
| 33 | + statusCode: errorStatus || ERROR_STATUS.SERVER_ERROR[0], |
| 34 | + showBackButton: true, |
| 35 | + showReloadButton: true, |
| 36 | + }); |
| 37 | + } |
| 38 | + return ({ |
| 39 | + title: messages['error.page.title.generic'], |
| 40 | + description: messages['error.page.message.generic'], |
| 41 | + showBackButton: true, |
| 42 | + showReloadButton: true, |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +const ErrorPage = ({ error, resetErrorBoundary }: FallbackProps) => { |
| 47 | + const intl = useIntl(); |
| 48 | + const [reloading, setReloading] = useState(false); |
| 49 | + |
| 50 | + const errorStatus: number = error?.customAttributes?.httpErrorStatus; |
| 51 | + const errorMessage: string = error?.message; |
| 52 | + const { |
| 53 | + title, description, statusCode, showBackButton, showReloadButton, |
| 54 | + } = getErrorConfig({ errorMessage, errorStatus }); |
| 55 | + |
| 56 | + const handleReload = () => { |
| 57 | + setReloading(true); |
| 58 | + resetErrorBoundary(); |
| 59 | + }; |
| 60 | + return ( |
| 61 | + <Container className="d-flex flex-column align-items-center justify-content-center min-vh-100 bg-light-200"> |
| 62 | + <h1 className="display-4 text-primary-200">{statusCode}</h1> |
| 63 | + <h1 className="text-primary">{intl.formatMessage(title)}</h1> |
| 64 | + <p>{intl.formatMessage(description)}</p> |
| 65 | + <Row> |
| 66 | + {showReloadButton && ( |
| 67 | + <Button |
| 68 | + className="m-2" |
| 69 | + disabled={reloading} |
| 70 | + onClick={handleReload} |
| 71 | + > |
| 72 | + {intl.formatMessage(messages['error.page.action.reload'])} |
| 73 | + </Button> |
| 74 | + )} |
| 75 | + {showBackButton && ( |
| 76 | + <Button |
| 77 | + as={Hyperlink} |
| 78 | + destination={`${getConfig().COURSE_AUTHORING_MICROFRONTEND_URL}/libraries`} |
| 79 | + className="m-2" |
| 80 | + variant={showReloadButton ? 'outline-primary' : 'primary'} |
| 81 | + > |
| 82 | + {intl.formatMessage(messages['error.page.action.back'])} |
| 83 | + </Button> |
| 84 | + )} |
| 85 | + |
| 86 | + </Row> |
| 87 | + </Container> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +const LibrariesErrorFallback = (props: FallbackProps) => <ErrorPage {...props} />; |
| 92 | +export default LibrariesErrorFallback; |
0 commit comments