All files / src/components/Person index.jsx

52% Statements 13/25
35.71% Branches 5/14
62.5% Functions 5/8
50% Lines 11/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                  31x       8x 8x               8x       8x 23x   8x 8x                                                 8x     8x             4x              
import styles from './style.postcss';
 
import React, { PureComponent } from 'react';
import StudentPicture from 'components/StudentPicture';
import AdultPicture from 'components/AdultPicture';
import log from 'domain/log';
import classnames from 'classnames';
import PropTypes from 'prop-types';
 
const isPicture = (c) => c.type === StudentPicture || c.type === AdultPicture;
 
class Person extends PureComponent {
  constructor(props) {
    super(props);
    this._setUp(props);
  }
 
  componentWillUpdate(props) {
    this._setUp(props);
  }
 
  _setUp(props) {
    Iif (! PRODUCTION) {
      this._showWarnings(props);
    }
 
    const children = React.Children.toArray(props.children);
    this._lines = children.filter((c) => ! isPicture(c));
 
    const picture = children.find(isPicture);
    this._picture = picture ? <picture.type {...picture.props}
        className={classnames(picture.props.className, styles.Person_picture,
        { [styles.Person_picture_vertical]: props.vertical }
        )} /> : null;
  }
 
  _showWarnings(props) {
    const children = React.Children.toArray(props.children);
    const pictures = children.filter(isPicture);
    const lines = children.filter((c) => ! isPicture(c));
 
    if (pictures <= 0) {
      log.warn('No picture provided to Person');
    }
 
    if (pictures > 1) {
      log.warn('More than one picture provided to Person');
    }
 
    if (lines <= 0) {
      log.warn('No lines provided to Person');
    }
  }
 
  render() {
    const personLinesClassName = this.props.vertical ?
      classnames(styles.Person_lines, styles.Person_lines_vertical) :
      styles.Person_lines;
    return <div className={classnames(styles.Person, this.props.className)}>
      {this._picture}
      <div className={personLinesClassName}>{this._lines}</div>
    </div>;
  }
}
 
Person.propTypes = {
  children: PropTypes.node,
  className: PropTypes.string,
  vertical: PropTypes.bool,
};
 
export default Person;