Attachment.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 'fa fa-file-image-o';
  12. }
  13. return 'fa fa-file-o';
  14. }
  15. _onAttachmentDeleteClicked(event) {
  16. this.props.onAttachmentDeleteClicked(this.props.attachment);
  17. }
  18. render() {
  19. const attachment = this.props.attachment;
  20. const attachmentId = attachment._id
  21. const formatIcon = this.iconNameByFormat(attachment.fileFormat);
  22. let fileInUse = '';
  23. if (this.props.inUse) {
  24. fileInUse = <span className="attachment-in-use label label-info">In Use</span>;
  25. }
  26. const fileType = <span className="attachment-filetype label label-default">{attachment.fileFormat}</span>;
  27. const btnTrash = (this.props.isUserLoggedIn)
  28. ? <a className="text-danger attachment-delete" onClick={this._onAttachmentDeleteClicked}>
  29. <i className="icon-trash"></i>
  30. </a>
  31. : '';
  32. return (
  33. <li>
  34. <User user={attachment.creator} />
  35. <i className={formatIcon}></i>
  36. <a href={attachment.url}> {attachment.originalName}</a>
  37. {fileType}
  38. {fileInUse}
  39. {btnTrash}
  40. </li>
  41. );
  42. }
  43. }
  44. Attachment.propTypes = {
  45. attachment: PropTypes.object.isRequired,
  46. inUse: PropTypes.bool.isRequired,
  47. onAttachmentDeleteClicked: PropTypes.func.isRequired,
  48. isUserLoggedIn: PropTypes.bool.isRequired,
  49. };
  50. Attachment.defaultProps = {
  51. };