login.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { NextPage } from "next";
  2. import { useContext, useEffect } from "react";
  3. import Layout from "../components/Layout";
  4. import { UserContext } from "../context/UserContext";
  5. import useForm from "../hooks/useForm";
  6. import api from "../utils/api";
  7. const Login: NextPage = () => {
  8. const context = useContext(UserContext);
  9. const { error, submitHandler, success } = useForm(api.login);
  10. useEffect(() => {
  11. if (success && context) {
  12. console.log(success.message, success.result, context);
  13. }
  14. }, [success, context]);
  15. return (
  16. <Layout>
  17. <form className="form" onSubmit={submitHandler}>
  18. <div className="form-group">
  19. <label htmlFor="email" className="form-label">
  20. Email
  21. </label>
  22. <input
  23. type="email"
  24. name="email"
  25. id="email"
  26. className="form-input"
  27. placeholder="monkey@gmail.com"
  28. required
  29. />
  30. </div>
  31. <div className="form-group">
  32. <label htmlFor="password" className="form-label">
  33. Password
  34. </label>
  35. <input
  36. type="password"
  37. name="password"
  38. id="password"
  39. className="form-input"
  40. placeholder="••••••"
  41. required
  42. />
  43. </div>
  44. <button type="submit" className="form-button">
  45. Log in
  46. </button>
  47. {error && (
  48. <div role="alert" className="form-alert-msg">
  49. {error.message}
  50. </div>
  51. )}
  52. </form>
  53. </Layout>
  54. );
  55. };
  56. export default Login;