diff --git a/src/components/AdminProfileModal.tsx b/src/components/AdminProfileModal.tsx new file mode 100644 index 0000000..1720099 --- /dev/null +++ b/src/components/AdminProfileModal.tsx @@ -0,0 +1,288 @@ +import React, { useState, useEffect } from 'react'; +import { X, Save, User, Phone, MapPin, Calendar, CreditCard, Building2, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react'; + +interface AdminProfileModalProps { + isOpen: boolean; + onClose: () => void; + token: string | null; + onUpdated?: (data: any) => void; +} + +export default function AdminProfileModal({ isOpen, onClose, token, onUpdated }: AdminProfileModalProps) { + const [form, setForm] = useState({ + name: '', + whatsapp: '', + birthDate: '', + cpf: '', + cep: '', + address: '', + number: '', + complement: '', + neighborhood: '', + city: '', + state: '', + }); + const [isLoading, setIsLoading] = useState(false); + const [isSaving, setIsSaving] = useState(false); + const [status, setStatus] = useState<{ type: 'success' | 'error'; msg: string } | null>(null); + + useEffect(() => { + if (isOpen && token) { + fetchProfile(); + } + }, [isOpen]); + + const fetchProfile = async () => { + setIsLoading(true); + try { + const res = await fetch('/api/auth/me', { + headers: { 'Authorization': `Bearer ${token}` } + }); + if (res.ok) { + const data = await res.json(); + setForm({ + name: data.name || '', + whatsapp: data.whatsapp || '', + birthDate: data.birthDate ? data.birthDate.substring(0, 10) : '', + cpf: data.cpf || '', + cep: data.cep || '', + address: data.address || '', + number: data.number || '', + complement: data.complement || '', + neighborhood: data.neighborhood || '', + city: data.city || '', + state: data.state || '', + }); + } + } catch (e) { + console.error('Erro ao carregar perfil:', e); + } finally { + setIsLoading(false); + } + }; + + const handleSave = async () => { + setIsSaving(true); + setStatus(null); + try { + const res = await fetch('/api/users/profile', { + method: 'PUT', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(form) + }); + const data = await res.json(); + if (res.ok) { + setStatus({ type: 'success', msg: 'Dados salvos com sucesso! O seu WhatsApp já será usado para os testes de envio.' }); + if (onUpdated) onUpdated(data.user); + } else { + setStatus({ type: 'error', msg: data.error || 'Erro ao salvar dados.' }); + } + } catch (e) { + setStatus({ type: 'error', msg: 'Erro interno ao salvar.' }); + } finally { + setIsSaving(false); + } + }; + + const set = (field: string) => (e: React.ChangeEvent) => + setForm(f => ({ ...f, [field]: e.target.value })); + + if (!isOpen) return null; + + return ( +
+
+ {/* Header */} +
+
+
+ +
+
+

Meu Perfil — Administrador

+

Esses dados são usados para testes de envio de WhatsApp

+
+
+ +
+ + {/* Body */} +
+ {isLoading ? ( +
+ +
+ ) : ( + <> + {/* Dados Pessoais */} +
+

+ + Dados Pessoais +

+
+
+ + +
+
+ + +
+
+ + +
+
+
+ + {/* Contato */} +
+

+ + Contato +

+
+ + +

Quando clicar em "Testar Envio" em qualquer modelo, a mensagem chegará neste número.

+
+
+ + {/* Endereço */} +
+

+ + Endereço +

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + {/* Status feedback */} + {status && ( +
+ {status.type === 'success' ? : } +

{status.msg}

+
+ )} + + )} +
+ + {/* Footer */} +
+ + +
+
+
+ ); +}