From 464e470441287572cfda8d95484f781236b9db35 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Mon, 9 Aug 2021 21:36:03 +0200 Subject: init commit --- helpers/submitForm.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 helpers/submitForm.js (limited to 'helpers/submitForm.js') diff --git a/helpers/submitForm.js b/helpers/submitForm.js new file mode 100644 index 0000000..77b283d --- /dev/null +++ b/helpers/submitForm.js @@ -0,0 +1,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 -- cgit v1.2.3