ApiErrorMessage.jsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. default:
  42. return (
  43. <strong><i className="icon-fw icon-ban"></i> Unknown error occured</strong>
  44. );
  45. }
  46. }
  47. if (errorCode != null) {
  48. return (
  49. <span className="text-danger">
  50. {renderMessageByErrorCode()}
  51. </span>
  52. );
  53. }
  54. if (errorMessage != null) {
  55. return (
  56. <span className="text-danger">
  57. {errorMessage}
  58. </span>
  59. );
  60. }
  61. // render null if no error has occurred
  62. return null;
  63. };
  64. ApiErrorMessage.propTypes = {
  65. t: PropTypes.func.isRequired, // i18next
  66. errorCode: PropTypes.string,
  67. errorMessage: PropTypes.string,
  68. targetPath: PropTypes.string,
  69. };
  70. export default withTranslation()(ApiErrorMessage);