diff -r 6f3078f7fd47 -r be36eed5e6e0 client/src/components/CreateGroup.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/src/components/CreateGroup.js Thu Aug 03 17:33:00 2017 +0200 @@ -0,0 +1,124 @@ +import React, { Component } from 'react'; +import { connect } from 'react-redux'; +import { bindActionCreators } from 'redux'; +import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button, Alert, HelpBlock } from 'react-bootstrap'; +import '../App.css'; +import Navbar from './Navbar'; +import * as authActions from '../actions/authActions'; +import { getOnline, getCreateGroup } from '../selectors/authSelectors'; + +class CreateGroup extends Component { + + state = { + name: '', + description: '' + } + + createGroup = () => { + + const { name, description } = this.state; + + if(name && name.trim() !== "") { + this.props.authActions.createGroup(name, description); + } + } + + submit = (e) => { + e.preventDefault(); + + this.createGroup(); + } + + handleInputChange = (e) => { + const target = e.target; + const value = target.value; + const name = target.name; + + this.setState({ + [name]: value + }); + } + + renderErrorMessage(errorMessages, fieldname) { + if (errorMessages && fieldname in errorMessages) { + return errorMessages[fieldname].map((message, key) => + { message } + ); + } + } + + renderNonFieldErrors(errorMessages) { + if (errorMessages && 'non_field_errors' in errorMessages) { + const errors = errorMessages['non_field_errors']; + return ( + + { errors.map((message, key) => +

{ message }

+ ) } +
+ ) + } + } + + cancel = (e) => { + e.preventDefault(); + this.props.history.push('/sessions'); + } + + + render() { + + const panelHeader = ( +

Créer un groupe

+ ) + + const errorMessages = this.props.createGroup.getIn(['errorMessages', 'data']); + const okDisabled = (!this.state.name || this.state.name.trim() === ""); + + return ( +
+ + + + + +
+ + Nom + + { this.renderErrorMessage(errorMessages, 'name') } + + + Password + + { this.renderErrorMessage(errorMessages, 'description') } + + { this.renderNonFieldErrors(errorMessages) } + + + + +
+
+ +
+
+
+ ); + } +} + +function mapStateToProps(state, props) { + return { + createGroup: getCreateGroup(state), + online: getOnline(state), + }; +} + +function mapDispatchToProps(dispatch) { + return { + authActions: bindActionCreators(authActions, dispatch), + } +} + +export default connect(mapStateToProps, mapDispatchToProps)(CreateGroup);