Skip to content

Commit 7332659

Browse files
committed
example completed
1 parent c560b2a commit 7332659

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

hooks/14_FormValidation/Readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,36 @@ const LoginForm = (props: PropsForm) => {
246246
npm start
247247
```
248248

249+
- And let's add an alert (Excercise and a notification) when the user clicks and the form all the fields are valid.
250+
251+
_./src/pages/loginPage.tsx_
252+
253+
```diff
254+
const onLogin = () => {
255+
+ loginFormValidation.validateForm(loginInfo)
256+
+ .then((formValidationResult) => {
257+
+ if(formValidationResult.succeeded) {
258+
if (isValidLogin(loginInfo)) {
259+
props.history.push("/pageB");
260+
} else {
261+
setShowLoginFailedMsg(true);
262+
}
263+
+ } else {
264+
+ alert('error, review the fields');
265+
+ const updatedLoginFormErrors = {
266+
+ ...loginFormErrors,
267+
+ ...formValidationResult.fieldErrors,
268+
+ }
269+
+ setLoginFormErrors(updatedLoginFormErrors);
270+
+ }
271+
272+
273+
+ });
274+
};
275+
```
276+
277+
> Excercise, refactor this method following single abstraction level principle and single resposibility principle.
278+
249279
# About Basefactor + Lemoncode
250280

251281
We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.

hooks/14_FormValidation/src/pages/loginPage.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,22 @@ const LoginPageInner = (props: Props) => {
4040
const { classes } = props;
4141

4242
const onLogin = () => {
43-
if (isValidLogin(loginInfo)) {
44-
props.history.push("/pageB");
45-
} else {
46-
setShowLoginFailedMsg(true);
47-
}
43+
loginFormValidation.validateForm(loginInfo).then(formValidationResult => {
44+
if (formValidationResult.succeeded) {
45+
if (isValidLogin(loginInfo)) {
46+
props.history.push("/pageB");
47+
} else {
48+
setShowLoginFailedMsg(true);
49+
}
50+
} else {
51+
alert("error, review the fields");
52+
const updatedLoginFormErrors = {
53+
...loginFormErrors,
54+
...formValidationResult.fieldErrors
55+
};
56+
setLoginFormErrors(updatedLoginFormErrors);
57+
}
58+
});
4859
};
4960

5061
const onUpdateLoginField = (name, value) => {

0 commit comments

Comments
 (0)