Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { Field, reduxForm } from 'redux-form';
  4. import { registerUser } from '../../actions/auth';
  5. import { ButtonToolbar, DropdownButton, MenuItem, FormControl, FormGroup, ControlLabel,
  6. Panel, Button } from 'react-bootstrap';
  7.  
  8. const form = reduxForm({
  9. form: 'register',
  10. validate,
  11. });
  12.  
  13. const renderField = field => (
  14. <div>
  15. <input className="form-control" {...field.input} />
  16. {field.touched && field.error && <div className="error">{field.error}</div>}
  17. </div>
  18. );
  19.  
  20. const panelStyle = {
  21. 'max-width': '520px',
  22. };
  23.  
  24. const NFLTEAMS = ['Cardinals','Falcons','Ravens','Bills', 'Panthers', 'Bears',
  25. 'Bengals', 'Browns','Cowboys', 'Broncos', 'Lions', 'Packers', 'Texans', 'Colts',
  26. 'Jaguars', 'Chiefs', 'Dolphins', 'Vikings', 'Patriots', 'Saints', 'Giants',
  27. 'Jets', 'Raiders', 'Eagles', 'Steelers', 'Chargers', '49ers', 'Seahawks',
  28. 'Rams', 'Buccaneers', 'Titans', 'Redskins'];
  29.  
  30. function renderTeamDropDown(team, i) {
  31. return (
  32. <option value={team}>{team}</option>
  33. );
  34. }
  35.  
  36. function FieldGroup({ id, label, help, inputRef, ...props }) {
  37. return (
  38. <FormGroup controlId={id}>
  39. <ControlLabel>{label}</ControlLabel>
  40. <FormControl {...props} inputRef={inputRef}/>
  41. {help && <HelpBlock>{help}</HelpBlock>}
  42. </FormGroup>
  43. );
  44. }
  45.  
  46. function TeamSelector({label, teamarray}) {
  47. return (
  48. <FormGroup controlId="formControlsSelect">
  49. <ControlLabel>{label}</ControlLabel>
  50. <FormControl componentClass="select" placeholder="select">
  51. <option value="select"></option>
  52. {teamarray.sort().map(renderTeamDropDown)}
  53. </FormControl>
  54. </FormGroup>
  55. )
  56.  
  57. }
  58.  
  59. function validate(formProps) {
  60. const errors = {};
  61.  
  62. if (!formProps.firstName) {
  63. errors.firstName = 'Please enter a first name';
  64. }
  65.  
  66. if (!formProps.lastName) {
  67. errors.lastName = 'Please enter a last name';
  68. }
  69.  
  70. if (!formProps.email) {
  71. errors.email = 'Please enter an email';
  72. }
  73.  
  74. if (!formProps.password) {
  75. errors.password = 'Please enter a password';
  76. }
  77.  
  78. return errors;
  79. }
  80.  
  81. class Register extends Component {
  82. constructor(props) {
  83. super(props);
  84.  
  85. this.handleClick = this.handleClick.bind(this);
  86. this.handleFormSubmit = this.handleFormSubmit.bind(this);
  87. this.handleChange = this.handleChange.bind(this);
  88. }
  89.  
  90. renderTeamDropDown(team, i) {
  91. return (
  92. <option value={team}>{team}</option>
  93. );
  94. }
  95.  
  96. handleFormSubmit(formProps) {
  97. alert('Text field value is: ' + this.username.value);
  98. this.props.registerUser(this.username.value);
  99. }
  100.  
  101. handleChange = () => {
  102. console.log(this.username.value);
  103. }
  104.  
  105. renderAlert() {
  106. if (this.props.errorMessage) {
  107. return (
  108. <div>
  109. <span><strong>Error!</strong> {this.props.errorMessage}</span>
  110. </div>
  111. );
  112. }
  113. }
  114.  
  115. render() {
  116. const { handleSubmit } = this.props;
  117.  
  118. var inputStyle = {
  119. width: 175,
  120. };
  121.  
  122. const panelStyle = {
  123. 'max-width': '350px',
  124. 'margin-left': 'auto',
  125. 'margin-right': 'auto',
  126. };
  127.  
  128. const tempStyle = {
  129. width: '100%',
  130. }
  131.  
  132. const headerStyle = {
  133. margin: '0 0 15px 0',
  134. }
  135.  
  136. return (
  137. <div style={tempStyle}>
  138. <Panel style={panelStyle}>
  139. <h2 style={headerStyle}>Create account</h2>
  140. <form>
  141. <FieldGroup
  142. id="formControlsText"
  143. type="text"
  144. label="Username"
  145. placeholder="Enter username"
  146. inputRef={(input) => { this.username = input; }}
  147. onChange={this.handleChange}
  148. />
  149. <FieldGroup
  150. id="formControlsText2"
  151. type="text"
  152. label="Full name"
  153. placeholder="Enter full name"
  154. />
  155. <FieldGroup
  156. id="formControlsEmail"
  157. type="email"
  158. label="Email"
  159. placeholder="Enter email"
  160. />
  161. <FieldGroup
  162. id="formControlsPassword"
  163. type="password"
  164. label="Password"
  165. />
  166. <TeamSelector label="Favorite MLB Team" teamarray={NFLTEAMS}/>
  167. <TeamSelector label="Favorite NBA Team" teamarray={NFLTEAMS}/>
  168. <TeamSelector label="Favorite NHL Team" teamarray={NFLTEAMS}/>
  169. <TeamSelector label="Favorite NFL Team" teamarray={NFLTEAMS}/>
  170. <Button style={tempStyle} onClick={this.handleFormSubmit}>
  171. Create your Max Break account
  172. </Button>
  173. </form>
  174. </Panel>
  175. </div>
  176. );
  177. }
  178.  
  179. handleClick( evt ) {
  180. this.refs.mlbDropdown.title = evt;
  181. console.log(evt);
  182. }
  183. }
  184.  
  185. function mapStateToProps(state) {
  186. return {
  187. errorMessage: state.auth.error,
  188. message: state.auth.message,
  189. authenticated: state.auth.authenticated,
  190. };
  191. }
  192.  
  193. export default connect(mapStateToProps, { registerUser })(form(Register));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement