PageAttachment.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. export default class PageAttachment extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. attachments: [],
  11. inUse: {},
  12. attachmentToDelete: null,
  13. deleting: false,
  14. deleteError: '',
  15. };
  16. this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
  17. this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
  18. }
  19. componentDidMount() {
  20. const pageId = this.props.pageId;
  21. if (!pageId) {
  22. return;
  23. }
  24. this.props.crowi.apiGet('/attachments.list', { page_id: pageId })
  25. .then((res) => {
  26. const attachments = res.attachments;
  27. const inUse = {};
  28. for (const attachment of attachments) {
  29. inUse[attachment._id] = this.checkIfFileInUse(attachment);
  30. }
  31. this.setState({
  32. attachments,
  33. inUse,
  34. });
  35. });
  36. }
  37. checkIfFileInUse(attachment) {
  38. if (this.props.markdown.match(attachment.filePathProxied)) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. onAttachmentDeleteClicked(attachment) {
  44. this.setState({
  45. attachmentToDelete: attachment,
  46. });
  47. }
  48. onAttachmentDeleteClickedConfirm(attachment) {
  49. const attachmentId = attachment._id;
  50. this.setState({
  51. deleting: true,
  52. });
  53. this.props.crowi.apiPost('/attachments.remove', { attachment_id: attachmentId })
  54. .then((res) => {
  55. this.setState({
  56. attachments: this.state.attachments.filter((at) => {
  57. // comparing ObjectId
  58. // eslint-disable-next-line eqeqeq
  59. return at._id != attachmentId;
  60. }),
  61. attachmentToDelete: null,
  62. deleting: false,
  63. });
  64. }).catch((err) => {
  65. this.setState({
  66. deleteError: 'Something went wrong.',
  67. deleting: false,
  68. });
  69. });
  70. }
  71. isUserLoggedIn() {
  72. return this.props.crowi.me !== '';
  73. }
  74. render() {
  75. let deleteAttachmentModal = '';
  76. if (this.isUserLoggedIn()) {
  77. const attachmentToDelete = this.state.attachmentToDelete;
  78. const deleteModalClose = () => {
  79. this.setState({ attachmentToDelete: null, deleteError: '' });
  80. };
  81. const showModal = attachmentToDelete !== null;
  82. let deleteInUse = null;
  83. if (attachmentToDelete !== null) {
  84. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  85. }
  86. deleteAttachmentModal = (
  87. <DeleteAttachmentModal
  88. show={showModal}
  89. animation={false}
  90. onHide={deleteModalClose}
  91. attachmentToDelete={attachmentToDelete}
  92. inUse={deleteInUse}
  93. deleting={this.state.deleting}
  94. deleteError={this.state.deleteError}
  95. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  96. />
  97. );
  98. }
  99. return (
  100. <div>
  101. <PageAttachmentList
  102. attachments={this.state.attachments}
  103. inUse={this.state.inUse}
  104. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  105. isUserLoggedIn={this.isUserLoggedIn()}
  106. />
  107. {deleteAttachmentModal}
  108. </div>
  109. );
  110. }
  111. }
  112. PageAttachment.propTypes = {
  113. crowi: PropTypes.object.isRequired,
  114. markdown: PropTypes.string.isRequired,
  115. pageId: PropTypes.string.isRequired,
  116. };