Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. import React, { PureComponent } from "react";
  2. import "animate.css/animate.min.css";
  3. import ScrollAnimation from "react-animate-on-scroll";
  4.  
  5. class Form extends PureComponent {
  6. state = { error: false };
  7. input = ({ target: { value, name } }) => this.setState({ [name]: value });
  8. submitData = async () => {
  9. if (!this.handleValidation())return
  10. let body = this.state;
  11. body = JSON.stringify({ ...this.state, platform: "SPOT" });
  12. console.log(body)
  13. const data = await fetch("/working", {
  14. method: "POST",
  15. headers: {
  16. "Content-type": "application/json"
  17. },
  18. body
  19. });
  20. const json = await data.text();
  21. console.log(json);
  22. this.setState({done:true})
  23. };
  24.  
  25. handleValidation() {
  26. let fields = this.state;
  27. let formIsValid = true;
  28.  
  29. //Name
  30. if (!fields["name"]) {
  31. formIsValid = false;
  32. }
  33.  
  34. if (typeof fields["name"] !== "undefined") {
  35. if (!fields["name"].match(/^[a-zA-Z]+$/)) {
  36. formIsValid = false;
  37. }
  38. }
  39.  
  40.  
  41. //Email
  42. if (!fields["email"]) {
  43. formIsValid = false;
  44. }
  45.  
  46. if (typeof fields["email"] !== "undefined") {
  47. let lastAtPos = fields["email"].lastIndexOf("@");
  48. let lastDotPos = fields["email"].lastIndexOf(".");
  49.  
  50. if (
  51. !(
  52. lastAtPos < lastDotPos &&
  53. lastAtPos > 0 &&
  54. fields["email"].indexOf("@@") == -1 &&
  55. lastDotPos > 2 &&
  56. fields["email"].length - lastDotPos > 2
  57. )
  58. ) {
  59. formIsValid = false;
  60. }
  61. }
  62.  
  63. this.setState({ error: !formIsValid });
  64. return formIsValid;
  65. }
  66.  
  67.  
  68.  
  69. render() {
  70. return (
  71. <div className="div-block-24">
  72. <div className="div-block-26">
  73. <div className="div-block-30">
  74. <ScrollAnimation
  75. animateIn="fadeInUp"
  76. duration={1.5}
  77. animateOnce="true"
  78. >
  79. <p className="paragraph-12 responseT">
  80. We want to be in touch with you
  81. </p>
  82. </ScrollAnimation>
  83. <ScrollAnimation
  84. delay="1500"
  85. animateIn="fadeIn "
  86. duration={1.5}
  87. animateOnce="true"
  88. >
  89. <p className="paragraph-13">
  90. Do you have questions? Write us a message or schedule a meeting
  91. with us, we are waiting for you
  92. </p>
  93. </ScrollAnimation>
  94. </div>
  95. </div>
  96.  
  97. <div className="div-block-27">
  98. <ScrollAnimation
  99. delay="1000"
  100. animateIn="fadeInUp"
  101. duration={1.5}
  102. animateOnce="true"
  103. width="100"
  104. className="div-block-28"
  105. >
  106. <div class="">
  107. <div className="div-block-29">
  108. <div id="form" className="topBefore">
  109. <div className="form white">
  110. <input
  111. id="name"
  112. name="name"
  113. type="text"
  114. value={this.state.name}
  115. onChange={this.input}
  116. placeholder="Your name"
  117. className="mg-input"
  118. requiered
  119. />
  120. <input
  121. id="email"
  122. name="email"
  123. className=""
  124. value={this.state.email}
  125. onChange={this.input}
  126. type="text"
  127. placeholder="Email"
  128. requiered
  129. />
  130. </div>
  131. <textarea
  132. placeholder="Message"
  133. className="Titillium"
  134. name="message"
  135. value={this.state.message}
  136. onChange={this.input}
  137. />
  138. <br />
  139. <div className="btndiv">
  140. <button className="btn-2 _2 btn4" onClick={this.submitData}>
  141. CONTACT US
  142. </button>
  143. </div>
  144. </div>
  145. {this.state.done && (
  146. <div class="w-form-done" id="mensajecorreo">
  147. <div>Thank you! Your submission has been received!</div>
  148. </div>
  149. )}
  150. {this.state.error && (
  151. <div class="w-form-fail" id="mensajeerror">
  152. <div>
  153. Oops! Something went wrong while submitting the form.
  154. </div>
  155. </div>
  156. )}
  157. </div>
  158. </div>
  159. </ScrollAnimation>
  160. </div>
  161. </div>
  162. );
  163. }
  164. }
  165.  
  166. export default Form;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement