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