Advertisement
fabiobiondi

React Pro - Real World App - Ch7. 08. Gestione Errori nel Login

Mar 17th, 2023
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // pages/login/LoginPage.tsx
  2. import { ServerError } from '@/shared/';
  3. import { ChangeEvent, FormEvent, useState } from 'react';
  4. import { selectAuthError, useAuth } from '@/services/auth';
  5. import { useLogin } from './hooks/useLogin';
  6.  
  7. export function LoginPage() {
  8.  
  9.   const error = useAuth(selectAuthError)
  10.   const login = useAuth(state => state.login);
  11.  
  12.   const {
  13.     formData, isValid, changeHandler
  14.   } = useLogin();
  15.  
  16.  
  17.   function doLogin(e: FormEvent<HTMLFormElement>) {
  18.     e.preventDefault();
  19.     console.log(formData)
  20.     login(formData.username, formData.password);
  21.   }
  22.  
  23.   return (
  24.     <div className="page-sm">
  25.       <h1 className="title">LOGIN</h1>
  26.  
  27.       {error && <ServerError />}
  28.  
  29.       <form className="flex flex-col gap-3" onSubmit={doLogin}>
  30.         <input
  31.           type="text"
  32.           placeholder="username"
  33.           value={formData.username}
  34.           onChange={changeHandler}
  35.           name="username"
  36.         />
  37.         <input
  38.           type="password"
  39.           placeholder="password"
  40.           value={formData.password}
  41.           onChange={changeHandler}
  42.           name="password"
  43.         />
  44.         <button disabled={!isValid} className="btn primary" type="submit">SIGN IN</button>
  45.       </form>
  46.  
  47.       <pre>{JSON.stringify(formData, null, 2)}</pre>
  48.     </div>
  49.   )
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement