Yuki Takei пре 6 година
родитељ
комит
189e753d31
1 измењених фајлова са 23 додато и 7 уклоњено
  1. 23 7
      src/client/js/components/User/UserPicture.jsx

+ 23 - 7
src/client/js/components/User/UserPicture.jsx

@@ -2,24 +2,27 @@ import React from 'react';
 import md5 from 'md5';
 import md5 from 'md5';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
+const DEFAULT_IMAGE = '/images/icons/user.svg';
+
 // TODO UserComponent?
 // TODO UserComponent?
 export default class UserPicture extends React.Component {
 export default class UserPicture extends React.Component {
 
 
   getUserPicture(user) {
   getUserPicture(user) {
+    let pictPath;
+
     // gravatar
     // gravatar
     if (user.isGravatarEnabled === true) {
     if (user.isGravatarEnabled === true) {
-      return this.generateGravatarSrc(user);
+      pictPath = this.generateGravatarSrc(user);
     }
     }
     // uploaded image
     // uploaded image
     if (user.image != null) {
     if (user.image != null) {
-      return user.image;
+      pictPath = user.image;
     }
     }
     if (user.imageAttachment != null) {
     if (user.imageAttachment != null) {
       return user.imageAttachment.filePathProxied;
       return user.imageAttachment.filePathProxied;
     }
     }
 
 
-    return '/images/icons/user.svg';
-
+    return pictPath || DEFAULT_IMAGE;
   }
   }
 
 
   generateGravatarSrc(user) {
   generateGravatarSrc(user) {
@@ -38,9 +41,22 @@ export default class UserPicture extends React.Component {
     return className.join(' ');
     return className.join(' ');
   }
   }
 
 
+  renderForNull() {
+    return (
+      <img
+        src={DEFAULT_IMAGE}
+        alt="someone"
+        className={this.getClassName()}
+      />
+    );
+  }
+
   render() {
   render() {
     const user = this.props.user;
     const user = this.props.user;
-    const href = `/user/${user.username}`;
+
+    if (user == null) {
+      return this.renderForNull();
+    }
 
 
     const imgElem = (
     const imgElem = (
       <img
       <img
@@ -53,14 +69,14 @@ export default class UserPicture extends React.Component {
     return (
     return (
       (this.props.withoutLink)
       (this.props.withoutLink)
         ? <span>{imgElem}</span>
         ? <span>{imgElem}</span>
-        : <a href={href}>{imgElem}</a>
+        : <a href={`/user/${user.username}`}>{imgElem}</a>
     );
     );
   }
   }
 
 
 }
 }
 
 
 UserPicture.propTypes = {
 UserPicture.propTypes = {
-  user: PropTypes.object.isRequired,
+  user: PropTypes.object,
   size: PropTypes.string,
   size: PropTypes.string,
   withoutLink: PropTypes.bool,
   withoutLink: PropTypes.bool,
 };
 };