Attachment.jsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { UserPicture } from '../User/UserPicture';
  4. export 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. if (this.props.onAttachmentDeleteClicked != null) {
  17. this.props.onAttachmentDeleteClicked(this.props.attachment);
  18. }
  19. }
  20. render() {
  21. const attachment = this.props.attachment;
  22. const formatIcon = this.iconNameByFormat(attachment.fileFormat);
  23. let fileInUse = '';
  24. if (this.props.inUse) {
  25. fileInUse = <span className="attachment-in-use badge badge-pill badge-info">In Use</span>;
  26. }
  27. const fileType = <span className="attachment-filetype badge badge-pill badge-secondary">{attachment.fileFormat}</span>;
  28. const btnDownload = (this.props.isUserLoggedIn)
  29. ? (
  30. <a className="attachment-download" href={attachment.downloadPathProxied}>
  31. <i className="icon-cloud-download" />
  32. </a>
  33. )
  34. : '';
  35. const btnTrash = (this.props.isUserLoggedIn)
  36. ? (
  37. /* eslint-disable-next-line */
  38. <a className="text-danger attachment-delete" onClick={this._onAttachmentDeleteClicked}>
  39. <i className="icon-trash" />
  40. </a>
  41. )
  42. : '';
  43. return (
  44. <div className="attachment mb-2">
  45. <span className="mr-1 attachment-userpicture">
  46. <UserPicture user={attachment.creator} size="sm"></UserPicture>
  47. </span>
  48. <a className="mr-2" href={attachment.filePathProxied} target="_blank" rel="noopener noreferrer">
  49. <i className={formatIcon}></i> {attachment.originalName}
  50. </a>
  51. <span className="mr-2">{fileType}</span>
  52. <span className="mr-2">{fileInUse}</span>
  53. <span className="mr-2">{btnDownload}</span>
  54. <span className="mr-2">{btnTrash}</span>
  55. </div>
  56. );
  57. }
  58. }
  59. Attachment.propTypes = {
  60. attachment: PropTypes.object.isRequired,
  61. inUse: PropTypes.bool,
  62. onAttachmentDeleteClicked: PropTypes.func,
  63. isUserLoggedIn: PropTypes.bool,
  64. };
  65. Attachment.defaultProps = {
  66. inUse: false,
  67. isUserLoggedIn: false,
  68. };