diff --git a/src/modules/management/components/WhatsappConfigModal.jsx b/src/modules/management/components/WhatsappConfigModal.jsx new file mode 100644 index 0000000..0b8fa85 --- /dev/null +++ b/src/modules/management/components/WhatsappConfigModal.jsx @@ -0,0 +1,370 @@ +import { useEffect, useState } from 'react'; +import { + getWhatsappConfig, + saveWhatsappConfig, + validateWhatsappConfig, +} from '../services/whatsappConfigService'; + +const fieldStyle = { + width: '100%', + border: '1px solid var(--color-border)', + borderRadius: 12, + padding: '0.7rem 0.85rem', + background: '#fff', + color: 'var(--color-text)', + fontWeight: 600, + fontSize: '0.92rem', + boxSizing: 'border-box', +}; + +const labelStyle = { + display: 'block', + fontSize: '0.8rem', + fontWeight: 800, + color: 'var(--color-text-soft)', + marginBottom: '0.35rem', + letterSpacing: '0.03em', + textTransform: 'uppercase', +}; + +export function WhatsappConfigModal({ onClose }) { + const [loading, setLoading] = useState(true); + const [currentConfig, setCurrentConfig] = useState(null); + const [saving, setSaving] = useState(false); + const [validating, setValidating] = useState(false); + const [validation, setValidation] = useState(null); + const [saveSuccess, setSaveSuccess] = useState(false); + const [error, setError] = useState(''); + + const [form, setForm] = useState({ + access_token: '', + phone_number_id: '', + api_version: 'v25.0', + webhook_token: '', + }); + + useEffect(() => { + getWhatsappConfig() + .then((data) => { + setCurrentConfig(data); + setForm((f) => ({ + ...f, + phone_number_id: data.phone_number_id || '', + api_version: data.api_version || 'v25.0', + webhook_token: data.webhook_token || '', + })); + }) + .catch(() => setError('Não foi possível carregar a configuração atual.')) + .finally(() => setLoading(false)); + }, []); + + function handleChange(field, value) { + setValidation(null); + setSaveSuccess(false); + setError(''); + setForm((f) => ({ ...f, [field]: value })); + } + + async function handleValidate() { + if (!form.access_token || !form.phone_number_id) return; + setValidating(true); + setValidation(null); + try { + const result = await validateWhatsappConfig({ + access_token: form.access_token, + phone_number_id: form.phone_number_id, + api_version: form.api_version || 'v25.0', + }); + setValidation(result); + } catch { + setValidation({ valid: false, error: 'Erro de conexão com a Meta API' }); + } finally { + setValidating(false); + } + } + + async function handleSave() { + setSaving(true); + setSaveSuccess(false); + setError(''); + try { + await saveWhatsappConfig(form); + setSaveSuccess(true); + setTimeout(onClose, 1200); + } catch { + setError('Erro ao salvar. Verifique os campos e tente novamente.'); + } finally { + setSaving(false); + } + } + + const canValidate = !!(form.access_token && form.phone_number_id); + const canSave = !!(form.access_token || form.phone_number_id) && !saving; + + return ( +
+ Carregando configuração atual… +
+ )} + + {/* Formulário */} ++ {error} +
+ )} + + {/* Feedback de sucesso */} + {saveSuccess && ( ++ ✅ Configuração salva com sucesso! +
+ )} + + {/* Ações */} +