login.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { NextPage } from "next";
  2. import { FormEventHandler, useState } from "react";
  3. import Layout from "../components/Layout";
  4. import fetchApi, { ApiMethods, ApiPaths } from "../utils/api";
  5. type FormError = {
  6. message: string;
  7. };
  8. const Login: NextPage = () => {
  9. const [error, setError] = useState<FormError | null>();
  10. const submitHandler: FormEventHandler<HTMLFormElement> = async (event) => {
  11. event.preventDefault();
  12. const formData = new FormData(event.currentTarget);
  13. const { error, message } = await fetchApi({
  14. method: ApiMethods.POST,
  15. path: ApiPaths.Login,
  16. body: Object.fromEntries(formData.entries()),
  17. });
  18. if (error) {
  19. setError({ message: error });
  20. } else {
  21. console.log(message);
  22. }
  23. };
  24. return (
  25. <Layout>
  26. <form
  27. className="w-full max-w-screen-xs mx-auto flex flex-col xs:grid xs:grid-cols-form xs:items-center gap-4"
  28. onSubmit={submitHandler}
  29. >
  30. <div className="xs:contents">
  31. <label htmlFor="email" className="font-bold flex-1 xs:text-right">
  32. Email
  33. </label>
  34. <input
  35. type="email"
  36. name="email"
  37. id="email"
  38. className="bg-prisma-50 w-full rounded py-2 px-3 text-sm"
  39. placeholder="monkey@gmail.com"
  40. required
  41. />
  42. </div>
  43. <div className="xs:contents">
  44. <label htmlFor="password" className="font-bold flex-1 xs:text-right">
  45. Password
  46. </label>
  47. <input
  48. type="password"
  49. name="password"
  50. id="password"
  51. className="bg-prisma-50 w-full rounded py-2 px-3 text-sm"
  52. placeholder="••••••"
  53. required
  54. />
  55. </div>
  56. <button
  57. type="submit"
  58. className="col-start-2 w-max py-2 px-4 bg-prisma-900 text-white font-bold rounded text-sm hover:bg-prisma-800 active:bg-prisma-700"
  59. >
  60. Log in
  61. </button>
  62. {error && (
  63. <div
  64. role="alert"
  65. className="col-span-2 bg-red-200 p-4 text-center rounded font-bold text-red-900"
  66. >
  67. {error.message}
  68. </div>
  69. )}
  70. </form>
  71. </Layout>
  72. );
  73. };
  74. export default Login;