import { useState, useEffect, ChangeEvent, CSSProperties, ReactNode } from "react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "../../lib/authContext"; import { getApiBaseUrl } from "../../lib/api/config"; // ── Types ───────────────────────────────────────────────────────────────────── type StatusType = "success" | "error" | "loading"; interface Status { text: string; type: StatusType; } interface SheikhProfileForm { birthDate: string; birthTime: string; country: string; gender: string; phone: string; bio: string; specialization: string; yearsOfExperience: string; hourlyRate: string; certificateUrl: string; profilePhotoUrl: string; } interface SheikhProfileBody { birthDateTime: string; country: string; gender: string; phone: string | null; bio: string | null; specialization: string | null; yearsOfExperience: number; hourlyRate: number; certificateUrl: string | null; profilePhotoUrl: string | null; } interface FormGroupProps { label: string; required?: boolean; children: ReactNode; } // ── Constants ───────────────────────────────────────────────────────────────── const BACKEND_URL = `${getApiBaseUrl()}/api/auth/complete-google-profile/sheikh`; const COUNTRIES: string[] = [ "Egypt", "Saudi Arabia", "UAE", "Jordan", "Kuwait", "Qatar", "Bahrain", "Oman", "Libya", "Tunisia", "Morocco", "Algeria", "Sudan", "Other", ]; const STATUS_COLORS: Record = { success: { bg: "#d4edda", color: "#155724" }, error: { bg: "#f8d7da", color: "#721c24" }, loading: { bg: "#fff3cd", color: "#856404" }, }; // ── Sub-component ───────────────────────────────────────────────────────────── function FormGroup({ label, required = false, children }: FormGroupProps) { return (
{children}
); } // ── Component ───────────────────────────────────────────────────────────────── // sheikh/interview export default function CompleteSheikhProfile() { const navigate = useNavigate(); const { updateProfile } = useAuth(); const [hasToken, setHasToken] = useState(true); const [loading, setLoading] = useState(false); const [status, setStatus] = useState(null); const [response, setResponse] = useState(null); const [form, setForm] = useState({ birthDate: "", birthTime: "00:00", country: "", gender: "", phone: "", bio: "", specialization: "", yearsOfExperience: "", hourlyRate: "", certificateUrl: "", profilePhotoUrl: "", }); useEffect(() => { setHasToken(!!localStorage.getItem("authToken")); }, []); function handleChange(e: ChangeEvent): void { const { id, value } = e.target; setForm((prev) => ({ ...prev, [id]: value })); } async function submitProfile(): Promise { const jwt = localStorage.getItem("authToken"); if (!jwt) { setStatus({ text: "❌ No authentication token found. Please sign in first.", type: "error" }); return; } if (!form.birthDate){ setStatus({ text: "❌ Birth date is required", type: "error" }); return; } if (!form.country) { setStatus({ text: "❌ Country is required", type: "error" }); return; } if (!form.gender) { setStatus({ text: "❌ Gender is required", type: "error" }); return; } const birthDateTime = `${form.birthDate}T${form.birthTime || "00:00"}:00`; const body: SheikhProfileBody = { birthDateTime, country: form.country, gender: form.gender, phone: form.phone || null, bio: form.bio || null, specialization: form.specialization || null, yearsOfExperience: parseInt(form.yearsOfExperience) || 0, hourlyRate: parseFloat(form.hourlyRate) || 0, certificateUrl: form.certificateUrl || null, profilePhotoUrl: form.profilePhotoUrl || null, }; setLoading(true); setStatus({ text: "⏳ Saving profile...", type: "loading" }); try { const res = await fetch(BACKEND_URL, { method: "POST", headers: { "Content-Type": "application/json", Authorization: "Bearer " + jwt }, body: JSON.stringify(body), }); const text = await res.text(); setResponse(text); if (res.ok) { setStatus({ text: "✅ Sheikh profile submitted! Waiting for admin approval.", type: "success" }); updateProfile({ profileCompleted: true }); setTimeout(() => navigate("/sheikh/interview", { replace: true }), 1000); } else { setStatus({ text: "❌ Error: " + text, type: "error" }); setLoading(false); } } catch (err: unknown) { const message = err instanceof Error ? err.message : "Unknown error"; setStatus({ text: "❌ Failed: " + message, type: "error" }); setLoading(false); } } return (
{/* Header */}
📚

Complete Sheikh Profile

Fill in your details to apply as a sheikh

SHEIKH
ℹ️ After submitting, your profile will be reviewed by the admin. You will receive an interview invitation once approved.
{/* No token warning */} {!hasToken && (
⚠️ No JWT found. Please{" "} sign in with Google first.
)} {/* Personal Info */}
👤 Personal Information
{/* Sheikh Profile */}
🕌 Sheikh Profile