"use client"; import React, { useState, useEffect } from "react"; import { useSearchParams } from "next/navigation"; import { toast } from "sonner"; import { RegistrationLayout } from "@/components/auth/registration-layout"; import { IdentityForm } from "@/components/auth/identity-form"; import { RegistrationOTPForm } from "@/components/auth/registration-otp-form"; import { ProfileForm } from "@/components/auth/profile-form"; import { RegistrationStep, UserCategory, JournalistRegistrationData, PersonnelRegistrationData, GeneralRegistrationData, RegistrationFormData } from "@/types/registration"; import { isValidCategory } from "@/lib/registration-utils"; import { useOTP } from "@/hooks/use-registration"; const RegistrationPage = () => { const searchParams = useSearchParams(); const [currentStep, setCurrentStep] = useState("identity"); const [category, setCategory] = useState("general"); const [identityData, setIdentityData] = useState(null); const [userData, setUserData] = useState(null); const { requestOTP } = useOTP(); // Get category from URL params useEffect(() => { const categoryParam = searchParams?.get("category"); console.log("Search params:", searchParams); console.log("Category param from URL:", categoryParam); console.log("Is valid category:", categoryParam && isValidCategory(categoryParam)); if (categoryParam && isValidCategory(categoryParam)) { console.log("Setting category to:", categoryParam); setCategory(categoryParam as UserCategory); } else { // Fallback: try to get category from URL directly const urlParams = new URLSearchParams(window.location.search); const fallbackCategory = urlParams.get("category"); console.log("Fallback category from URL:", fallbackCategory); if (fallbackCategory && isValidCategory(fallbackCategory)) { console.log("Setting category from fallback to:", fallbackCategory); setCategory(fallbackCategory as UserCategory); } else { console.log("Using default category:", "general"); setCategory("general"); } } }, [searchParams]); // Handle identity form success const handleIdentitySuccess = async (data: JournalistRegistrationData | PersonnelRegistrationData | GeneralRegistrationData) => { try { // Store identity data setIdentityData(data); // Get member identity based on category let memberIdentity: string | undefined; if (category === "6") { memberIdentity = (data as JournalistRegistrationData).journalistCertificate; } else if (category === "7") { memberIdentity = (data as PersonnelRegistrationData).policeNumber; } // Debug logging console.log("=== OTP Request Debug ==="); console.log("Category:", category); console.log("Category type:", typeof category); console.log("Email:", data.email); console.log("Member Identity:", memberIdentity); console.log("Current URL:", window.location.href); console.log("URL search params:", window.location.search); // Validate category before proceeding if (!category || !isValidCategory(category)) { console.error("Invalid category detected:", category); toast.error("Invalid registration category. Please refresh the page and try again."); return; } // Request OTP const success = await requestOTP(data.email, category, memberIdentity); if (success) { // Move to OTP step only if OTP request was successful setCurrentStep("otp"); } else { toast.error("Failed to send OTP. Please try again."); } } catch (error: any) { toast.error(error.message || "Failed to send OTP"); } }; const handleIdentityError = (error: string) => { toast.error(error); }; // Handle OTP form success const handleOTPSuccess = (data: any) => { setUserData(data); setCurrentStep("profile"); }; const handleOTPError = (error: string) => { toast.error(error); }; const handleOTPResend = () => { toast.info("OTP resent successfully"); }; // Handle profile form success const handleProfileSuccess = (data: RegistrationFormData) => { toast.success("Registration completed successfully!"); // Redirect to login page after a short delay setTimeout(() => { window.location.href = "/auth"; }, 2000); }; const handleProfileError = (error: string) => { toast.error(error); }; // Render current step const renderCurrentStep = () => { switch (currentStep) { case "identity": return ( ); case "otp": if (!identityData) { toast.error("Identity data not found. Please start over."); setCurrentStep("identity"); return null; } return ( ); case "profile": // Always render the profile form, even if userData is null/undefined return ( ); default: return ( ); } }; // Don't render if category is invalid if (!isValidCategory(category)) { return (

Invalid Registration Category

The registration category specified is not valid.

Back to Login
); } return ( {renderCurrentStep()} ); }; export default RegistrationPage;