All files / components/utilities error-message.jsx

100% Statements 6/6
83.33% Branches 15/18
100% Functions 1/1
100% Lines 6/6
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          6x 6x 6x   6x                                       2x                                 2x                
import React from 'react';
import PropTypes from 'prop-types';
 
class ErrorMessage extends React.Component {
  render() {
    const origin = this.props.error && this.props.error.origin ? this.props.error.origin : this.props.origin;
    const code = this.props.error && this.props.error.code ? this.props.error.code : this.props.code;
    const message = this.props.error && this.props.error.message ? this.props.error.message : this.props.message;
    
    return (
      <div className={`alert ${this.props.theme} text-left`}>
        {code || origin ? (
          <p>
            <strong>Error {code}:&nbsp;</strong>
            {origin ? <small>({origin})</small> : ''}
          </p>
        ) : ''}
        <div>{message}</div>
      </div>
    );
  }
}
 
/**
  *  this.props.error expects { code, origin, message }
  *  Code is the error's status code
  *  Origin is the route or process that induced the error
  *  Message is the message for the user to read
*/
ErrorMessage.propTypes = {
  error: PropTypes.object,
  origin: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element
  ]),
  code: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element,
    PropTypes.number
  ]),
  message: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element
  ]),
  theme: PropTypes.string
};
ErrorMessage.defaultProps = {
  theme: 'alert-danger',
  origin: '',
  code: '',
  message: ''
};
 
export default ErrorMessage;