Skip to content

Instantly share code, notes, and snippets.

@oliverroick
Created February 9, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliverroick/e903569d1b776dc28eda to your computer and use it in GitHub Desktop.
Save oliverroick/e903569d1b776dc28eda to your computer and use it in GitHub Desktop.
import React from 'react';
import classNames from 'classnames';
const propTypes = {
messageDismiss: React.PropTypes.func,
message: React.PropTypes.object.isRequired,
};
class Message extends React.Component {
constructor(props) {
super(props);
this.dismiss = this.dismiss.bind(this);
this.renderDismiss = this.renderDismiss.bind(this);
this.renderErrorDetail = this.renderErrorDetail.bind(this);
}
dismiss() {
this.props.messageDismiss(this.props.message.get('id'));
}
renderDismiss() {
if (this.props.message.get('dismissable')) {
return <button type="button" onClick={ this.dismiss }>Dismiss</button>;
}
}
renderErrorDetail() {
const details = this.props.message.get('details');
if (details && details.count()) {
return <ul>{details.map(detail => <li>{ detail }</li>)}</ul>;
}
}
getClassNames(msgType) {
let classes = ['alert', 'text-center'];
switch(msgType):
case 'success':
classes.push('alert-success');
break;
case 'error':
classes.push('alert-danger');
break;
return classNames('message', ...classes);
}
render() {
const message = this.props.message;
const messageClass = this.getClassNames(message.get('type'));
return (
<div className={ messageClass }>
<p>{ message.get('msg') }</p>
{ this.renderErrorDetail() }
{ this.renderDismiss() }
</div>
);
}
}
Message.propTypes = propTypes;
export default Message;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment