client/src/components/Sessions.js
changeset 62 b2514a9bcd49
parent 61 7586b4a11c32
child 63 4088f8dc6b52
equal deleted inserted replaced
61:7586b4a11c32 62:b2514a9bcd49
     1 import React, { Component } from 'react';
       
     2 import { connect } from 'react-redux';
       
     3 import { bindActionCreators } from 'redux';
       
     4 import { Grid, Row, Col, ListGroup, ListGroupItem, Button } from 'react-bootstrap';
       
     5 import moment from 'moment';
       
     6 import '../App.css';
       
     7 import Navbar from './Navbar';
       
     8 import * as sessionsActions from '../actions/sessionsActions';
       
     9 
       
    10 class Sessions extends Component {
       
    11 
       
    12   createSession = () => {
       
    13     this.props.sessionsActions.createSession();
       
    14   }
       
    15 
       
    16   componentDidUpdate = () => {
       
    17     if (this.props.currentSession) {
       
    18       this.props.history.push('/sessions/' + this.props.currentSession._id)
       
    19     }
       
    20   }
       
    21 
       
    22   render() {
       
    23     return (
       
    24       <div>
       
    25         <Navbar history={this.props.history} />
       
    26         <Grid fluid>
       
    27           <Row>
       
    28             <Col md={6} mdOffset={3}>
       
    29               <ListGroup>
       
    30                 {this.props.sessions.map((session) =>
       
    31                   <ListGroupItem
       
    32                     key={session._id}
       
    33                     onClick={() => this.props.history.push('/sessions/' + session._id)}>
       
    34                     {session.title || 'No title'} {session._id} {moment(session.date).format('DD/MM/YYYY')}
       
    35                   </ListGroupItem>
       
    36                 )}
       
    37               </ListGroup>
       
    38               <Button bsStyle="success" onClick={this.createSession}>Create new session</Button>
       
    39             </Col>
       
    40           </Row>
       
    41         </Grid>
       
    42       </div>
       
    43     );
       
    44   }
       
    45 }
       
    46 
       
    47 function mapStateToProps(state, props) {
       
    48   return {
       
    49     currentSession: state.get('currentSession'),
       
    50     sessions: state.get('sessions')
       
    51   };
       
    52 }
       
    53 
       
    54 function mapDispatchToProps(dispatch) {
       
    55   return {
       
    56     sessionsActions: bindActionCreators(sessionsActions, dispatch)
       
    57   }
       
    58 }
       
    59 
       
    60 export default connect(mapStateToProps, mapDispatchToProps)(Sessions);