PageAttachment.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import React from 'react';
  2. import Icon from './Common/Icon';
  3. import PageAttachmentList from './PageAttachment/PageAttachmentList';
  4. import DeleteAttachmentModal from './PageAttachment/DeleteAttachmentModal';
  5. export default class PageAttachment extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. attachments: [],
  10. inUse: {},
  11. attachmentToDelete: null,
  12. deleting: false,
  13. deleteError: '',
  14. };
  15. this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
  16. this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
  17. }
  18. componentDidMount() {
  19. const pageId = this.props.pageId;
  20. if (!pageId) {
  21. return ;
  22. }
  23. this.props.crowi.apiGet('/attachments.list', {page_id: pageId })
  24. .then(res => {
  25. const attachments = res.attachments;
  26. let inUse = {};
  27. for (const attachment of attachments) {
  28. inUse[attachment._id] = this.checkIfFileInUse(attachment);
  29. }
  30. this.setState({
  31. attachments: attachments,
  32. inUse: inUse,
  33. });
  34. });
  35. }
  36. checkIfFileInUse(attachment) {
  37. if (this.props.pageContent.match(attachment.url)) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. onAttachmentDeleteClicked(attachment) {
  43. this.setState({
  44. attachmentToDelete: attachment,
  45. });
  46. }
  47. onAttachmentDeleteClickedConfirm(attachment) {
  48. const attachmentId = attachment._id;
  49. this.setState({
  50. deleting: true,
  51. });
  52. this.props.crowi.apiPost('/attachments.remove', {attachment_id: attachmentId})
  53. .then(res => {
  54. this.setState({
  55. attachments: this.state.attachments.filter((at) => {
  56. return at._id != attachmentId;
  57. }),
  58. attachmentToDelete: null,
  59. deleting: false,
  60. });
  61. }).catch(err => {
  62. this.setState({
  63. deleteError: 'Something went wrong.',
  64. deleting: false,
  65. });
  66. });
  67. }
  68. render() {
  69. const attachmentToDelete = this.state.attachmentToDelete;
  70. let deleteModalClose = () => this.setState({ attachmentToDelete: null });
  71. let showModal = attachmentToDelete !== null;
  72. let deleteInUse = null;
  73. if (attachmentToDelete !== null) {
  74. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  75. }
  76. return (
  77. <div>
  78. <p>Attachments</p>
  79. <PageAttachmentList
  80. attachments={this.state.attachments}
  81. inUse={this.state.inUse}
  82. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  83. />
  84. <DeleteAttachmentModal
  85. show={showModal}
  86. animation={false}
  87. onHide={deleteModalClose}
  88. attachmentToDelete={attachmentToDelete}
  89. inUse={deleteInUse}
  90. deleting={this.state.deleting}
  91. deleteError={this.state.deleteError}
  92. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  93. />
  94. </div>
  95. );
  96. }
  97. }