PageAttachment.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. isUserLoggedIn() {
  69. return this.props.crowi.me !== '';
  70. }
  71. render() {
  72. let deleteAttachmentModal = '';
  73. if (this.isUserLoggedIn()) {
  74. const attachmentToDelete = this.state.attachmentToDelete;
  75. let deleteModalClose = () => this.setState({ attachmentToDelete: null });
  76. let showModal = attachmentToDelete !== null;
  77. let deleteInUse = null;
  78. if (attachmentToDelete !== null) {
  79. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  80. }
  81. deleteAttachmentModal = (
  82. <DeleteAttachmentModal
  83. show={showModal}
  84. animation={false}
  85. onHide={deleteModalClose}
  86. attachmentToDelete={attachmentToDelete}
  87. inUse={deleteInUse}
  88. deleting={this.state.deleting}
  89. deleteError={this.state.deleteError}
  90. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  91. />
  92. );
  93. }
  94. return (
  95. <div>
  96. <p>Attachments</p>
  97. <PageAttachmentList
  98. attachments={this.state.attachments}
  99. inUse={this.state.inUse}
  100. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  101. isUserLoggedIn={this.isUserLoggedIn()}
  102. />
  103. {deleteAttachmentModal}
  104. </div>
  105. );
  106. }
  107. }