ApiErrorMessage.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. const ApiErrorMessage = (props) => {
  5. const {
  6. t, errorCode, errorMessage, targetPath,
  7. } = props;
  8. function reload() {
  9. window.location.reload();
  10. }
  11. function renderMessageByErrorCode() {
  12. switch (errorCode) {
  13. case 'already_exists':
  14. return (
  15. <>
  16. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.already_exists') }</strong>
  17. <small><a href={targetPath}>{targetPath} <i className="icon-login"></i></a></small>
  18. </>
  19. );
  20. case 'notfound_or_forbidden':
  21. return (
  22. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.notfound_or_forbidden') }</strong>
  23. );
  24. case 'user_not_admin':
  25. return (
  26. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.user_not_admin') }</strong>
  27. );
  28. case 'outdated':
  29. return (
  30. <>
  31. <strong><i className="icon-fw icon-bulb"></i> { t('page_api_error.outdated') }</strong>
  32. <a className="btn-link" onClick={reload}>
  33. <i className="fa fa-angle-double-right"></i> { t('Load latest') }
  34. </a>
  35. </>
  36. );
  37. case 'invalid_path':
  38. return (
  39. <strong><i className="icon-fw icon-ban"></i> Invalid path</strong>
  40. );
  41. case 'unknown':
  42. return (
  43. <strong><i className="icon-fw icon-ban"></i> Unknown error occured</strong>
  44. );
  45. default:
  46. return null;
  47. }
  48. }
  49. if (errorCode != null) {
  50. return (
  51. <span className="text-danger">
  52. {renderMessageByErrorCode()}
  53. </span>
  54. );
  55. }
  56. if (errorMessage != null) {
  57. return (
  58. <span className="text-danger">
  59. {errorMessage}
  60. </span>
  61. );
  62. }
  63. // render null if no error has occurred
  64. return null;
  65. };
  66. ApiErrorMessage.propTypes = {
  67. t: PropTypes.func.isRequired, // i18next
  68. errorCode: PropTypes.string,
  69. errorMessage: PropTypes.string,
  70. targetPath: PropTypes.string,
  71. };
  72. export default withTranslation()(ApiErrorMessage);