This commit is contained in:
parent
1d0e5f27d7
commit
e2e3e626c4
@ -2,7 +2,6 @@ import { useEffect, useState } from 'react';
|
||||
import {
|
||||
getWhatsappConfig,
|
||||
saveWhatsappConfig,
|
||||
validateWhatsappConfig,
|
||||
} from '../services/whatsappConfigService';
|
||||
|
||||
const fieldStyle = {
|
||||
@ -69,15 +68,29 @@ export function WhatsappConfigModal({ onClose }) {
|
||||
if (!form.access_token || !form.phone_number_id) return;
|
||||
setValidating(true);
|
||||
setValidation(null);
|
||||
const apiVersion = form.api_version || 'v25.0';
|
||||
const url =
|
||||
`https://graph.facebook.com/${apiVersion}/${form.phone_number_id}` +
|
||||
`?access_token=${encodeURIComponent(form.access_token)}&fields=display_phone_number,verified_name,quality_rating`;
|
||||
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);
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
setValidation({
|
||||
valid: false,
|
||||
error: data?.error?.message || 'Credenciais inválidas',
|
||||
code: data?.error?.code,
|
||||
});
|
||||
} else {
|
||||
setValidation({
|
||||
valid: true,
|
||||
phone_number: data.display_phone_number,
|
||||
verified_name: data.verified_name,
|
||||
quality_rating: data.quality_rating,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
setValidation({ valid: false, error: 'Erro de conexão com a Meta API' });
|
||||
setValidation({ valid: false, error: 'Sem acesso à internet para validar' });
|
||||
} finally {
|
||||
setValidating(false);
|
||||
}
|
||||
@ -91,8 +104,9 @@ export function WhatsappConfigModal({ onClose }) {
|
||||
await saveWhatsappConfig(form);
|
||||
setSaveSuccess(true);
|
||||
setTimeout(onClose, 1200);
|
||||
} catch {
|
||||
setError('Erro ao salvar. Verifique os campos e tente novamente.');
|
||||
} catch (err) {
|
||||
const msg = err?.message || '';
|
||||
setError(`Erro ao salvar${msg ? `: ${msg}` : '. Verifique se a migration 025 foi executada no banco.'}`);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user