import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { DevicePhoneMobileIcon, CheckCircleIcon, XMarkIcon } from '@heroicons/react/24/outline'; import { api } from '../../lib/api'; import type { ApiResponse } from '../../lib/api'; import { sanitizeMobileInput } from '../../lib/utils'; interface NotificationMobileData { notification_mobile: string | null; } interface Props { target: 'doctor' | 'clinic'; } export default function NotificationMobileCard({ target }: Props) { const qc = useQueryClient(); const [step, setStep] = useState<'idle' | 'enter_mobile' | 'enter_otp'>('idle'); const [newMobile, setNewMobile] = useState(''); const [otpCode, setOtpCode] = useState(''); const [error, setError] = useState(null); const { data, isLoading } = useQuery({ queryKey: ['notification-mobile', target], queryFn: () => api.get>(`/api/v1/notification-mobile/${target}`), staleTime: 60_000, }); // eslint-disable-next-line @typescript-eslint/no-explicit-any const current: string | null = ((data?.data as any)?.data ?? data?.data)?.notification_mobile ?? null; const requestOtp = useMutation({ mutationFn: (mobile: string) => api.post>('/api/v1/notification-mobile/request-otp', { target, new_mobile: mobile, }), onSuccess: () => { setError(null); setStep('enter_otp'); }, onError: (e: unknown) => setError(String(e)), }); const verify = useMutation({ mutationFn: (code: string) => api.post>('/api/v1/notification-mobile/verify', { target, otp_code: code, }), onSuccess: () => { qc.invalidateQueries({ queryKey: ['notification-mobile', target] }); setStep('idle'); setNewMobile(''); setOtpCode(''); setError(null); }, // eslint-disable-next-line @typescript-eslint/no-explicit-any onError: (e: any) => setError(e?.message ?? 'خطا در تأیید کد'), }); const remove = useMutation({ mutationFn: () => api.delete>(`/api/v1/notification-mobile/${target}`), onSuccess: () => qc.invalidateQueries({ queryKey: ['notification-mobile', target] }), }); if (isLoading) { return
; } return (

شماره اعلان نوبت

پیامک نوبت جدید به این شماره ارسال می‌شود

{/* نمایش شماره فعلی */} {current && step === 'idle' && (
{current}
)} {!current && step === 'idle' && (

شماره‌ای تنظیم نشده است.

)} {/* مرحله ۱: ورود شماره جدید */} {step === 'idle' && ( )} {step === 'enter_mobile' && (
setNewMobile(sanitizeMobileInput(e.target.value))} placeholder="09XXXXXXXXX" style={{ width: '100%', maxWidth: 220, height: 38, padding: '0 12px', borderRadius: 'var(--r-sm)', border: '1px solid var(--border)', background: 'var(--surface)', color: 'var(--text)', fontSize: 13.5, boxSizing: 'border-box', }} />
{error &&

{error}

}
)} {/* مرحله ۲: ورود کد OTP */} {step === 'enter_otp' && (

کد ۶ رقمی ارسال‌شده به {newMobile} را وارد کنید.

setOtpCode(e.target.value.replace(/\D/g, '').slice(0, 6))} placeholder="_ _ _ _ _ _" dir="ltr" maxLength={6} style={{ width: 140, height: 44, padding: '0 12px', textAlign: 'center', letterSpacing: 8, fontSize: 22, fontWeight: 700, borderRadius: 'var(--r-sm)', border: '1.5px solid var(--border)', background: 'var(--surface)', color: 'var(--text)', boxSizing: 'border-box', }} />
{error &&

{error}

}
)}
); }