import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; const ApiErrorMessage = (props) => { const { t, errorCode, errorMessage, targetPath, } = props; function reload() { window.location.reload(); } function renderMessageByErrorCode() { switch (errorCode) { case 'already_exists': return ( <> { t('page_api_error.already_exists') } {targetPath} ); case 'notfound_or_forbidden': return ( { t('page_api_error.notfound_or_forbidden') } ); case 'user_not_admin': return ( { t('page_api_error.user_not_admin') } ); case 'outdated': return ( <> { t('page_api_error.outdated') } { t('Load latest') } ); case 'invalid_path': return ( Invalid path ); default: return ( Unknown error occured ); } } if (errorCode != null) { return ( {renderMessageByErrorCode()} ); } if (errorMessage != null) { return ( {errorMessage} ); } // render null if no error has occurred return null; }; ApiErrorMessage.propTypes = { t: PropTypes.func.isRequired, // i18next errorCode: PropTypes.string, errorMessage: PropTypes.string, targetPath: PropTypes.string, }; export default withTranslation()(ApiErrorMessage);