Attachment.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import User from '../User/User';
  4. export default class Attachment extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this._onAttachmentDeleteClicked = this._onAttachmentDeleteClicked.bind(this);
  8. }
  9. iconNameByFormat(format) {
  10. if (format.match(/image\/.+/i)) {
  11. return 'icon-picture';
  12. }
  13. return 'icon-doc';
  14. }
  15. _onAttachmentDeleteClicked(event) {
  16. this.props.onAttachmentDeleteClicked(this.props.attachment);
  17. }
  18. render() {
  19. const attachment = this.props.attachment;
  20. const formatIcon = this.iconNameByFormat(attachment.fileFormat);
  21. let fileInUse = '';
  22. if (this.props.inUse) {
  23. fileInUse = <span className="attachment-in-use label label-info">In Use</span>;
  24. }
  25. const fileType = <span className="attachment-filetype label label-default">{attachment.fileFormat}</span>;
  26. const btnDownload = (this.props.isUserLoggedIn)
  27. ? (
  28. <a className="attachment-download" href={`/download/${attachment._id}`}>
  29. <i className="icon-cloud-download"></i>
  30. </a>)
  31. : '';
  32. const btnTrash = (this.props.isUserLoggedIn)
  33. ? (
  34. <a className="text-danger attachment-delete" onClick={this._onAttachmentDeleteClicked}>
  35. <i className="icon-trash"></i>
  36. </a>)
  37. : '';
  38. return (
  39. <li>
  40. <User user={attachment.creator} />
  41. <i className={formatIcon}></i>
  42. <a href={attachment.url}> {attachment.originalName}</a>
  43. {fileType}
  44. {fileInUse}
  45. {btnDownload}
  46. {btnTrash}
  47. </li>
  48. );
  49. }
  50. }
  51. Attachment.propTypes = {
  52. attachment: PropTypes.object.isRequired,
  53. inUse: PropTypes.bool.isRequired,
  54. onAttachmentDeleteClicked: PropTypes.func.isRequired,
  55. isUserLoggedIn: PropTypes.bool.isRequired,
  56. };
  57. Attachment.defaultProps = {
  58. };