client/src/components/SessionList.js
author ymh <ymh.work@gmail.com>
Fri, 28 Jul 2017 19:40:35 +0200
changeset 129 d48946d164c6
parent 124 c77570164050
child 133 6f3078f7fd47
permissions -rw-r--r--
Add a first version of synchronisation Remove redux-offline dependency make the redux state fully immutable TODO: better error management TODO: make syncronization work automatically
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';
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    10
import { ActionEnum } from '../constants';
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
class SessionList extends Component {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    14
  state = {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    15
    showModal: false,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    16
    sessionToDelete: null
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    17
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    18
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
  createSession = () => {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    const sessionId = uuidV1();
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    this.props.sessionsActions.createSession(sessionId);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    this.props.history.push('/sessions/' + sessionId);
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    25
  onClickDelete(session, e) {
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    26
    e.preventDefault();
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    27
    e.stopPropagation();
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    28
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    29
    this.setState({
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    30
      showModal: true,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    31
      sessionToDelete: session
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    32
    })
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    33
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    34
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    35
  closeModal = () => {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    36
    this.setState({ showModal: false })
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    37
  }
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    38
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    39
  deleteSession = () => {
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    40
    const { sessionToDelete } = this.state;
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    41
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    42
    this.props.sessionsActions.deleteSession(sessionToDelete);
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    43
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    44
    this.setState({
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    45
      showModal: false,
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    46
      sessionToDelete: null
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    47
    })
93
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    48
  }
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    49
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
  render() {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    return (
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
      <div>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        <Navbar history={this.props.history} />
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        <Grid fluid>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
          <Row>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            <Col md={6} mdOffset={3}>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
              <ListGroup>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
                {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
    59
                  <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
    60
                    <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
    61
                    <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
    62
                      <span className="material-icons">delete</span>
469da13402e2 Allow to delete session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 62
diff changeset
    63
                    </a>
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                  </ListGroupItem>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
                )}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
              </ListGroup>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
              <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
    68
            </Col>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
          </Row>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        </Grid>
95
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    71
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    72
        <Modal show={ this.state.showModal } onHide={ this.closeModal }>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    73
          <Modal.Body>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    74
            Are you sure?
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    75
          </Modal.Body>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    76
          <Modal.Footer>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    77
            <Button bsStyle="primary" onClick={ this.deleteSession }>Confirm</Button>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    78
            <Button onClick={ this.closeModal }>Close</Button>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    79
          </Modal.Footer>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    80
        </Modal>
7bc08467c726 Add confirmation when deleting session.
Alexandre Segura <mex.zktk@gmail.com>
parents: 93
diff changeset
    81
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
      </div>
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    );
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
function mapStateToProps(state, props) {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
  return {
129
d48946d164c6 Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    89
    sessions: state.get('sessions').filter(session => session.get('action') !== ActionEnum.DELETED)
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
  };
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
function mapDispatchToProps(dispatch) {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
  return {
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    sessionsActions: bindActionCreators(sessionsActions, dispatch)
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
  }
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
}
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
export default connect(mapStateToProps, mapDispatchToProps)(SessionList);