aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/submitForm.js
blob: 77b283dfa43b2bd14f66edbe4cdbd5f741cb565f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import fetchJson from 'lib/fetchJson'

const submitForm = async (e, url, mutateUser, setErrorMsg) => {
  e.preventDefault()

  const body = {
    email: e.currentTarget.email.value,
    password: e.currentTarget.password.value,
  }

  try {
    mutateUser(
      await fetchJson(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
      })
    )
  } catch (err) {
    url.includes('login')
      ? setErrorMsg('Could not log in')
      : setErrorMsg('Could not register user')
  }
}

export default submitForm