"use client"; import { FormEvent, useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { getStoredToken } from "@/lib/auth"; import { identityApi, type AccountProfile } from "@/lib/identity"; import { Button, Container } from "@/components/ui"; const inputClass = "mt-1 w-full rounded-xl border border-gray-200 px-4 py-2.5 focus:border-primary focus:outline-none focus:ring-1 focus:ring-primary disabled:bg-gray-50"; const labelClass = "block text-sm font-medium text-secondary"; interface AccountSettingsProps { backHref?: string; backLabel?: string; } export function AccountSettings({ backHref, backLabel }: AccountSettingsProps) { const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); const [loadError, setLoadError] = useState(""); const load = useCallback(async () => { const token = getStoredToken(); if (!token) { setLoadError("توکن احراز هویت یافت نشد. لطفاً دوباره وارد شوید."); setLoading(false); return; } setLoading(true); setLoadError(""); try { const data = await identityApi.profile(token); setProfile(data); } catch (err) { setLoadError(err instanceof Error ? err.message : "دریافت پروفایل ناموفق بود"); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); return (

تنظیمات حساب

مدیریت پروفایل و رمز عبور حساب کاربری شما

{backHref && ( )}
{loadError && (

{loadError}

)} {loading ? (

در حال بارگذاری...

) : profile ? (
) : null}
); } function ProfileForm({ profile, onUpdated, }: { profile: AccountProfile; onUpdated: (p: AccountProfile) => void; }) { const [displayName, setDisplayName] = useState(profile.display_name || ""); const [email, setEmail] = useState(profile.email || ""); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const [success, setSuccess] = useState(""); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(""); setSuccess(""); const token = getStoredToken(); if (!token) { setError("توکن احراز هویت یافت نشد."); return; } setSaving(true); try { const updated = await identityApi.updateProfile(token, { display_name: displayName.trim() || null, email: email.trim() || undefined, }); onUpdated(updated); setSuccess("پروفایل با موفقیت ذخیره شد."); } catch (err) { setError(err instanceof Error ? err.message : "ذخیره پروفایل ناموفق بود"); } finally { setSaving(false); } }; return (

اطلاعات پروفایل

نام نمایشی و ایمیل خود را ویرایش کنید.

setDisplayName(e.target.value)} placeholder="نام و نام خانوادگی" maxLength={255} />
setEmail(e.target.value)} placeholder="you@example.com" />

{profile.mobile_verified ? "شماره موبایل تأیید شده است." : "برای تغییر شماره موبایل با پشتیبانی تماس بگیرید."}

{profile.username && (
)}
{error && (

{error}

)} {success && (

{success}

)}
); } function PasswordForm() { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const [success, setSuccess] = useState(""); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(""); setSuccess(""); if (newPassword.length < 8) { setError("رمز عبور جدید باید حداقل ۸ کاراکتر باشد."); return; } if (newPassword !== confirmPassword) { setError("رمز عبور جدید و تکرار آن یکسان نیستند."); return; } const token = getStoredToken(); if (!token) { setError("توکن احراز هویت یافت نشد."); return; } setSaving(true); try { await identityApi.changePassword(token, { current_password: currentPassword.trim() || undefined, new_password: newPassword, }); setSuccess("رمز عبور با موفقیت به‌روزرسانی شد."); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } catch (err) { setError(err instanceof Error ? err.message : "تغییر رمز عبور ناموفق بود"); } finally { setSaving(false); } }; return (

تغییر رمز عبور

اگر تاکنون رمز عبوری تنظیم نکرده‌اید، فیلد رمز فعلی را خالی بگذارید.

setCurrentPassword(e.target.value)} autoComplete="current-password" />
setNewPassword(e.target.value)} autoComplete="new-password" placeholder="حداقل ۸ کاراکتر" />
setConfirmPassword(e.target.value)} autoComplete="new-password" />
{error && (

{error}

)} {success && (

{success}

)}
); }