UserPicture.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import md5 from 'md5';
  3. // TODO UserComponent?
  4. export default class UserPicture extends React.Component {
  5. getUserPicture(user) {
  6. // gravatar
  7. if (user.isGravatarEnabled === true) {
  8. return this.generateGravatarSrc(user);
  9. }
  10. // uploaded image
  11. else if (user.image && user.image != '/images/userpicture.png') {
  12. return user.image;
  13. }
  14. else {
  15. return '/images/userpicture.png';
  16. }
  17. }
  18. generateGravatarSrc(user) {
  19. const hash = md5(user.email.trim().toLowerCase());
  20. return `https://gravatar.com/avatar/${hash}`;
  21. }
  22. getClassName() {
  23. let className = ['picture', 'picture-rounded'];
  24. if (this.props.size) {
  25. className.push('picture-' + this.props.size);
  26. }
  27. return className.join(' ');
  28. }
  29. render() {
  30. const user = this.props.user;
  31. return (
  32. <img
  33. src={this.getUserPicture(user)}
  34. alt={user.username}
  35. className={this.getClassName()}
  36. />
  37. );
  38. }
  39. }
  40. UserPicture.propTypes = {
  41. user: React.PropTypes.object.isRequired,
  42. size: React.PropTypes.string,
  43. };
  44. UserPicture.defaultProps = {
  45. user: {},
  46. size: null,
  47. };