UserPicture.jsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. const DEFAULT_IMAGE = '/images/icons/user.svg';
  4. // TODO UserComponent?
  5. export default class UserPicture extends React.Component {
  6. getClassName() {
  7. const className = ['img-circle', 'picture'];
  8. // size
  9. if (this.props.size) {
  10. className.push(`picture-${this.props.size}`);
  11. }
  12. return className.join(' ');
  13. }
  14. renderForNull() {
  15. return (
  16. <img
  17. src={DEFAULT_IMAGE}
  18. alt="someone"
  19. className={this.getClassName()}
  20. />
  21. );
  22. }
  23. render() {
  24. const user = this.props.user;
  25. if (user == null) {
  26. return this.renderForNull();
  27. }
  28. if (!user.imageUrlCached) {
  29. // [TODO][GW-1942] add imageUrlCached
  30. return this.renderForNull();
  31. }
  32. const imgElem = (
  33. <img
  34. src={user.imageUrlCached}
  35. alt={user.username}
  36. className={this.getClassName()}
  37. />
  38. );
  39. return (
  40. (this.props.withoutLink)
  41. ? <span>{imgElem}</span>
  42. : <a href={`/user/${user.username}`}>{imgElem}</a>
  43. );
  44. }
  45. }
  46. UserPicture.propTypes = {
  47. user: PropTypes.object,
  48. size: PropTypes.string,
  49. withoutLink: PropTypes.bool,
  50. };
  51. UserPicture.defaultProps = {
  52. size: null,
  53. };