Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. // @Flow
  2.  
  3. import React from 'react';
  4. import { Field, reduxForm } from 'redux-form';
  5.  
  6. import { Fieldset, Form, Button, FormGroup, FormHelper, TextBox } from 'conferize-frontend';
  7.  
  8. const validateInput = (values, props) => {
  9. const SignUpFormHelper = props.SignUpFormHelper;
  10. const fieldsMetaInfo = SignUpFormHelper.fieldsMetaInfo;
  11.  
  12. const errors = SignUpFormHelper.triggerActiveServerErrors(values, props.touch);
  13. if (!SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.firstName)) {
  14. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.firstName, 'Please add a name');
  15. }
  16.  
  17. if (!SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.lastName)) {
  18. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.lastName, 'Please add a last name');
  19. }
  20.  
  21. const userNameValue = SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.userName);
  22. if (!userNameValue) {
  23. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.userName, 'Please add a username');
  24. } else if (userNameValue.length < 3) {
  25. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.userName, 'Username too small. Try a longer username');
  26. } else if (userNameValue.length > 15) {
  27. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.userName, 'Username can not have more than 15 characters');
  28. }
  29.  
  30. const emailValue = SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.email);
  31. if (!emailValue) {
  32. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.email, 'Please add an email');
  33. } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(emailValue)) {
  34. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.email, 'Invalid email address');
  35. }
  36.  
  37. const passwordValue = SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.password);
  38. if (!passwordValue) {
  39. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.password, 'Please add a password');
  40. } else if (passwordValue.length < 6) {
  41. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.password, 'Password must have at least 6 characters');
  42. }
  43.  
  44. const confirmPasswordValue = SignUpFormHelper.getFieldValue(values, fieldsMetaInfo.password);
  45. if (!confirmPasswordValue) {
  46. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.confirmPassword, 'Please confirm the password');
  47. } else if (passwordValue !== confirmPasswordValue) {
  48. SignUpFormHelper.setFieldValue(errors, fieldsMetaInfo.confirmPassword, 'The password must coincide');
  49. }
  50. return errors;
  51. };
  52.  
  53. /* SignUpForm ReduxForm */
  54.  
  55. type SignUpReduxFormProps = {
  56.  
  57. /** FormHelper. */
  58. SignUpFormHelper: FormHelper,
  59.  
  60. /** Will be true if there is any asynch operation going on. */
  61. submitting: boolean,
  62.  
  63. /** Will be true if there is at least one input with errors. */
  64. invalid: boolean,
  65.  
  66. /** Will be true if the form data is the same as its initialized values. Opposite of dirty. */
  67. pristine: boolean,
  68.  
  69. /** Action specifies where to send the form-data when a form is submitted. */
  70. action: string,
  71.  
  72. /** Method specifies how to send form-data. */
  73. method: string,
  74. };
  75.  
  76. /**
  77. * SignUpReduxForm component to be connected to redux-form.
  78. */
  79.  
  80. // Use named export for unconnected redux component (for tests)
  81. export const SignUpReduxForm = ({
  82. SignUpFormHelper,
  83. submitting,
  84. invalid,
  85. pristine,
  86. action,
  87. method }: SignUpReduxFormProps,
  88. ) => (
  89. <Form action={action} method={method}>
  90. <Fieldset>
  91. <FormGroup>
  92. <Field name={SignUpFormHelper.formFieldNames.firstName} component={TextBox} placeholder="First name" required htmlFor="firstName" />
  93. </FormGroup>
  94. <FormGroup>
  95. <Field name={SignUpFormHelper.formFieldNames.lastName} component={TextBox} placeholder="Last name" required htmlFor="lastName" />
  96. </FormGroup>
  97. <FormGroup>
  98. <Field name={SignUpFormHelper.formFieldNames.userName} component={TextBox} placeholder="Username" required htmlFor="userName" />
  99. </FormGroup>
  100. <FormGroup>
  101. <Field name={SignUpFormHelper.formFieldNames.email} component={TextBox} placeholder="Email" required htmlFor="email" />
  102. </FormGroup>
  103. <FormGroup>
  104. <Field name={SignUpFormHelper.formFieldNames.password} component={TextBox} placeholder="Password" required type="password" htmlFor="password" />
  105. </FormGroup>
  106. <FormGroup>
  107. <Field name={SignUpFormHelper.formFieldNames.confirmPassword} component={TextBox} placeholder="Repeat Password" required type="password" htmlFor="confirmPassword" />
  108. </FormGroup>
  109. </Fieldset>
  110. <br />
  111. <div className="row">
  112. <div className="col-xs-12 end-xs">
  113. <Button variant="primary" disabled={invalid || pristine || submitting}>Sign In</Button>
  114. </div>
  115. </div>
  116. </Form>
  117. );
  118.  
  119. /* Decorate the form component */
  120. const SignUpReduxFormConnected = reduxForm({
  121. form: 'SignUpForm', // a unique name for this form
  122. validate: validateInput,
  123. })(SignUpReduxForm);
  124.  
  125. /* SignUpForm */
  126.  
  127. type Props = {
  128. /** This is the description of prop1 */
  129. fields: {
  130. [fieldName:
  131. 'user[first_name]' |
  132. 'user[last_name]' |
  133. 'user[user_name]' |
  134. 'user[email]' |
  135. 'user[password]' |
  136. 'user[confirm_password]'
  137. ]: {
  138. value: string,
  139. errors: Array<string>
  140. }
  141. },
  142.  
  143. /** Action specifies where to send the form-data when a form is submitted. */
  144. action: string,
  145.  
  146. /** Method specifies how to send form-data. */
  147. method: string,
  148. };
  149.  
  150. /**
  151. * SignUpForm component.
  152. */
  153. const SignUpForm = ({ fields, action, method }: Props) => {
  154. const formFieldNames = {
  155. firstName: 'user[first_name]',
  156. lastName: 'user[last_name]',
  157. userName: 'user[user_name]',
  158. email: 'user[email]',
  159. password: 'user[password]',
  160. confirmPassword: 'user[confirm_password]',
  161. };
  162.  
  163. const SignUpFormHelper = new FormHelper(fields, formFieldNames);
  164.  
  165. return (
  166. <SignUpReduxFormConnected
  167. initialValues={SignUpFormHelper.initialValues}
  168. SignUpFormHelper={SignUpFormHelper}
  169. action={action}
  170. method={method}
  171. />
  172. );
  173. };
  174.  
  175. export default SignUpForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement