client/src/components/SessionList.js
author ymh <ymh.work@gmail.com>
Thu, 03 Aug 2017 23:04:33 +0200
changeset 137 279e1dffa213
parent 133 6f3078f7fd47
child 143 cfcbf4bc66f1
permissions -rw-r--r--
session is now created with current group and protocol
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import React, { Component } from 'react';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import { connect } from 'react-redux';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import { bindActionCreators } from 'redux';
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
     4
import { Grid, Row, Col, ListGroup, ListGroupItem, Button, Modal } from 'react-bootstrap';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import moment from 'moment';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import '../App.css';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import Navbar from './Navbar';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import * as sessionsActions from '../actions/sessionsActions';
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import uuidV1 from 'uuid/v1';
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    10
import { getActiveSessions } from '../selectors/coreSelectors';
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    11
import { getCurrentUser, getGroups, getCurrentGroup } from '../selectors/authSelectors';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
class SessionList extends Component {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    15
  state = {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    16
    showModal: false,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    17
    sessionToDelete: null
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    18
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    19
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
  createSession = () => {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    const sessionId = uuidV1();
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    22
    let groupName = null;
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    23
    let protocol = null;
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    24
    if(this.props.currentGroup) {
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    25
      groupName = this.props.currentGroup.get('name');
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    26
      protocol = this.props.currentGroup.get('protocol');
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    27
    }
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    28
    if(groupName === null) {
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    29
      groupName = (this.props.currentUser)?this.props.currentUser.get('default_group'):null;
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    30
      if(groupName != null) {
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    31
        const group = this.props.groups.find((g) => g.name === groupName);
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    32
        if(group) {
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    33
          protocol = group.get('protocol');
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    34
        }
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    35
      }
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    36
    }
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    37
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
    38
    this.props.sessionsActions.createSession(sessionId, groupName, protocol);
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    this.props.history.push('/sessions/' + sessionId);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    42
  onClickDelete(session, e) {
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    43
    e.preventDefault();
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    44
    e.stopPropagation();
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    45
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    46
    this.setState({
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    47
      showModal: true,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    48
      sessionToDelete: session
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    49
    })
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    50
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    51
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    52
  closeModal = () => {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    53
    this.setState({ showModal: false })
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    54
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    55
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    56
  deleteSession = () => {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    57
    const { sessionToDelete } = this.state;
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    58
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    59
    this.props.sessionsActions.deleteSession(sessionToDelete);
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    60
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    61
    this.setState({
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    62
      showModal: false,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    63
      sessionToDelete: null
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    64
    })
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    65
  }
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    66
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
  render() {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    return (
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
      <div>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        <Navbar history={this.props.history} />
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        <Grid fluid>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
          <Row>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            <Col md={6} mdOffset={3}>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
              <ListGroup>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                {this.props.sessions.map((session) =>
124
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    76
                  <ListGroupItem key={session.get('_id')}>
c77570164050 implement soft delete and indicator that session and notes have been modiied
ymh <ymh.work@gmail.com>
parents: 95
diff changeset
    77
                    <span onClick={() => this.props.history.push('/sessions/' + session.get('_id'))}>{session.title || 'No title'} {session.get('_id')} {moment(session.get('date')).format('DD/MM/YYYY')}</span>
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    78
                    <a className="pull-right" onClick={ this.onClickDelete.bind(this, session) }>
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    79
                      <span className="material-icons">delete</span>
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    80
                    </a>
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                  </ListGroupItem>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                )}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
              </ListGroup>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
              <Button bsStyle="success" onClick={this.createSession}>Create new session</Button>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            </Col>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
          </Row>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        </Grid>
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    88
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    89
        <Modal show={ this.state.showModal } onHide={ this.closeModal }>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    90
          <Modal.Body>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    91
            Are you sure?
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    92
          </Modal.Body>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    93
          <Modal.Footer>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    94
            <Button bsStyle="primary" onClick={ this.deleteSession }>Confirm</Button>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    95
            <Button onClick={ this.closeModal }>Close</Button>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    96
          </Modal.Footer>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    97
        </Modal>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    98
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
      </div>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
    );
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
function mapStateToProps(state, props) {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
  return {
137
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   106
    sessions: getActiveSessions(state),
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   107
    currentUser: getCurrentUser(state),
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   108
    groups: getGroups(state),
279e1dffa213 session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   109
    currentGroup: getCurrentGroup(state)
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
  };
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
function mapDispatchToProps(dispatch) {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
  return {
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 129
diff changeset
   115
    sessionsActions: bindActionCreators(sessionsActions, dispatch),
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
export default connect(mapStateToProps, mapDispatchToProps)(SessionList);