PageAttachment.js 3.1 KB

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