forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create Button, Field and Form components b00tc4mp#451; create Registe…
…r page; write register user logic; check user registered successfully in our db b00tc4mp#422
- Loading branch information
Showing
5 changed files
with
87 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function Field(props) { | ||
return ( | ||
<> | ||
<label htmlFor={props.inputId}>{props.children}</label> | ||
<input name={props.name} id={props.inputId} /> | ||
</> | ||
) | ||
} | ||
|
||
export default Field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function Form(props) { | ||
return <form onSubmit={props.onSubmit}>{props.children}</form> | ||
} | ||
|
||
export default Form |
44 changes: 25 additions & 19 deletions
44
staff/judy-grayland/project/app/src/logic/registerUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
import { validate, errors } from '../../../shared' | ||
import { SystemError } from '../../../shared/errors' | ||
|
||
async function registerUser(name, email, password) { | ||
function registerUser(name, email, password) { | ||
validate.text(name, 'name') | ||
validate.email(email, 'email') | ||
validate.password(password, 'password') | ||
|
||
try { | ||
const req = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ name, email, password }), | ||
} | ||
|
||
const res = await fetch(`${import.meta.env.VITE_API_URL}/users/`, req) | ||
|
||
if (!res.ok) { | ||
const body = await res.json() | ||
throw new errors[body.error](body.message) | ||
} | ||
return null | ||
} catch (error) { | ||
throw error | ||
const req = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ name, email, password }), | ||
} | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users`, req) | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((res) => { | ||
if (!res.ok) { | ||
return res | ||
.json() | ||
.catch((error) => { | ||
throw new SystemError(error.message) | ||
}) | ||
.then((body) => { | ||
throw new errors[body.error](body.message) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export default registerUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters