"use client"; import React, { useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogFooter, DialogTrigger, } from "@/components/ui/dialog"; import { FormField } from "@/components/auth/form-field"; import { loginSchema, LoginFormData, LoginFormProps } from "@/types/auth"; import { useAuth } from "@/hooks/use-auth"; import { listRole } from "@/service/landing/landing"; import { Role } from "@/types/auth"; import Link from "next/link"; import { useTranslations } from "next-intl"; export const LoginForm: React.FC = ({ onSuccess, onError, className, }) => { const { login } = useAuth(); const t = useTranslations("MediaUpdate"); const [showPassword, setShowPassword] = useState(false); const [rememberMe, setRememberMe] = useState(false); const [roles, setRoles] = useState([]); const [selectedCategory, setSelectedCategory] = useState("5"); const [isDialogOpen, setIsDialogOpen] = useState(false); const { register, handleSubmit, formState: { errors, isSubmitting }, getValues, } = useForm({ resolver: zodResolver(loginSchema), mode: "onChange", }); // Load roles on component mount React.useEffect(() => { const loadRoles = async () => { try { const response = await listRole(); setRoles(response?.data?.data || []); } catch (error) { console.error("Failed to load roles:", error); } }; loadRoles(); }, []); const handlePasswordToggle = () => { setShowPassword(!showPassword); }; const handleLogin = async (data: LoginFormData) => { try { await login(data, { skipRedirect: true }); // await login(data); onSuccess?.(data); } catch (error: any) { const message = getLoginErrorMessage(error); onError?.(message); } }; const onSubmit = async (data: LoginFormData) => { try { // onSuccess?.(data); await handleLogin(data); } catch (error: any) { onError?.(error.message || "Login failed"); } }; const getLoginErrorMessage = (error: any): string => { const data = error?.response?.data; // Backend Netidhub if (Array.isArray(data?.messages) && data.messages.length > 0) { return data.messages[0]; } // Fallback lain if (typeof data?.message === "string") return data.message; if (typeof error?.message === "string") return error.message; return "Username atau kata sandi salah"; }; return (
{/* Header */}
netidhub Logo

{t("unite")}

{/*
Register

Category Reg

Select One

{roles.map((role) => (
setSelectedCategory(e.target.value)} className="text-red-500 focus:ring-red-500" />
))}
Next
*/}
{/* Username Field */} {/* Password Field */} {/* Remember Me and Forgot Password */}
setRememberMe(e.target.checked)} disabled={isSubmitting} className="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary" />
{/*
setRememberMe(checked as boolean)} disabled={isSubmitting} />
*/} {t("forgotPass")}
{/* Submit Button */}
); };