PageAttachment.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. };
  13. this.onAttachmentDeleteClicked = this.onAttachmentDeleteClicked.bind(this);
  14. this.onAttachmentDeleteClickedConfirm = this.onAttachmentDeleteClickedConfirm.bind(this);
  15. }
  16. componentDidMount() {
  17. const pageId = this.props.pageId;
  18. if (!pageId) {
  19. return ;
  20. }
  21. this.props.crowi.apiGet('/attachments.list', {page_id: pageId })
  22. .then(res => {
  23. const attachments = res.attachments;
  24. let inUse = {};
  25. for (let attachment in attachments.length) {
  26. inUse[attachment._id] = this.checkIfFileInUse(attachment);;
  27. }
  28. this.setState({
  29. attachments: attachments,
  30. inUse: inUse,
  31. });
  32. console.log(attachments);
  33. });
  34. }
  35. checkIfFileInUse(attachment) {
  36. // todo
  37. return true;
  38. }
  39. onAttachmentDeleteClicked(attachment) {
  40. this.setState({
  41. attachmentToDelete: attachment
  42. });
  43. }
  44. onAttachmentDeleteClickedConfirm(attachment) {
  45. const attachmentId = attachment._id;
  46. console.log('Do Delete!!', attachmentId);
  47. this.props.crowi.apiPost('/attachments.remove', {attachment_id: attachmentId})
  48. .then(res => {
  49. this.setState({
  50. attachments: this.state.attachments.filter((at) => {
  51. return at._id != attachmentId;
  52. }),
  53. attachmentToDelete: null,
  54. });
  55. }).catch(err => {
  56. console.log('error', err);
  57. });
  58. }
  59. render() {
  60. const attachmentToDelete = this.state.attachmentToDelete;
  61. let deleteModalClose = () => this.setState({ attachmentToDelete: null });
  62. let showModal = attachmentToDelete !== null;
  63. let deleteInUse = null;
  64. if (attachmentToDelete !== null) {
  65. deleteInUse = this.state.inUse[attachmentToDelete._id] || false;
  66. }
  67. return (
  68. <div>
  69. <p>Attachments</p>
  70. <PageAttachmentList
  71. attachments={this.state.attachments}
  72. inUse={this.state.inUse}
  73. onAttachmentDeleteClicked={this.onAttachmentDeleteClicked}
  74. />
  75. <DeleteAttachmentModal
  76. show={showModal}
  77. animation={false}
  78. onHide={deleteModalClose}
  79. attachmentToDelete={attachmentToDelete}
  80. inUse={deleteInUse}
  81. onAttachmentDeleteClickedConfirm={this.onAttachmentDeleteClickedConfirm}
  82. />
  83. </div>
  84. );
  85. }
  86. }