import { useState } from "react"; import { View, Text, KeyboardAvoidingView, Platform, ScrollView, Alert, Pressable, } from "react-native"; import { useRouter } from "expo-router"; import { useMutation, useAction } from "convex/react"; import { api } from "../../convex/_generated/api"; import { Input } from "../../components/ui/input"; import { Button } from "../../components/ui/button"; import { useTheme } from "../../components/theme"; import { useAuth } from "../_layout"; type Role = "handyman" | "customer"; export default function RegisterScreen() { const theme = useTheme(); const router = useRouter(); const { login } = useAuth(); const registerMutation = useMutation(api.auth.register); const hashPassword = useAction(api.auth.hashPassword); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [role, setRole] = useState("customer"); const [loading, setLoading] = useState(false); async function handleRegister() { if (!name || !email || !password) { Alert.alert("Грешка", "Пополнете ги сите задолжителни полиња"); return; } if (password.length < 8) { Alert.alert("Грешка", "Лозинката мора да има најмалку 8 карактери"); return; } setLoading(true); try { const passwordHash = await hashPassword({ password }); const result = await registerMutation({ name, email, passwordHash, role, phone: phone || undefined, }); await login(result.token, result.user!._id); router.replace("/(tabs)/(home)"); } catch (e: any) { Alert.alert("Регистрација неуспешна", e.message || "Обидете се повторно"); } finally { setLoading(false); } } return ( Креирајте профил Изберете ја вашата улога setRole("customer")} style={{ flex: 1, paddingVertical: theme.spacing.md, paddingHorizontal: theme.spacing.sm, borderRadius: theme.radius.md, borderWidth: 2, borderColor: role === "customer" ? theme.colors.primary : theme.colors.border, backgroundColor: role === "customer" ? theme.colors.primaryLight : theme.colors.surface, alignItems: "center", gap: theme.spacing.sm, }} > 🏠 Клиент Барајте мајстори setRole("handyman")} style={{ flex: 1, paddingVertical: theme.spacing.md, paddingHorizontal: theme.spacing.sm, borderRadius: theme.radius.md, borderWidth: 2, borderColor: role === "handyman" ? theme.colors.primary : theme.colors.border, backgroundColor: role === "handyman" ? theme.colors.primaryLight : theme.colors.surface, alignItems: "center", gap: theme.spacing.sm, }} > 🔧 Мајстор Нудете услуги Веќе имате профил? router.back()}> Најавете се ); }