client/src/components/GroupForm.js
author ymh <ymh.work@gmail.com>
Thu, 03 Aug 2017 09:44:37 +0200
changeset 133 6f3078f7fd47
parent 132 906a6c7c7943
child 134 be36eed5e6e0
permissions -rw-r--r--
Work on correct protocol propagation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import React, { Component } from 'react';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import { connect } from 'react-redux';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import { bindActionCreators } from 'redux';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import { FormGroup, FormControl, Button, InputGroup, HelpBlock, Glyphicon } from 'react-bootstrap';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import * as authActions from '../actions/authActions';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
import * as sessionsActions from '../actions/sessionsActions';
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
class GroupForm extends Component {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
  state = {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    createGroup: false,
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    groupName: ''
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
  toggleCreateGroup = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    e.preventDefault();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    const { createGroup } = this.state;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    this.setState({ createGroup: !createGroup });
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
  onClickCreateGroup = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    e.preventDefault();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    const groupName = this.state.groupName;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    this.props.sessionsActions.createGroupAndUpdateSession(this.props.session, groupName);
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    this.setState({
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
      createGroup: false,
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      groupName: ''
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    })
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
  handleInputChange = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    const target = e.target;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    const value = target.value;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    const name = target.name;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    this.setState({
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
      [name]: value
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    });
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
  render = () => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    const { createGroup } = this.props;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    const hasErrors = true === createGroup.get('error') && createGroup.get('errorMessages').has('name');
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    let errors = [];
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    if (hasErrors) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
      const errorMessages = createGroup.get('errorMessages').toArray();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
      errors = errorMessages.map((message, key) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
          <HelpBlock key={ key }>{ message }</HelpBlock>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        )
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
      })
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    if (this.state.createGroup) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
      return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        <FormGroup validationState={ hasErrors ? 'error' : null }>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
          <InputGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            <FormControl
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
              type="text"
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
              placeholder="Enter a name for your group"
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
              onChange={this.handleInputChange}
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
              name="groupName"
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
              value={this.state.groupName} />
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            <InputGroup.Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
              <Button bsStyle="primary" onClick={ this.onClickCreateGroup }>Create</Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            </InputGroup.Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
          </InputGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
          { errors }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
          <hr />
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
          <Button onClick={ this.toggleCreateGroup }>Cancel</Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        </FormGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
      )
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    if(this.props.online) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
      return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        <FormGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
          <Button className="pull-right" bsSize="small" onClick={ this.toggleCreateGroup }>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            <Glyphicon glyph="plus" />  Create a new group
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
          </Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        </FormGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
      )
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
    }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    return null;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
}
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
function mapStateToProps(state, props) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
  return {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    createGroup: state.get('createGroup'),
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    online: state.getIn(['status', 'online']),
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
  };
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
}
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
function mapDispatchToProps(dispatch) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
  return {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    sessionsActions: bindActionCreators(sessionsActions, dispatch),
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    authActions: bindActionCreators(authActions, dispatch),
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
  }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
}
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
export default connect(mapStateToProps, mapDispatchToProps)(GroupForm);