Skip to content

Commit 89b85f3

Browse files
committed
feat(user-signup): [next-ts-middle-out] 🟢 implement form error handling
1 parent bafa7ce commit 89b85f3

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

exercises/user_signup/solutions/codely_next-typescript-middle-out/src/components/SignUpForm.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ export function SignUpForm() {
1212
const data = new FormData(ev.target as HTMLFormElement);
1313
const entries = Object.fromEntries(data.entries()) as { [key: string]: string };
1414

15-
await signUpUser({name: entries.name, email: entries.email});
16-
setFormStatus("success");
15+
return signUpUser({name: entries.name, email: entries.email})
16+
.then(() => {
17+
setFormStatus("success");
18+
})
19+
.catch(() => {
20+
setFormStatus("error");
21+
});
1722
}
1823

1924
if (formStatus === "success") {
@@ -23,6 +28,9 @@ export function SignUpForm() {
2328
}
2429

2530
return (<form onSubmit={handleSubmit}>
31+
{
32+
formStatus === "error" && (<div>An error ocurred. Please try again</div>)
33+
}
2634
<Input label="Name" id="name" />
2735
<Input type="email" label="Email" id="email" />
2836
<button type="submit">Submit</button>

exercises/user_signup/solutions/codely_next-typescript-middle-out/tests/unit/SignUpForm.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const mockSignUpUser = signUpUser as jest.Mock<Promise<void>>;
1111
describe("SignUpForm component", () => {
1212
it("displays success message after correct submission", async () => {
1313
render(<SignUpForm />);
14+
mockSignUpUser.mockResolvedValue();
1415

1516
const nameField = screen.getByLabelText(/name/i);
1617
const emailField = screen.getByLabelText(/email/i);

0 commit comments

Comments
 (0)