Search code examples
javascriptreactjssemantic-ui

State not appearing, updating or being passed to value of checkboxes from semantic


EDIT: Require checkbox to be checked is resolved, just cant work out how to get the checkbox values into the email.

The aim: Pass the value of state.TNC and state.promos into the emailjs.sendForm. The form should return alert "You need to accept Terms and Conditions before proceeding.

I am lost for words as to what I need to google to find a resolution.

Here is my code that I thought was going to do that for me.

sendEmail = (e, props) => {
    e.preventDefault();
    if (this.state.TNC !== true) {
      return emailjs
        .sendForm("test", "testTemp", e.target, "user_EPYkKnHwljQTCbuiyfJkzO7YD")
        .then(
          (result) => {
            alert("Email sent successfully!");
            e.target.reset();
          },
          (error) => {
            alert("Email send failed... :( I had one job...");
          }
        );
    } else {
      return alert(
        "You need to accept the Terms and Conditions before proceeding."
      );
    }
  };

The code for the two checkboxes, (I have tried them with and without the value={this.state.TNC})

<Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>

Here is the full code, and you can find a semi-functional version of the form at https://test.ghostrez.net

import React from "react";
import emailjs from "emailjs-com";
import { Component } from "react";
import { Button, Checkbox, Form, Input, TextArea } from "semantic-ui-react";

export default class ContactUs extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TNC: false,
      promos: true,
    };
  }
  onToggle = () => {
    const TNC = !this.state.TNC;
    this.setState({ TNC });
  };
  onTogglePromos = () => {
    const promos = !this.state.promos;
    this.setState({ promos });
  };
  sendEmail = (e, props) => {
    e.preventDefault();
    if (this.state.TNC !== true) {
      return emailjs
        .sendForm("test", "testTemp", e.target, "user_EPYkKnHwljQTCbJkzO7YD")
        .then(
          (result) => {
            alert("Email sent successfully!");
            e.target.reset();
          },
          (error) => {
            alert("Email send failed... :( I had one job...");
          }
        );
    } else {
      return alert(
        "You need to accept the Terms and Conditions before proceeding."
      );
    }
  };

  render() {
    return (
      <Form onSubmit={this.sendEmail}>
        <Form.Group widths="equal">
          <Form.Field
            id="firstName"
            control={Input}
            label="First name"
            name="firstName"
            placeholder="First name"
            required
          />
          <Form.Field
            id="lastName"
            name="lastName"
            control={Input}
            label="Last name"
            placeholder="Last name"
          />
        </Form.Group>
        <Form.Group widths="equal">
          <Form.Field
            id="Email"
            control={Input}
            label="Email"
            name="email"
            placeholder="joe@schmoe.com"
            onChange=""
            required
          />
          <Form.Field
            id="Phone"
            control={Input}
            label="Phone"
            name="phone"
            placeholder="0469 420 689"
            required
          />
        </Form.Group>
        <Form.Field
          id="Message"
          control={TextArea}
          label="Message"
          name="message"
          placeholder="Message"
          required
        />
        <Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>
        <Form.Field
          id="Submit"
          control={Button}
          type="submit"
          value="submit"
          positive
          content="Submit"
        />
      </Form>
    );
  }
}

Thank you for your help, I really appreciate it.


Solution

  • It's the way you have written the logic

    this.state.TNC !== true, what does this equate to? false !== true which is true, hence it goes in the if block. I think what you want is if(this.state.TNC), i.e. if it's true, then go in the if block.

    Also, your Email field throws an error because it's onChange is an empty string.