fac19 / work-in-progress

Weeks 11-13 Student Project
https://wip-app.netlify.app/
3 stars 0 forks source link

document.querySelector @ LoginForm lines 35/36 #147

Closed Chloeh24 closed 4 years ago

Chloeh24 commented 4 years ago

You shouldn't really use the DOM in React since there isn't a real DOM.

E.G. (this isn't tested but we've done a similar thing)

const LogInForm = (props) => {
  const [error, setError] = React.useState("");
  const [form, setForm] = React.useState({ username: "", password: "" });

  const handleEvent = (e) => {
    let { name, value } = e.target;
    setForm({ ...form, [name]: value });
  };

  const handleSubmit = (event) => {
    event.preventDefault();

    logInPost(form)
      .then(() => history.push("/feed"))
      .catch((error) => setError(error));
  };

Add handle Event to inputs
 <input
          type="text"
          id="username"
          placeholder="username"
          name="username"
          required
         ** onChange={handleEvent}**
          autoFocus
        ></input>

Can add error at bottom of form
  {error ? <p>{error}</p> : ""} 

Post-fetch.js


function logInPost(logInFormData) {
  const options = {
    endpoint: "logIn",
    body: logInFormData,
    error: "Could not log you in",
  };
  return postFetch(options).then((res) => {
    localStorage.setItem("auth", res.token);
  });
}

Also could pass the error from postFetch function as part of the response to the loginForm

Disclaimer: I looked at this for 30mins, but I have not tested this. This may or may not ruin everything