Advertisement
Guest User

Untitled

a guest
May 24th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let React = require('react');
  2.  
  3. function App(props) {
  4.     return (
  5.         <div id="app">
  6.             <Notification {...props.notification}/>
  7.         </div>
  8.     )
  9. }
  10.  
  11. let map = {
  12.     success: 'success',
  13.     message: 'info',
  14.     caution: 'warning',
  15.     error: 'danger'
  16. }
  17.  
  18. class Notification extends React.Component {
  19.     render() {
  20. //       console.log(this.props);
  21.         let props = this.props;
  22.         let type = map[props.type] || 'info';
  23.         let className = ['alert', `alert-${type}`].join(' ');
  24.         if(props.message) {
  25.             return (
  26.                 <div className={className}>{props.message}</div>
  27.             )
  28.         }
  29.         return null;
  30.     }
  31. }
  32.  
  33. // TODO: Create a Confirmation Component
  34. class Confirmation extends React.Component {
  35. //   state = { clicked: false };
  36.   constructor(props) {
  37.     super(props);
  38.     this.state = { clicked: false };
  39.     this.handleClick = this.handleClick.bind(this);
  40.   }
  41.  
  42.   handleClick(type) {
  43.     return () => {
  44.       const { accept, decline} = this.props;
  45.       this.setState({ clicked: true });
  46.       if (type === 'accept') {
  47.         accept();
  48.       } else {
  49.         decline();
  50.       }
  51.     }
  52.   };
  53.  
  54.     render() {
  55.       console.log(this.props);
  56.       const { type, message } = this.props;
  57.       const messageComp = (
  58.         <React.Fragment>
  59.          <p>{message}</p>
  60.          <div className="btn btn-primary" onClick={this.handleClick('accept')}>Sure</div>
  61.          <div className="btn btn-danger" onClick={this.handleClick('decline')}>No Thanks</div>
  62.         </React.Fragment>
  63.       );
  64.         if(!this.state.clicked) {
  65.             return (
  66.               <Notification message={messageComp} type={type} />
  67.             )
  68.         }
  69.         return null;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement