Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Input from '../../../src/components/common/input';
  3. import ReactSimpleTooltip from 'react-simple-tooltip';
  4. import segment from '../../../src/segment/segment';
  5.  
  6. export type FormInfo = {
  7. userName: string;
  8. address: string;
  9. phoneNumber: string;
  10. email: string;
  11. };
  12.  
  13. type PropTypes = {
  14. userName: string;
  15. address: string;
  16. email: string;
  17. phoneNumber: string;
  18. company: string;
  19. onSubmit: (formInfo: FormInfo) => void;
  20. };
  21.  
  22. type StateType = {
  23. errors: { [key: string]: string };
  24. formInfo: FormInfo;
  25. company: string;
  26. showFullForm: boolean;
  27. isFormValid: boolean;
  28. addressIsValid?: boolean;
  29. };
  30.  
  31. interface OnChangeEvent {
  32. persist?: () => void;
  33. target: { value: string; name: string };
  34. isValid: boolean;
  35. }
  36.  
  37. class GetStartedForm extends Component<PropTypes> {
  38. state: StateType = {
  39. errors: {},
  40. formInfo: {
  41. userName: this.props.userName,
  42. phoneNumber: this.props.phoneNumber,
  43. address: this.props.address,
  44. email: this.props.email
  45. },
  46. isFormValid: true,
  47. company: this.props.company,
  48. showFullForm: false,
  49. addressIsValid: true
  50. };
  51.  
  52. isFormValid = errors => {
  53. const isValid = Object.keys(errors).length === 0;
  54. return isValid;
  55. };
  56.  
  57. toggleForm = () => {
  58. this.setState((currentState: StateType) => ({
  59. showFullForm: !currentState.showFullForm
  60. }));
  61. };
  62.  
  63. onChange = (ev: OnChangeEvent) => {
  64. ev.persist && ev.persist();
  65.  
  66. const name = ev.target.name;
  67. this.setState((currentState: StateType) => {
  68. const newFormInfo = {
  69. ...currentState.formInfo,
  70. [name]: ev.target.value
  71. };
  72.  
  73. const errors = {
  74. ...currentState.errors
  75. };
  76.  
  77. if (!ev.isValid) {
  78. errors[name] = 'invalid field';
  79. } else {
  80. delete errors[name];
  81. }
  82.  
  83. return { formInfo: newFormInfo, errors };
  84. });
  85. };
  86.  
  87. render() {
  88. return (
  89. <div className="users-data-container">
  90. <div className="users-data-input-container">
  91. <Input
  92. value={this.state.formInfo.userName}
  93. placeholder="Name"
  94. onChange={this.onChange}
  95. classNames={'info-username'}
  96. inputName="userName"
  97. type="name"
  98. disableErrors={true}
  99. />
  100. {!this.state.showFullForm && (
  101. <img
  102. onClick={() => {
  103. segment.trackEvent(
  104. 'Clicked',
  105. 'edit your profile',
  106. 'nta-partners'
  107. );
  108. this.setState({ showFullForm: true });
  109. }}
  110. src="/static/img/nta-partners/svg-icons/icon-edit-pencil.svg"
  111. className="info-pencil"
  112. />
  113. )}
  114. {this.state.showFullForm && (
  115. <Input
  116. value={this.state.formInfo.email}
  117. placeholder="Email"
  118. onChange={this.onChange}
  119. classNames={'info-username'}
  120. type="email"
  121. required={true}
  122. inputName="email"
  123. disableErrors={true}
  124. />
  125. )}
  126. {this.state.showFullForm && (
  127. <div className="info-username no-border info-phone-number">
  128. {this.state.formInfo.phoneNumber ||
  129. 'You have to validate your phone number!'}
  130. <ReactSimpleTooltip
  131. arrow={15}
  132. background="#3084d5"
  133. border="none"
  134. color="white"
  135. content={
  136. this.props.phoneNumber
  137. ? 'We`ve already confirmed your phone number'
  138. : 'You have to validate your phone number!'
  139. }
  140. fadeDuration={0.5}
  141. fadeEasing="linear"
  142. fixed={false}
  143. fontFamily="inherit"
  144. fontSize="12px"
  145. offset={0}
  146. padding={15}
  147. placement="bottom"
  148. radius={5}
  149. zIndex={2}
  150. >
  151. <span className="info-wrapper">
  152. <i className="info-tooltip">i</i>
  153. </span>
  154. </ReactSimpleTooltip>
  155. </div>
  156. )}
  157. {this.state.showFullForm && (
  158. <Input
  159. type="address"
  160. placeholder="Address"
  161. classNames="info-username"
  162. value={this.state.formInfo.address}
  163. onChange={this.onChange}
  164. inputName={'address'}
  165. disableErrors={true}
  166. />
  167. )}
  168.  
  169. {this.state.showFullForm && (
  170. <div className="toggle-form-out" onClick={this.toggleForm}>
  171. <i className="fas fa-angle-up" />
  172. </div>
  173. )}
  174. </div>
  175. <div className="get-started-button-container">
  176. <button
  177. disabled={
  178. !this.props.phoneNumber ||
  179. Object.keys(this.state.errors).length > 0
  180. }
  181. className="info-button"
  182. onClick={() => {
  183. segment.trackEvent('Clicked', 'get-started', 'nta-partners');
  184. this.props.onSubmit(this.state.formInfo);
  185. }}
  186. >
  187. Get Started
  188. </button>
  189. </div>
  190. </div>
  191. );
  192. }
  193. }
  194.  
  195. export default GetStartedForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement