@@ -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
133251We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.
0 commit comments