From af8b347173b19da6e01471c1acbca10c8f0cb885 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Wed, 21 May 2025 19:27:08 +0200 Subject: [PATCH] feat: finished login page --- web/src/pages/Login/index.tsx | 200 ++++++++++++++++++++++++++-------- 1 file changed, 152 insertions(+), 48 deletions(-) diff --git a/web/src/pages/Login/index.tsx b/web/src/pages/Login/index.tsx index 8ad6bfb..56d94c0 100644 --- a/web/src/pages/Login/index.tsx +++ b/web/src/pages/Login/index.tsx @@ -1,68 +1,172 @@ import { Card, CardContent } from "@/components/ui/card"; -import { Mail, Lock, LogIn } from "lucide-react"; +import { Mail, Lock } from "lucide-react"; import { Link } from "react-router-dom"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; + import overlay from "@/assets/overlay.jpg"; +import { useForm, type SubmitHandler } from "react-hook-form"; +import { useCallback, useState } from "react"; + +interface LoginForm { + email: string; + password: string; +} export default function LoginPage() { + const { + register, + reset, + handleSubmit, + formState: { errors }, + } = useForm(); + + const [isLoading, setLoading] = useState(false); + const [error, setError] = useState(""); + const [success, setSuccess] = useState(""); + + const onSubmit: SubmitHandler = useCallback( + async (data) => { + console.log({ data }); + + setLoading(true); + setError(""); + setSuccess(""); + + try { + const response = await fetch("/api/v1/login", { + method: "POST", + body: JSON.stringify({ + email: data.email, + password: data.password, + }), + headers: { + "Content-Type": "application/json", + }, + }); + if (response.status != 200) { + const json = await response.json(); + const text = json.error || "Unexpected error happened"; + setError( + `Failed to create an account. ${ + text[0].toUpperCase() + text.slice(1) + }` + ); + } else { + setSuccess("You have successfully logged in"); + reset(); + } + } catch (err) { + console.log(err); + setError("Failed to create account. Unexpected error happened"); + } finally { + setLoading(false); + } + }, + [reset] + ); + return (
-
+
+ +
+ icon -
- -
- -

Login

+
+

+ Sign In +

+

+ Enter your credentials to access home services and tools. +

+
+ {/* */} -
- -
- - +
+
+
+ + +
+ {!!errors.email && ( +

+ {errors.email.message ?? "Email is required"} +

+ )}
-
-
- -
- - +
+
+ + +
+ {!!errors.password && ( +

+ {errors.password.message ?? "Password is required"} +

+ )}
-
- -
- Don’t have an account?{" "} - - Register - -
+ {success.length > 0 && ( +
+ {success} +
+ )} + + {error.length > 0 && ( +
+ {error} +
+ )} + + +
+ Don't have an account?{" "} + + Register + +
+