PageAttachment.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* eslint-disable react/no-access-state-in-setstate */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import PageAttachmentList from './PageAttachment/PageAttachmentList';
  6. import DeleteAttachmentModal from './PageAttachment/DeleteAttachmentModal';
  7. import PaginationWrapper from './PaginationWrapper';
  8. import { withUnstatedContainers } from './UnstatedUtils';
  9. import AppContainer from '../services/AppContainer';
  10. import PageContainer from '../services/PageContainer';
  11. class PageAttachment extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. const { appContainer } = this.props;
  15. this.state = {
  16. activePage: 1,
  17. limit: appContainer.getConfig().pageLimitationS || 10,
  18. totalAttachments: 0,
  19. attachments: [],
  20. inUse: {},
  21. attachmentToDelete: null,
  22. deleting: false,
  23. deleteError: '',
  24. };
  25. this.handlePage = this.handlePage.bind(this);
  26. this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
  27. this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
  28. }
  29. async handlePage(selectedPage) {
  30. const { pageId } = this.props.pageContainer.state;
  31. const { limit } = this.state;
  32. const offset = (selectedPage - 1) * limit;
  33. const activePage = selectedPage;
  34. if (!pageId) { return }
  35. const res = await this.props.appContainer.apiv3Get('/attachment/list', {
  36. pageId, limit, offset,
  37. });
  38. const attachments = res.data.paginateResult.docs;
  39. const totalAttachments = res.data.paginateResult.totalDocs;
  40. const inUse = {};
  41. for (const attachment of attachments) {
  42. inUse[attachment._id] = this.checkIfFileInUse(attachment);
  43. }
  44. this.setState({
  45. activePage,
  46. totalAttachments,
  47. attachments,
  48. inUse,
  49. });
  50. }
  51. async componentDidMount() {
  52. await this.handlePage(1);
  53. this.setState({
  54. activePage: 1,
  55. });
  56. }
  57. checkIfFileInUse(attachment) {
  58. const { markdown } = this.props.pageContainer.state;
  59. if (markdown.match(attachment._id)) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. onAttachmentDeleteClicked(attachment) {
  65. this.setState({
  66. attachmentToDelete: attachment,
  67. });
  68. }
  69. onAttachmentDeleteClickedConfirm(attachment) {
  70. const attachmentId = attachment._id;
  71. this.setState({
  72. deleting: true,
  73. });
  74. this.props.appContainer.apiPost('/attachments.remove', { attachment_id: attachmentId })
  75. .then((res) => {
  76. this.setState({
  77. attachments: this.state.attachments.filter((at) => {
  78. // comparing ObjectId
  79. // eslint-disable-next-line eqeqeq
  80. return at._id != attachmentId;
  81. }),
  82. attachmentToDelete: null,
  83. deleting: false,
  84. });
  85. }).catch((err) => {
  86. this.setState({
  87. deleteError: 'Something went wrong.',
  88. deleting: false,
  89. });
  90. });
  91. }
  92. isUserLoggedIn() {
  93. return this.props.appContainer.currentUser != null;
  94. }
  95. render() {
  96. const { t } = this.props;
  97. if (this.state.attachments.length === 0) {
  98. return t('No_attachments_yet');
  99. }
  100. let deleteAttachmentModal = '';
  101. if (this.isUserLoggedIn()) {
  102. const attachmentToDelete = this.state.attachmentToDelete;
  103. const deleteModalClose = () => {
  104. this.setState({ attachmentToDelete: null, deleteError: '' });
  105. };
  106. const showModal = attachmentToDelete !== null;
  107. let deleteInUse = null;
  108. if (attachmentToDelete !== null) {
  109. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  110. }
  111. deleteAttachmentModal = (
  112. <DeleteAttachmentModal
  113. isOpen={showModal}
  114. animation="false"
  115. toggle={deleteModalClose}
  116. attachmentToDelete={attachmentToDelete}
  117. inUse={deleteInUse}
  118. deleting={this.state.deleting}
  119. deleteError={this.state.deleteError}
  120. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  121. />
  122. );
  123. }
  124. console.log(this.state.limit);
  125. return (
  126. <>
  127. <PageAttachmentList
  128. attachments={this.state.attachments}
  129. inUse={this.state.inUse}
  130. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  131. isUserLoggedIn={this.isUserLoggedIn()}
  132. />
  133. {deleteAttachmentModal}
  134. <PaginationWrapper
  135. activePage={this.state.activePage}
  136. changePage={this.handlePage}
  137. totalItemsCount={this.state.totalAttachments}
  138. pagingLimit={this.state.limit}
  139. align="center"
  140. />
  141. </>
  142. );
  143. }
  144. }
  145. /**
  146. * Wrapper component for using unstated
  147. */
  148. const PageAttachmentWrapper = withUnstatedContainers(PageAttachment, [AppContainer, PageContainer]);
  149. PageAttachment.propTypes = {
  150. t: PropTypes.func.isRequired,
  151. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  152. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  153. };
  154. export default withTranslation()(PageAttachmentWrapper);