client/src/components/GroupForm.js
author ymh <ymh.work@gmail.com>
Thu, 03 Aug 2017 17:33:00 +0200
changeset 134 be36eed5e6e0
parent 132 906a6c7c7943
permissions -rw-r--r--
add menu to change current group and create a new group
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';
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
     7
import { getOnline, getCreateGroup } from '../selectors/authSelectors';
132
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
class GroupForm extends Component {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
  state = {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    createGroup: false,
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    groupName: ''
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
  toggleCreateGroup = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    e.preventDefault();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    const { createGroup } = this.state;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    this.setState({ createGroup: !createGroup });
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
  onClickCreateGroup = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    e.preventDefault();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    const groupName = this.state.groupName;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    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
    26
    this.setState({
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      createGroup: false,
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
      groupName: ''
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
  handleInputChange = (e) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    const target = e.target;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    const value = target.value;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    const name = target.name;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    this.setState({
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
      [name]: value
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
  render = () => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    const { createGroup } = this.props;
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    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
    46
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    let errors = [];
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    if (hasErrors) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
      const errorMessages = createGroup.get('errorMessages').toArray();
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
      errors = errorMessages.map((message, key) => {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
          <HelpBlock key={ key }>{ message }</HelpBlock>
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    if (this.state.createGroup) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        <FormGroup validationState={ hasErrors ? 'error' : null }>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
          <InputGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            <FormControl
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
              type="text"
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
              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
    64
              onChange={this.handleInputChange}
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
              name="groupName"
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
              value={this.state.groupName} />
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            <InputGroup.Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
              <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
    69
            </InputGroup.Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
          </InputGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
          { errors }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
          <hr />
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
          <Button onClick={ this.toggleCreateGroup }>Cancel</Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        </FormGroup>
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    if(this.props.online) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
      return (
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        <FormGroup>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
          <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
    82
            <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
    83
          </Button>
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        </FormGroup>
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
    }
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    return null;
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
function mapStateToProps(state, props) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
  return {
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    95
    createGroup: getCreateGroup(state),
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 132
diff changeset
    96
    online: getOnline(state),
132
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
function mapDispatchToProps(dispatch) {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
  return {
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    sessionsActions: bindActionCreators(sessionsActions, dispatch),
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    authActions: bindActionCreators(authActions, dispatch),
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
906a6c7c7943 add group to sync + create groups + various component cleaning
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
export default connect(mapStateToProps, mapDispatchToProps)(GroupForm);