import { zodResolver } from '@hookform/resolvers/zod' import { Loader2 } from 'lucide-react' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { FormError, FormField, PasswordInput } from '@/components/form-field' import { LogoutButton } from '@/components/layout/app-layout' import { PageLoading } from '@/components/page' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Section } from '@/features/planning/plan-read' import { useMe } from '@/lib/auth' import { useChangePassword, useUpdateProfile } from '@/lib/household' import { t } from '@/lib/strings' import type { MeResponse } from '@/types/MeResponse' import { nameSchema, passwordSchema, type NameForm, type PasswordForm } from './schemas' import { SettingsSubpage } from './subpage' const s = t.settings.account const message = (e: unknown) => (e instanceof Error ? e.message : String(e)) /** Ayarlar → Hesabım: ad, e-posta (salt okunur), şifre değiştirme, çıkış */ export function AccountPage() { const me = useMe() return ( {!me.data ? ( ) : ( )} ) } function Profile({ me }: { me: MeResponse }) { const update = useUpdateProfile() const { register, handleSubmit, setError, reset, formState: { errors, isDirty }, } = useForm({ resolver: zodResolver(nameSchema), mode: 'onTouched', defaultValues: { name: me.user.name }, }) const onSubmit = handleSubmit(async (v) => { try { const next = await update.mutateAsync(v.name) reset({ name: next.user.name }) toast.success(s.saved) } catch (e) { setError('name', { message: message(e) }) } }) return ( {(f) => } {(f) => } {s.role}: {me.user.role === 'owner' ? t.account.owner : t.account.member} · {me.household.name} {update.isPending && } {s.save} ) } function ChangePassword() { const change = useChangePassword() const { register, handleSubmit, setError, reset, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(passwordSchema), mode: 'onTouched', defaultValues: { current_password: '', new_password: '', repeat: '' }, }) const onSubmit = handleSubmit(async (v) => { try { await change.mutateAsync({ current_password: v.current_password, new_password: v.new_password, }) reset() toast.success(s.changed) } catch (e) { const msg = message(e) // "Mevcut şifre hatalı" → ilgili alana; diğerleri formun altına if (/mevcut/i.test(msg)) setError('current_password', { message: msg }, { shouldFocus: true }) else setError('root', { message: msg }) } }) return ( {s.otherDevices} {/* Parola yöneticileri için gizli kullanıcı adı alanı */} {(f) => ( )} {(f) => ( )} {(f) => } {isSubmitting && } {s.change} ) }
{s.role}: {me.user.role === 'owner' ? t.account.owner : t.account.member} · {me.household.name}
{s.otherDevices}