import React from 'react'; import { isString } from '../commons/utils'; /** * Small module to handle error messages from the server */ export default function Errors(props) { const errs = props.messages; if (!errs) { return null; } // is a simple string representation ? if (isString(errs)) { return React.createElement( 'div', null, errs ); } // no message found ? if (errs.length === 0) { return null; } const msgs = Errors.format(errs); // return component to be displayed return msgs.length === 1 ? React.createElement( 'div', null, msgs[0] ) : React.createElement( 'ul', null, msgs.map((k, index) => React.createElement( 'li', { key: index }, k )) ); } /** * Format the messages for displaying * @param {[type]} messages [description] * @return {[type]} [description] */ Errors.format = function (messages) { return messages.map(msg => msg.field ? msg.field + ': ' + msg.msg : msg.msg); }; Errors.propTypes = { messages: React.PropTypes.any };