/* eslint-disable @typescript-eslint/no-explicit-any */ import { Card, CardContent } from "@/components/ui/card"; import { Mail, Lock } from "lucide-react"; import { Link, useLocation } from "react-router"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useForm, type SubmitHandler } from "react-hook-form"; import { useCallback, useState } from "react"; import { loginApi } from "@/api/login"; import { useAccountRepo } from "@/repository/account"; import { useAuth } from "@/store/auth"; 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 repo = useAccountRepo(); const location = useLocation(); const updateActiveAccount = useAuth((state) => state.updateActiveAccount); const onSubmit: SubmitHandler = useCallback( async (data) => { setLoading(true); setError(""); setSuccess(""); try { const response = await loginApi({ email: data.email, password: data.password, }); const account = await repo.save({ accountId: response.id, label: response.full_name, email: response.email, profilePicture: response.profile_picture, access: response.access, refresh: response.refresh, }); setSuccess("You have successfully logged in"); reset(); await updateActiveAccount(account); } catch (err: any) { console.log(err); setError( err.response?.data?.error ?? err.message ?? "Unexpected error happened", ); } finally { setLoading(false); } }, [repo, reset, updateActiveAccount], ); return (
icon

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"}

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