import React, { useState, useEffect, useRef } from "react"; import { View, Keyboard, Platform, Image, TouchableOpacity, KeyboardAvoidingView, TouchableWithoutFeedback, } from "react-native"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { Text } from "~/components/ui/text"; import { router } from "expo-router"; import { ROUTES } from "~/lib/routes"; import { Icons } from "~/assets/icons"; import { useGlobalLoading } from "~/lib/hooks/useGlobalLoading"; import { AuthService } from "~/lib/services/authServices"; import ModalToast from "~/components/ui/toast"; import ScreenWrapper from "~/components/ui/ScreenWrapper"; import { useAuthStore } from "~/lib/stores/authStore"; export default function AgentSignup() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [loading, setLoading] = useState(false); const { withLoading } = useGlobalLoading(); const { setUser } = useAuthStore(); const [toastVisible, setToastVisible] = useState(false); const [toastTitle, setToastTitle] = useState(""); const [toastDescription, setToastDescription] = useState( undefined ); const [toastVariant, setToastVariant] = useState< "success" | "error" | "warning" | "info" >("info"); const toastTimeoutRef = useRef | null>(null); const showToast = ( title: string, description?: string, variant: "success" | "error" | "warning" | "info" = "info" ) => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } setToastTitle(title); setToastDescription(description); setToastVariant(variant); setToastVisible(true); toastTimeoutRef.current = setTimeout(() => { setToastVisible(false); toastTimeoutRef.current = null; }, 2500); }; const validateEmail = (email: string): boolean => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const handleSignUp = async () => { Keyboard.dismiss(); // Validation if (!email.trim()) { showToast("Error", "Please enter your email", "warning"); return; } if (!validateEmail(email.trim())) { showToast("Error", "Please enter a valid email address", "warning"); return; } if (!password.trim()) { showToast("Error", "Please enter a password", "warning"); return; } if (password.length < 6) { showToast("Error", "Password must be at least 6 characters", "warning"); return; } if (password !== confirmPassword) { showToast("Error", "Passwords do not match", "warning"); return; } setLoading(true); try { const result = await withLoading(() => AuthService.signUpAgent(email.trim(), password) ); if (result.error) { showToast("Sign Up Error", result.error, "error"); return; } if (result.user) { // Set user in auth store setUser(result.user); // Navigate to home router.replace(ROUTES.HOME); } } catch (error) { console.error("Agent signup error:", error); showToast( "Error", error instanceof Error ? error.message : "Failed to sign up", "error" ); } finally { setLoading(false); } }; useEffect(() => { return () => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } }; }, []); return ( amba pay Agent Sign Up Already have an account?{" "} router.push("/auth/agent-signin")} > Sign In ); }