| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import React, { useState } from 'react';
- import PropTypes from 'prop-types';
- import { withTranslation } from 'react-i18next';
- import { toastError } from '../../util/apiNotification';
- import { createSubscribedElement } from '../UnstatedUtils';
- import AppContainer from '../../services/AppContainer';
- import PageContainer from '../../services/PageContainer';
- import UserPicture from '../User/UserPicture';
- import EmptyTrashModal from '../EmptyTrashModal';
- import PageDeleteModal from '../PageDeleteModal';
- const TrashPageAlert = (props) => {
- const { t, appContainer, pageContainer } = props;
- const {
- path, isDeleted, revisionAuthor, updatedAt, hasChildren, isAbleToDeleteCompletely,
- } = pageContainer.state;
- const { currentUser } = appContainer;
- const [isEmptyTrashModalShown, setIsEmptyTrashModalShown] = useState(false);
- const [isPageDeleteModalShown, setIsPageDeleteModalShown] = useState(false);
- function openEmptyTrashModal() {
- setIsEmptyTrashModalShown(true);
- }
- function closeEmptyTrashModal() {
- setIsEmptyTrashModalShown(false);
- }
- function openPageDeleteModal() {
- setIsPageDeleteModalShown(true);
- }
- function closePageDeleteModal() {
- setIsPageDeleteModalShown(false);
- }
- async function onClickEmptyBtn() {
- try {
- await appContainer.apiv3Delete('/pages/empty-trash');
- window.location.reload();
- }
- catch (err) {
- toastError(err);
- }
- }
- async function onClickDeleteBtn(recursively, completely) {
- console.log(completely);
- console.log(recursively);
- }
- function renderEmptyButton() {
- return (
- <button
- href="#"
- type="button"
- className="btn btn-danger rounded-pill btn-sm ml-auto"
- data-target="#emptyTrash"
- onClick={openEmptyTrashModal}
- >
- <i className="icon-trash" aria-hidden="true"></i>{ t('modal_empty.empty_the_trash') }
- </button>
- );
- }
- function renderTrashPageManagementButtons() {
- return (
- <>
- <button
- type="button"
- className="btn btn-outline-secondary rounded-pill btn-sm ml-auto mr-2"
- data-target="#putBackPage"
- data-toggle="modal"
- >
- <i className="icon-action-undo" aria-hidden="true"></i> { t('Put Back') }
- </button>
- <button
- type="button"
- className="btn btn-danger rounded-pill btn-sm mr-2"
- disabled={!isAbleToDeleteCompletely}
- onClick={openPageDeleteModal}
- >
- <i className="icon-fire" aria-hidden="true"></i> { t('Delete Completely') }
- </button>
- </>
- );
- }
- return (
- <>
- <div className="alert alert-warning py-3 px-4 d-flex align-items-center">
- <div>
- This page is in the trash <i className="icon-trash" aria-hidden="true"></i>.
- {isDeleted && <span><br /><UserPicture user={revisionAuthor} /> Deleted by {revisionAuthor.name} at {updatedAt}</span>}
- </div>
- {(currentUser.admin && path === '/trash' && hasChildren) && renderEmptyButton()}
- {(isDeleted && currentUser != null) && renderTrashPageManagementButtons()}
- </div>
- <EmptyTrashModal isOpen={isEmptyTrashModalShown} toggle={closeEmptyTrashModal} onClickSubmit={onClickEmptyBtn} />
- <PageDeleteModal
- isOpen={isPageDeleteModalShown}
- toggle={closePageDeleteModal}
- onClickSubmit={onClickDeleteBtn}
- path={path}
- isDeleteCompletelyModal
- isAbleToDeleteCompletely={isAbleToDeleteCompletely}
- />
- </>
- );
- };
- /**
- * Wrapper component for using unstated
- */
- const TrashPageAlertWrapper = (props) => {
- return createSubscribedElement(TrashPageAlert, props, [AppContainer, PageContainer]);
- };
- TrashPageAlert.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
- };
- export default withTranslation()(TrashPageAlertWrapper);
|