Andrewnt219 / rent-near-me

https://rent-near-me.vercel.app/
1 stars 1 forks source link

Login modal is closed on wrong credentials #93

Closed Andrewnt219 closed 3 years ago

Andrewnt219 commented 3 years ago

Reproduce

Branch: dev

What happened

The modal is closed

Expect

Login should open and show an error message


I suspect this is because react-hook-form doesn't properly handle errors thrown in handleSubmit. I might have done it wrong, but their docs is not accurate. This does not handle the throw (even though their docs says so)

const onSubmit = () => {
  throw new Error('Something is wrong')
}

handleSubmit(onSubmit).catch((e) => {
  // you will need to catch that error
})

In the other project, I handle this with mutation from react-query. SWR seems to has the same concept for mutation, but I'm not sure.

vitokhangnguyen commented 3 years ago

Yea,. this is a bug.

I think it's just because how I write the submit handler:

  const onSubmit = form.handleSubmit(async (data) => {
    await AuthService.signInWithEmail(
      data.email,
      data.password,
      data.keepLogIn
    ).catch((err) => setSubmitError(err.error_description || err.message));

    loginModal.hide();
  });

I should do loginModal.hide(); in a then clause because a promise with a catch clause won't throw an error.