PageAttachment.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable react/no-access-state-in-setstate */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import PageAttachmentList from './PageAttachment/PageAttachmentList';
  5. import DeleteAttachmentModal from './PageAttachment/DeleteAttachmentModal';
  6. import { withUnstatedContainers } from './UnstatedUtils';
  7. import AppContainer from '../services/AppContainer';
  8. import PageContainer from '../services/PageContainer';
  9. class PageAttachment extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. attachments: [],
  14. inUse: {},
  15. attachmentToDelete: null,
  16. deleting: false,
  17. deleteError: '',
  18. };
  19. this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
  20. this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
  21. }
  22. async componentDidMount() {
  23. const { pageId } = this.props.pageContainer.state;
  24. if (!pageId) {
  25. return;
  26. }
  27. const res = await this.props.appContainer.apiv3Get('/attachment/list', { pageId });
  28. const attachments = res.data.attachments;
  29. const inUse = {};
  30. for (const attachment of attachments) {
  31. inUse[attachment._id] = this.checkIfFileInUse(attachment);
  32. }
  33. this.setState({
  34. attachments,
  35. inUse,
  36. });
  37. }
  38. checkIfFileInUse(attachment) {
  39. const { markdown } = this.props.pageContainer.state;
  40. if (markdown.match(attachment._id)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. onAttachmentDeleteClicked(attachment) {
  46. this.setState({
  47. attachmentToDelete: attachment,
  48. });
  49. }
  50. onAttachmentDeleteClickedConfirm(attachment) {
  51. const attachmentId = attachment._id;
  52. this.setState({
  53. deleting: true,
  54. });
  55. this.props.appContainer.apiPost('/attachments.remove', { attachment_id: attachmentId })
  56. .then((res) => {
  57. this.setState({
  58. attachments: this.state.attachments.filter((at) => {
  59. // comparing ObjectId
  60. // eslint-disable-next-line eqeqeq
  61. return at._id != attachmentId;
  62. }),
  63. attachmentToDelete: null,
  64. deleting: false,
  65. });
  66. }).catch((err) => {
  67. this.setState({
  68. deleteError: 'Something went wrong.',
  69. deleting: false,
  70. });
  71. });
  72. }
  73. isUserLoggedIn() {
  74. return this.props.appContainer.currentUser != null;
  75. }
  76. render() {
  77. let deleteAttachmentModal = '';
  78. if (this.isUserLoggedIn()) {
  79. const attachmentToDelete = this.state.attachmentToDelete;
  80. const deleteModalClose = () => {
  81. this.setState({ attachmentToDelete: null, deleteError: '' });
  82. };
  83. const showModal = attachmentToDelete !== null;
  84. let deleteInUse = null;
  85. if (attachmentToDelete !== null) {
  86. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  87. }
  88. deleteAttachmentModal = (
  89. <DeleteAttachmentModal
  90. isOpen={showModal}
  91. animation={false}
  92. toggle={deleteModalClose}
  93. attachmentToDelete={attachmentToDelete}
  94. inUse={deleteInUse}
  95. deleting={this.state.deleting}
  96. deleteError={this.state.deleteError}
  97. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  98. />
  99. );
  100. }
  101. return (
  102. <div>
  103. <PageAttachmentList
  104. attachments={this.state.attachments}
  105. inUse={this.state.inUse}
  106. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  107. isUserLoggedIn={this.isUserLoggedIn()}
  108. />
  109. {deleteAttachmentModal}
  110. </div>
  111. );
  112. }
  113. }
  114. /**
  115. * Wrapper component for using unstated
  116. */
  117. const PageAttachmentWrapper = withUnstatedContainers(PageAttachment, [AppContainer, PageContainer]);
  118. PageAttachment.propTypes = {
  119. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  120. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  121. };
  122. export default PageAttachmentWrapper;