"use client"; import React, { useState, type FC, type FormEvent } from "react"; import { saveSession, type ClientSession } from "@/utils/helperFun"; import { Spinner } from "../UI"; const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL!; console.log("API_BASE:", API_BASE); //make sure API_BASE ends without slash // ─── Types ─────────────────────────────────── interface RegForm { name: string; email: string; phone: string; companyName: string; } interface RegErrors { name?: string; email?: string; phone?: string; } interface InputProps extends React.InputHTMLAttributes { label: string; error?: string; icon?: React.ReactNode; } const Input: FC = ({ label, error, icon, className = "", ...rest }) => (
{icon &&
{icon}
}
{error &&

{error}

}
); interface BtnProps extends React.ButtonHTMLAttributes { loading?: boolean; } const PrimaryBtn: FC = ({ loading, children, className = "", disabled, ...rest }) => ( ); const Alert: FC<{ type?: "error" | "info" | "success"; message: string; onClose?: () => void }> = ({ type = "error", message, onClose, }) => { const styles = { error: "bg-red-50 border-red-200 text-red-700", info: "bg-blue-50 border-blue-200 text-blue-700", success: "bg-emerald-50 border-emerald-200 text-emerald-700", }; const icons = { error: "⚠", info: "ℹ", success: "✓" }; return (
{icons[type]} {message} {onClose && ( )}
); }; // ─── RegisterStep ──────────────────────────── interface RegisterStepProps { onSuccess: (s: ClientSession) => void; } const RegisterStep: FC = ({ onSuccess }) => { const [form, setForm] = useState({ name: "", email: "", phone: "", companyName: "" }); const [errors, setErrors] = useState({}); const [loading, setLoading] = useState(false); const [apiError, setApiError] = useState(""); const validate = (): boolean => { const e: RegErrors = {}; if (!form.name.trim()) e.name = "الاسم الكامل مطلوب"; if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) e.email = "أدخل بريدًا إلكترونيًا صحيحًا"; if (!/^(\+966|0)?[5][0-9]{8}$/.test(form.phone.replace(/\s/g, ""))) e.phone = "أدخل رقم هاتف سعودي صحيح (+966 5x xxx xxxx)"; setErrors(e); return Object.keys(e).length === 0; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (!validate()) return; setLoading(true); setApiError(""); try { const res = await fetch(`${API_BASE}/client`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name.trim(), email: form.email.trim(), phone: form.phone.trim(), companyName: form.companyName.trim() || undefined, }), }); const data = await res.json(); if (!res.ok) throw new Error(data.message ?? "فشل التسجيل"); // save session and proceed const session: ClientSession = { id: data.id ?? data._id, name: data.name, email: data.email, phone: data.phone, }; saveSession(session); onSuccess(session); } catch (err: unknown) { setApiError(err instanceof Error ? err.message : "فشل التسجيل، يرجى المحاولة مجددًا."); } finally { setLoading(false); } }; const set = (field: keyof RegForm) => (e: React.ChangeEvent) => { setForm(p => ({ ...p, [field]: e.target.value })); if (errors[field as keyof RegErrors]) setErrors(p => ({ ...p, [field]: undefined })); }; return (
عميل جديد

إنشاء حساب جديد

أدخل بياناتك للبدء مع Slash.so.

{apiError && (
setApiError("")} />
)}
} /> } /> } /> } />
إنشاء الحساب
); }; export default RegisterStep;