import React from 'react'; import PropTypes from 'prop-types'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import moment from 'moment'; import { Utils } from '../../utils/utils'; import { gettext, wikiId } from '../../utils/constants'; import wikiAPI from '../../utils/wiki-api'; import ModalPortal from '../../components/modal-portal'; import toaster from '../../components/toast'; import Paginator from '../../components/paginator'; import WikiCleanTrash from '../../components/dialog/wiki-clean-trash'; import NavItemIcon from './common/nav-item-icon'; import '../../css/toolbar.css'; import '../../css/search.css'; import '../../css/wiki-trash-dialog.css'; const propTypes = { showTrashDialog: PropTypes.bool.isRequired, toggleTrashDialog: PropTypes.func.isRequired, getWikiConfig: PropTypes.func.isRequired }; class WikiTrashDialog extends React.Component { constructor(props) { super(props); this.state = { isLoading: true, errorMsg: '', items: [], isCleanTrashDialogOpen: false, currentPage: 1, perPage: 100, hasNextPage: false }; } componentDidMount() { this.getItems(); } getItems = (page) => { wikiAPI.getWikiTrash(wikiId, page, this.state.perPage).then((res) => { const { items, total_count } = res.data; if (!page) { page = 1; } this.setState({ currentPage: page, hasNextPage: total_count - page * this.state.perPage > 0, isLoading: false, items: items, }); }); }; resetPerPage = (perPage) => { this.setState({ perPage: perPage }, () => { this.getItems(1); }); }; cleanTrash = () => { this.toggleCleanTrashDialog(); }; toggleCleanTrashDialog = () => { this.setState({ isCleanTrashDialogOpen: !this.state.isCleanTrashDialogOpen }); }; refreshTrash = () => { this.setState({ isLoading: true, errorMsg: '', items: [] }); this.getItems(); }; render() { const { showTrashDialog, toggleTrashDialog } = this.props; const { isCleanTrashDialogOpen } = this.state; const { isAdmin, enableUserCleanTrash, repoName } = window.wiki.config; let title = gettext('{placeholder} Wiki Trash'); title = title.replace('{placeholder}', '' + Utils.HTMLescape(repoName) + ''); return (
{(isAdmin && enableUserCleanTrash) && }
} >
{isCleanTrashDialogOpen && }
); } } class Content extends React.Component { constructor(props) { super(props); this.theadData = [ { width: '3%', text: gettext('Name') }, { width: '20%', text: '' }, { width: '30%', text: gettext('Size') }, { width: '37%', text: gettext('Delete Time') }, { width: '10%', text: '' } ]; } getPreviousPage = () => { this.props.getListByPage(this.props.currentPage - 1); }; getNextPage = () => { this.props.getListByPage(this.props.currentPage + 1); }; render() { const { items } = this.props.data; const { curPerPage, currentPage, hasNextPage } = this.props; return ( {this.theadData.map((item, index) => { return ; })} {items.map((item, index) => { return ( ); })}
{item.text}
); } } Content.propTypes = { data: PropTypes.object.isRequired, getListByPage: PropTypes.func.isRequired, resetPerPage: PropTypes.func.isRequired, currentPage: PropTypes.number.isRequired, curPerPage: PropTypes.number.isRequired, hasNextPage: PropTypes.bool.isRequired, getWikiConfig: PropTypes.func.isRequired }; class Item extends React.Component { constructor(props) { super(props); this.state = { restored: false, isIconShown: false, getWikiConfig: PropTypes.func.isRequired }; } handleMouseOver = () => { this.setState({ isIconShown: true }); }; handleMouseOut = () => { this.setState({ isIconShown: false }); }; restoreItem = (e) => { e.preventDefault(); const item = this.props.item; wikiAPI.revertTrashPage(wikiId, item.page_id).then(res => { this.setState({ restored: true }); this.props.getWikiConfig(); toaster.success(gettext('Successfully restored 1 item.')); }).catch((error) => { let errorMsg = ''; if (error.response) { errorMsg = error.response.data.error_msg || gettext('Error'); } else { errorMsg = gettext('Please check the network.'); } toaster.danger(errorMsg); }); }; render() { const item = this.props.item; const { restored, isIconShown } = this.state; if (restored) { return null; } const { isAdmin } = window.wiki.config; return ( {item.name} {Utils.bytesToSize(item.size)} {moment(item.deleted_time).format('YYYY-MM-DD')} {isAdmin && {gettext('Restore')} } ); } } Item.propTypes = { item: PropTypes.object.isRequired }; WikiTrashDialog.propTypes = propTypes; export default WikiTrashDialog;