| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /* eslint-disable react/no-access-state-in-setstate */
- import React from 'react';
- import PropTypes from 'prop-types';
- import { withTranslation } from 'react-i18next';
- import AppContainer from '~/client/services/AppContainer';
- import PageContainer from '~/client/services/PageContainer';
- import { apiPost } from '~/client/util/apiv1-client';
- import { apiv3Get } from '~/client/util/apiv3-client';
- import DeleteAttachmentModal from './PageAttachment/DeleteAttachmentModal';
- import PageAttachmentList from './PageAttachment/PageAttachmentList';
- import PaginationWrapper from './PaginationWrapper';
- import { withUnstatedContainers } from './UnstatedUtils';
- class PageAttachment extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- activePage: 1,
- totalAttachments: 0,
- limit: Infinity,
- attachments: [],
- inUse: {},
- attachmentToDelete: null,
- deleting: false,
- deleteError: '',
- };
- this.handlePage = this.handlePage.bind(this);
- this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
- this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
- }
- async handlePage(selectedPage) {
- const { pageId } = this.props.pageContainer.state;
- const page = selectedPage;
- if (!pageId) { return }
- const res = await apiv3Get('/attachment/list', { pageId, page });
- const attachments = res.data.paginateResult.docs;
- const totalAttachments = res.data.paginateResult.totalDocs;
- const pagingLimit = res.data.paginateResult.limit;
- const inUse = {};
- for (const attachment of attachments) {
- inUse[attachment._id] = this.checkIfFileInUse(attachment);
- }
- this.setState({
- activePage: selectedPage,
- totalAttachments,
- limit: pagingLimit,
- attachments,
- inUse,
- });
- }
- async componentDidMount() {
- await this.handlePage(1);
- this.setState({
- activePage: 1,
- });
- }
- checkIfFileInUse(attachment) {
- const { markdown } = this.props.pageContainer.state;
- if (markdown.match(attachment._id)) {
- return true;
- }
- return false;
- }
- onAttachmentDeleteClicked(attachment) {
- this.setState({
- attachmentToDelete: attachment,
- });
- }
- onAttachmentDeleteClickedConfirm(attachment) {
- const attachmentId = attachment._id;
- this.setState({
- deleting: true,
- });
- apiPost('/attachments.remove', { attachment_id: attachmentId })
- .then((res) => {
- this.setState({
- attachments: this.state.attachments.filter((at) => {
- // comparing ObjectId
- // eslint-disable-next-line eqeqeq
- return at._id != attachmentId;
- }),
- attachmentToDelete: null,
- deleting: false,
- });
- }).catch((err) => {
- this.setState({
- deleteError: 'Something went wrong.',
- deleting: false,
- });
- });
- }
- isUserLoggedIn() {
- return this.props.appContainer.currentUser != null;
- }
- render() {
- const { t } = this.props;
- if (this.state.attachments.length === 0) {
- return t('No_attachments_yet');
- }
- let deleteAttachmentModal = '';
- if (this.isUserLoggedIn()) {
- const attachmentToDelete = this.state.attachmentToDelete;
- const deleteModalClose = () => {
- this.setState({ attachmentToDelete: null, deleteError: '' });
- };
- const showModal = attachmentToDelete !== null;
- let deleteInUse = null;
- if (attachmentToDelete !== null) {
- deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
- }
- deleteAttachmentModal = (
- <DeleteAttachmentModal
- isOpen={showModal}
- animation="false"
- toggle={deleteModalClose}
- attachmentToDelete={attachmentToDelete}
- inUse={deleteInUse}
- deleting={this.state.deleting}
- deleteError={this.state.deleteError}
- onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
- />
- );
- }
- return (
- <>
- <PageAttachmentList
- attachments={this.state.attachments}
- inUse={this.state.inUse}
- onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
- isUserLoggedIn={this.isUserLoggedIn()}
- />
- {deleteAttachmentModal}
- <PaginationWrapper
- activePage={this.state.activePage}
- changePage={this.handlePage}
- totalItemsCount={this.state.totalAttachments}
- pagingLimit={this.state.limit}
- align="center"
- />
- </>
- );
- }
- }
- /**
- * Wrapper component for using unstated
- */
- const PageAttachmentWrapper = withUnstatedContainers(PageAttachment, [AppContainer, PageContainer]);
- PageAttachment.propTypes = {
- t: PropTypes.func.isRequired,
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
- pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
- };
- export default withTranslation()(PageAttachmentWrapper);
|