Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import axios from 'axios'
  3. import 'semantic-ui-less/semantic.less'
  4. import { Message } from 'semantic-ui-react'
  5. import ReCaptcha from 'react-google-invisible-recaptcha';
  6. import {
  7.   formSubmittedEvent,
  8.   formSubmittedIdentifyCall,
  9.   linkedInConversionTrackingEvent
  10. } from '../segment/index';
  11.  
  12. class Contact extends React.Component {
  13.   constructor(props) {
  14.     super(props)
  15.     this.domRef = React.createRef()
  16.     this.state = { feedbackMsg: null, isChecked: true }
  17.     this.handleClick = this.handleClick.bind(this)
  18.   }
  19.   encode(data) {
  20.     return Object.keys(data)
  21.       .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
  22.       .join('&')
  23.   }
  24.  
  25.   handleClick() {
  26.     console.log(this.state.isChecked)
  27.     this.setState(prevState => ({
  28.       isChecked: !prevState.isChecked
  29.     }))
  30.     if (!this.state.isChecked) {
  31.       document.getElementById('mailinglist').setAttribute('value', 'on')
  32.     } else {
  33.       document.getElementById('mailinglist').setAttribute('value', 'off')
  34.     }
  35.   }
  36.  
  37.   handleSubmit(event) {
  38.     // Do not submit form via HTTP, since we're doing that via XHR request.
  39.     event.preventDefault()
  40.     this.recaptcha.execute()
  41.     // Loop through this component's refs (the fields) and add them to the
  42.     // formData object. What we're left with is an object of key-value pairs
  43.     // that represent the form data we want to send to Netlify.
  44.     const formData = {}
  45.     Object.keys(this.refs).map(key => (formData[key] = this.refs[key].value))
  46.  
  47.     formSubmittedIdentifyCall(formData, this.state.isChecked)
  48.     formSubmittedEvent(formData, this.state.isChecked)
  49.     linkedInConversionTrackingEvent()
  50.  
  51.     const axiosOptions = {
  52.       url: window.location.href,
  53.       method: 'post',
  54.       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  55.       data: this.encode(formData)
  56.     }
  57.  
  58.     console.log(axiosOptions)
  59.     console.log(formData)
  60.     // Submit to Netlify. Upon success, set the feedback message and clear all
  61.     // the fields within the form. Upon failure, keep the fields as they are,
  62.     // but set the feedback message to show the error state.
  63.  
  64.     axios(axiosOptions)
  65.       .then(response => {
  66.         console.log(formData)
  67.         console.log(formData.mailinglist)
  68.         this.setState({
  69.           feedbackMsg: "Thank you! We'll reach out to you shortly."
  70.         })
  71.         this.domRef.current.reset()
  72.       })
  73.       .catch(err => {
  74.         console.log(err)
  75.       })
  76.   }
  77.   onResolved = () => {
  78.     this.setState({ messageSent: true })
  79.     console.log(this.state)
  80.   }
  81.   render() {
  82.     return (
  83.       <div>
  84.         <form
  85.           className="ui form"
  86.           ref={this.domRef}
  87.           name="New Contact"
  88.           method="POST"
  89.           data-netlify="true"
  90.           netlify-honeypot="bot-field"
  91.           onSubmit={event => this.handleSubmit(event)}
  92.         >
  93.           <input
  94.             ref="form-name"
  95.             type="hidden"
  96.             name="form-name"
  97.             value="New Contact"
  98.           />
  99.           <p class="hidden">
  100.             {/* <label><input ref="pathname" type="text" name="pathname" value={window.location.href} /></label> */}
  101.             <label>
  102.               Don’t fill this out: <input ref="bot-field" name="bot-field" />
  103.             </label>
  104.           </p>
  105.           <div className="two fields">
  106.             <p className="field">
  107.               <input
  108.                 ref="first-name"
  109.                 type="text"
  110.                 placeholder="First Name"
  111.                 name="first-name"
  112.                 required
  113.               />
  114.             </p>
  115.             <p className="field">
  116.               <input
  117.                 ref="last-name"
  118.                 type="text"
  119.                 placeholder="Last Name"
  120.                 name="last-name"
  121.                 required
  122.               />
  123.             </p>
  124.           </div>
  125.           <div className="two fields">
  126.             <p className="field">
  127.               <input
  128.                 ref="email"
  129.                 type="email"
  130.                 placeholder="Email Address"
  131.                 name="email"
  132.                 required
  133.               />
  134.             </p>
  135.             <p className="field service-field">
  136.               <input
  137.                 ref="service"
  138.                 type="text"
  139.                 placeholder="Which of our services are you interested in?"
  140.                 name="service"
  141.               />
  142.             </p>
  143.           </div>
  144.           <div className="two fields">
  145.             <p className="field company-field">
  146.               <input
  147.                 ref="company"
  148.                 type="text"
  149.                 placeholder="Company"
  150.                 name="company"
  151.               />
  152.             </p>
  153.           </div>
  154.           <p className="field">
  155.             <label>
  156.               <textarea
  157.                 ref="message"
  158.                 name="message"
  159.                 placeholder="How can we help you?"
  160.               />
  161.             </label>
  162.           </p>
  163.           <div className="field">
  164.             <div className="ui checkbox">
  165.               <input
  166.                 type="checkbox"
  167.                 tabIndex="0"
  168.                 name="mailinglist"
  169.                 ref="mailinglist"
  170.                 id="mailinglist"
  171.                 onClick={this.handleClick}
  172.                 defaultChecked={this.state.isChecked}
  173.               />
  174.               <label>Add me to the Ragnarok List to stay up to date!</label>
  175.             </div>
  176.           </div>
  177.           <p>
  178.             <button
  179.               id="get_in_touch_btn"
  180.               className="ui button"
  181.               type="submit"
  182.               onclick="return gtag_report_conversion('https://www.ragnaroknyc.com/')"
  183.             >
  184.               CONTACT US
  185.             </button>
  186.           </p>
  187.           {this.state.feedbackMsg && (
  188.             <Message positive header={this.state.feedbackMsg} />
  189.           )}
  190.         </form>
  191.         <ReCaptcha
  192.           ref={ref => (this.recaptcha = ref)}
  193.           sitekey={`${process.env.GATSBY_NEW_GOOGLE_SITE_KEY}`}
  194.           onResolved={this.onResolved}
  195.         />
  196.       </div>
  197.     )
  198.   }
  199. }
  200.  
  201. export default Contact
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement