ApiErrorMessage.jsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  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. case 'single_deletion_empty_pages':
  43. return (
  44. <strong><i className="icon-fw icon-ban"></i>{ t('page_api_error.single_deletion_empty_pages') }</strong>
  45. );
  46. default:
  47. return (
  48. <strong><i className="icon-fw icon-ban"></i> Unknown error occured</strong>
  49. );
  50. }
  51. }
  52. if (errorCode != null) {
  53. return (
  54. <span className="text-danger">
  55. {renderMessageByErrorCode()}
  56. </span>
  57. );
  58. }
  59. if (errorMessage != null) {
  60. return (
  61. <span className="text-danger">
  62. {errorMessage}
  63. </span>
  64. );
  65. }
  66. // render null if no error has occurred
  67. return null;
  68. };
  69. ApiErrorMessage.propTypes = {
  70. errorCode: PropTypes.string,
  71. errorMessage: PropTypes.string,
  72. targetPath: PropTypes.string,
  73. };
  74. export default ApiErrorMessage;