CognxSafeTrack commited on
Commit
5220c27
·
1 Parent(s): d3a9684

fix: resolve frontend API export and remove node-fetch dependency

Browse files
apps/admin/src/components/WhatsAppConnectButton.tsx CHANGED
@@ -1,10 +1,12 @@
1
  import React, { useEffect, useState } from 'react';
2
  import { initMetaSDK, launchEmbeddedSignup } from '../lib/meta-signup';
3
- import { api } from '../lib/api';
4
  import { useParams } from 'react-router-dom';
 
5
 
6
  export const WhatsAppConnectButton: React.FC = () => {
7
  const { orgId } = useParams<{ orgId: string }>();
 
8
  const [loading, setLoading] = useState(false);
9
  const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle');
10
 
@@ -13,7 +15,7 @@ export const WhatsAppConnectButton: React.FC = () => {
13
  }, []);
14
 
15
  const handleConnect = async () => {
16
- if (!orgId) return;
17
  setLoading(true);
18
  setStatus('idle');
19
 
@@ -22,13 +24,19 @@ export const WhatsAppConnectButton: React.FC = () => {
22
  const result = await launchEmbeddedSignup();
23
 
24
  // 2. Send IDs and Code/Token to our Backend
25
- await api.post(`/v1/organizations/${orgId}/whatsapp-setup`, {
26
- wabaId: result.waba_id,
27
- accessToken: result.accessToken || result.code, // Can be token or code depending on Meta config
28
- phoneNumberId: result.phone_number_id,
29
- phoneNumber: '' // Meta doesn't always send the phone number string in the event
 
 
 
 
30
  });
31
 
 
 
32
  setStatus('success');
33
  console.log("[SETUP] WhatsApp connecté avec succès !");
34
  } catch (err) {
 
1
  import React, { useEffect, useState } from 'react';
2
  import { initMetaSDK, launchEmbeddedSignup } from '../lib/meta-signup';
3
+ import { API_URL, ah } from '../lib/api';
4
  import { useParams } from 'react-router-dom';
5
+ import { useAuth } from '../lib/auth';
6
 
7
  export const WhatsAppConnectButton: React.FC = () => {
8
  const { orgId } = useParams<{ orgId: string }>();
9
+ const { token } = useAuth();
10
  const [loading, setLoading] = useState(false);
11
  const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle');
12
 
 
15
  }, []);
16
 
17
  const handleConnect = async () => {
18
+ if (!orgId || !token) return;
19
  setLoading(true);
20
  setStatus('idle');
21
 
 
24
  const result = await launchEmbeddedSignup();
25
 
26
  // 2. Send IDs and Code/Token to our Backend
27
+ const response = await fetch(`${API_URL}/v1/organizations/${orgId}/whatsapp-setup`, {
28
+ method: 'POST',
29
+ headers: ah(token, orgId),
30
+ body: JSON.stringify({
31
+ wabaId: result.waba_id,
32
+ accessToken: result.accessToken || result.code,
33
+ phoneNumberId: result.phone_number_id,
34
+ phoneNumber: ''
35
+ })
36
  });
37
 
38
+ if (!response.ok) throw new Error('Setup failed');
39
+
40
  setStatus('success');
41
  console.log("[SETUP] WhatsApp connecté avec succès !");
42
  } catch (err) {
apps/api/src/scripts/simulate-webhook.ts CHANGED
@@ -1,4 +1,4 @@
1
- import fetch from 'node-fetch';
2
 
3
  /**
4
  * WhatsApp Webhook Simulator
 
1
+
2
 
3
  /**
4
  * WhatsApp Webhook Simulator