UserPicture.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 {
  12. return user.image || '/images/userpicture.png';
  13. }
  14. }
  15. generateGravatarSrc(user) {
  16. const hash = md5(user.email.trim().toLowerCase());
  17. return `https://gravatar.com/avatar/${hash}`;
  18. }
  19. getClassName() {
  20. let className = ['picture', 'picture-rounded'];
  21. if (this.props.size) {
  22. className.push('picture-' + this.props.size);
  23. }
  24. return className.join(' ');
  25. }
  26. render() {
  27. const user = this.props.user;
  28. return (
  29. <img
  30. src={this.getUserPicture(user)}
  31. alt={user.username}
  32. className={this.getClassName()}
  33. />
  34. );
  35. }
  36. }
  37. UserPicture.propTypes = {
  38. user: React.PropTypes.object.isRequired,
  39. size: React.PropTypes.string,
  40. };
  41. UserPicture.defaultProps = {
  42. user: {},
  43. size: null,
  44. };