ApiErrorMessage.jsx 2.0 KB

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