Attachment.js 1.6 KB

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