TrashPageAlert.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { toastError } from '../../util/apiNotification';
  5. import { createSubscribedElement } from '../UnstatedUtils';
  6. import AppContainer from '../../services/AppContainer';
  7. import PageContainer from '../../services/PageContainer';
  8. import UserPicture from '../User/UserPicture';
  9. import EmptyTrashModal from '../EmptyTrashModal';
  10. import PageDeleteModal from '../PageDeleteModal';
  11. const TrashPageAlert = (props) => {
  12. const { t, appContainer, pageContainer } = props;
  13. const {
  14. path, isDeleted, revisionAuthor, updatedAt, hasChildren, isAbleToDeleteCompletely,
  15. } = pageContainer.state;
  16. const { currentUser } = appContainer;
  17. const [isEmptyTrashModalShown, setIsEmptyTrashModalShown] = useState(false);
  18. const [isPageDeleteModalShown, setIsPageDeleteModalShown] = useState(false);
  19. function openEmptyTrashModal() {
  20. setIsEmptyTrashModalShown(true);
  21. }
  22. function closeEmptyTrashModal() {
  23. setIsEmptyTrashModalShown(false);
  24. }
  25. function openPageDeleteModal() {
  26. setIsPageDeleteModalShown(true);
  27. }
  28. function closePageDeleteModal() {
  29. setIsPageDeleteModalShown(false);
  30. }
  31. async function onClickEmptyBtn() {
  32. try {
  33. await appContainer.apiv3Delete('/pages/empty-trash');
  34. window.location.reload();
  35. }
  36. catch (err) {
  37. toastError(err);
  38. }
  39. }
  40. async function onClickDeleteBtn(recursively, completely) {
  41. console.log(completely);
  42. console.log(recursively);
  43. }
  44. function renderEmptyButton() {
  45. return (
  46. <button
  47. href="#"
  48. type="button"
  49. className="btn btn-danger rounded-pill btn-sm ml-auto"
  50. data-target="#emptyTrash"
  51. onClick={openEmptyTrashModal}
  52. >
  53. <i className="icon-trash" aria-hidden="true"></i>{ t('modal_empty.empty_the_trash') }
  54. </button>
  55. );
  56. }
  57. function renderTrashPageManagementButtons() {
  58. return (
  59. <>
  60. <button
  61. type="button"
  62. className="btn btn-outline-secondary rounded-pill btn-sm ml-auto mr-2"
  63. data-target="#putBackPage"
  64. data-toggle="modal"
  65. >
  66. <i className="icon-action-undo" aria-hidden="true"></i> { t('Put Back') }
  67. </button>
  68. <button
  69. type="button"
  70. className="btn btn-danger rounded-pill btn-sm mr-2"
  71. disabled={!isAbleToDeleteCompletely}
  72. onClick={openPageDeleteModal}
  73. >
  74. <i className="icon-fire" aria-hidden="true"></i> { t('Delete Completely') }
  75. </button>
  76. </>
  77. );
  78. }
  79. return (
  80. <>
  81. <div className="alert alert-warning py-3 px-4 d-flex align-items-center">
  82. <div>
  83. This page is in the trash <i className="icon-trash" aria-hidden="true"></i>.
  84. {isDeleted && <span><br /><UserPicture user={revisionAuthor} /> Deleted by {revisionAuthor.name} at {updatedAt}</span>}
  85. </div>
  86. {(currentUser.admin && path === '/trash' && hasChildren) && renderEmptyButton()}
  87. {(isDeleted && currentUser != null) && renderTrashPageManagementButtons()}
  88. </div>
  89. <EmptyTrashModal isOpen={isEmptyTrashModalShown} toggle={closeEmptyTrashModal} onClickSubmit={onClickEmptyBtn} />
  90. <PageDeleteModal
  91. isOpen={isPageDeleteModalShown}
  92. toggle={closePageDeleteModal}
  93. onClickSubmit={onClickDeleteBtn}
  94. path={path}
  95. isDeleteCompletelyModal
  96. isAbleToDeleteCompletely={isAbleToDeleteCompletely}
  97. />
  98. </>
  99. );
  100. };
  101. /**
  102. * Wrapper component for using unstated
  103. */
  104. const TrashPageAlertWrapper = (props) => {
  105. return createSubscribedElement(TrashPageAlert, props, [AppContainer, PageContainer]);
  106. };
  107. TrashPageAlert.propTypes = {
  108. t: PropTypes.func.isRequired, // i18next
  109. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  110. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  111. };
  112. export default withTranslation()(TrashPageAlertWrapper);