Skip to content

Commit 960af5a

Browse files
committed
Validating User Input & Outputting Validation Errors
1 parent 0968751 commit 960af5a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

backend/events.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"events":[{"title":"A new event","image":"https://cdn.muenchen-p.de/fl_progressive,q_65/.imaging/stk/responsive/teaser300/dms/va-2016/muenchner-christkindlmarkt/christkindlmarkt-marienplat-logo-hp/document/christkindlmarkt-marienplat-logo-hp.jpg","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"[email protected]","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"}]}
1+
{"events":[{"title":"A new event","image":"https://cdn.muenchen-p.de/fl_progressive,q_65/.imaging/stk/responsive/teaser300/dms/va-2016/muenchner-christkindlmarkt/christkindlmarkt-marienplat-logo-hp/document/christkindlmarkt-marienplat-logo-hp.jpg","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"[email protected]","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"},{"email":"[email protected]","password":"$2a$12$3m6KUOiNwanEyfkY61rBkeADcW2rW3FSIAd9bS4M5grlHMQkMDV/S","id":"e54095a1-a14c-46d6-b7dc-b316c3a6ccfd"},{"email":"[email protected]","password":"$2a$12$J9lpeDvXxDB3onD8FpMcGu2srQyzCpiMrFPdp/BezkQCSjXCZQlfC","id":"538c7b46-8b5c-482d-a4ab-106cd9e63b75"}]}

frontend/src/components/AuthForm.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
import { Form, Link, useSearchParams } from 'react-router-dom';
1+
import {
2+
Form,
3+
Link,
4+
useSearchParams,
5+
useActionData,
6+
useNavigation,
7+
} from 'react-router-dom';
28

39
import classes from './AuthForm.module.css';
410

511
function AuthForm() {
12+
const data = useActionData();
13+
const navigation = useNavigation();
14+
615
const [searchParams] = useSearchParams();
716
const isLogin = searchParams.get('mode') === 'login';
17+
const isSubmitting = navigation.state === 'submitting';
18+
819
return (
920
<>
1021
<Form method="post" className={classes.form}>
1122
<h1>{isLogin ? 'Log in' : 'Create a new user'}</h1>
23+
{data && data.errors && (
24+
<ul>
25+
{Object.values(data.errors).map((err) => (
26+
<li key={err}>{err}</li>
27+
))}
28+
</ul>
29+
)}
30+
{data && data.message && <p>{data.message}</p>}
1231
<p>
1332
<label htmlFor="email">Email</label>
1433
<input id="email" type="email" name="email" required />
@@ -21,7 +40,9 @@ function AuthForm() {
2140
<Link to={`?mode=${isLogin ? 'signup' : 'login'}`}>
2241
{isLogin ? 'Create new user' : 'Login'}
2342
</Link>
24-
<button>Save</button>
43+
<button disabled={isSubmitting}>
44+
{isSubmitting ? 'Submitting...' : 'Save'}
45+
</button>
2546
</div>
2647
</Form>
2748
</>

0 commit comments

Comments
 (0)