Skip to content

Commit c560b2a

Browse files
committed
basic validation working pending click login
1 parent 448d237 commit c560b2a

2 files changed

Lines changed: 147 additions & 7 deletions

File tree

hooks/14_FormValidation/Readme.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,124 @@ export const createDefaultLoginFormErrors = (): LoginFormErrors => ({
128128

129129
- First let's add the dataFormErrors to the state of the component.
130130

131+
_./src/pages/loginPage.tsx_
132+
133+
```diff
134+
import { isValidLogin } from "../api/login";
135+
import { NotificationComponent } from "../common";
136+
+ import {LoginFormErrors, createDefaultLoginFormErrors} from './loginPage.viewmodel';
137+
```
138+
139+
_./src/pages/loginPage.tsx_
140+
141+
```diff
142+
const LoginPageInner = (props: Props) => {
143+
const [loginInfo, setLoginInfo] = React.useState<LoginEntity>(
144+
createEmptyLogin()
145+
);
146+
+ const [loginFormErrors, setLoginFormErrors] = React.useState<LoginFormErrors>(createDefaultLoginFormErrors());
147+
const [showLoginFailedMsg, setShowLoginFailedMsg] = React.useState(false);
148+
```
149+
150+
- Let's fire the validation on viemodel update.
151+
152+
_./src/pages/loginPage.tsx_
153+
154+
```diff
155+
+ import { loginFormValidation } from "./loginPage.validation";
156+
```
157+
158+
_./src/pages/loginPage.tsx_
159+
160+
```diff
161+
const onUpdateLoginField = (name, value) => {
162+
setLoginInfo({
163+
...loginInfo,
164+
[name]: value
165+
});
166+
167+
+ loginFormValidation.validateField(loginInfo, name, value)
168+
+ .then((fieldValidationResult) => {
169+
170+
+ setLoginFormErrors({
171+
+ ...loginFormErrors,
172+
+ [name]: fieldValidationResult,
173+
+ });
174+
+ });
175+
}
176+
```
177+
178+
- We need to pass down dataFormErrors
179+
180+
_./src/pages/loginPage.tsx_
181+
182+
```diff
183+
<LoginForm
184+
onLogin={onLogin}
185+
onUpdateField={onUpdateLoginField}
186+
loginInfo={loginInfo}
187+
+ loginFormErrors={this.state.loginFormErrors}
188+
/>
189+
```
190+
191+
_./src/pages/loginPage.tsx_
192+
193+
```diff
194+
interface PropsForm {
195+
onLogin: () => void;
196+
onUpdateField: (string, any) => void;
197+
loginInfo: LoginEntity;
198+
+ loginFormErrors : LoginFormErrors;
199+
}
200+
```
201+
202+
- Let's replace the _TextFieldForm_ entries with the wrapper we have created (includes
203+
displaying validation errors).
204+
205+
_./src/pages/loginPage.tsx_
206+
207+
```diff
208+
+ import { TextFieldForm } from '../common';
209+
```
210+
211+
_./src/pages/loginPage.tsx_
212+
213+
```diff
214+
const LoginForm = (props: PropsForm) => {
215+
- const { onLogin, onUpdateField, loginInfo } = props;
216+
+ const { onLogin, onUpdateField, loginInfo, loginFormErrors } = props;
217+
```
218+
219+
```diff
220+
- <TextField
221+
+ <TextFieldForm
222+
label="Name"
223+
name="login"
224+
- margin="normal"
225+
value={loginInfo.login}
226+
- onChange={onTexFieldChange("login")}
227+
+ onChange={onUpdateField}
228+
+ error={loginFormErrors.login.errorMessage}
229+
/>
230+
- <TextField
231+
+ <TextFieldForm
232+
label="Password"
233+
name="password"
234+
type="password"
235+
- margin="normal"
236+
value={loginInfo.password}
237+
- onChange={onTexFieldChange("password")}
238+
+ onChange={onUpdateField}
239+
+ error={loginFormErrors.password.errorMessage}
240+
/>
241+
```
242+
243+
- let's give a try
244+
245+
```
246+
npm start
247+
```
248+
131249
# About Basefactor + Lemoncode
132250

133251
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: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { FormHelperText } from "@material-ui/core";
1111
import { LoginEntity, createEmptyLogin } from "../model/login";
1212
import { isValidLogin } from "../api/login";
1313
import { NotificationComponent } from "../common";
14+
import {
15+
LoginFormErrors,
16+
createDefaultLoginFormErrors
17+
} from "./loginPage.viewmodel";
18+
import { loginFormValidation } from "./loginPage.validation";
19+
import { TextFieldForm } from "../common";
1420

1521
// https://material-ui.com/guides/typescript/
1622
const styles = theme =>
@@ -27,6 +33,9 @@ const LoginPageInner = (props: Props) => {
2733
const [loginInfo, setLoginInfo] = React.useState<LoginEntity>(
2834
createEmptyLogin()
2935
);
36+
const [loginFormErrors, setLoginFormErrors] = React.useState<LoginFormErrors>(
37+
createDefaultLoginFormErrors()
38+
);
3039
const [showLoginFailedMsg, setShowLoginFailedMsg] = React.useState(false);
3140
const { classes } = props;
3241

@@ -43,6 +52,15 @@ const LoginPageInner = (props: Props) => {
4352
...loginInfo,
4453
[name]: value
4554
});
55+
56+
loginFormValidation
57+
.validateField(loginInfo, name, value)
58+
.then(fieldValidationResult => {
59+
setLoginFormErrors({
60+
...loginFormErrors,
61+
[name]: fieldValidationResult
62+
});
63+
});
4664
};
4765

4866
return (
@@ -54,6 +72,7 @@ const LoginPageInner = (props: Props) => {
5472
onLogin={onLogin}
5573
onUpdateField={onUpdateLoginField}
5674
loginInfo={loginInfo}
75+
loginFormErrors={loginFormErrors}
5776
/>
5877
</CardContent>
5978
</Card>
@@ -72,10 +91,11 @@ interface PropsForm {
7291
onLogin: () => void;
7392
onUpdateField: (string, any) => void;
7493
loginInfo: LoginEntity;
94+
loginFormErrors: LoginFormErrors;
7595
}
7696

7797
const LoginForm = (props: PropsForm) => {
78-
const { onLogin, onUpdateField, loginInfo } = props;
98+
const { onLogin, onUpdateField, loginInfo, loginFormErrors } = props;
7999

80100
// TODO: Enhacement move this outside the stateless component discuss why is a good idea
81101
const onTexFieldChange = fieldId => e => {
@@ -90,18 +110,20 @@ const LoginForm = (props: PropsForm) => {
90110
justifyContent: "center"
91111
}}
92112
>
93-
<TextField
113+
<TextFieldForm
94114
label="Name"
95-
margin="normal"
115+
name="login"
96116
value={loginInfo.login}
97-
onChange={onTexFieldChange("login")}
117+
onChange={onUpdateField}
118+
error={loginFormErrors.login.errorMessage}
98119
/>
99-
<TextField
120+
<TextFieldForm
100121
label="Password"
101122
type="password"
102-
margin="normal"
123+
name="password"
103124
value={loginInfo.password}
104-
onChange={onTexFieldChange("password")}
125+
onChange={onUpdateField}
126+
error={loginFormErrors.password.errorMessage}
105127
/>
106128
<Button variant="contained" color="primary" onClick={onLogin}>
107129
Login

0 commit comments

Comments
 (0)