@controleonline/ui-common 1.2.107 → 1.2.109
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.release/rc-manifest.json +11 -8
- package/package.json +1 -1
- package/src/react/pages/Connections.js +17 -95
- package/src/react/pages/WhatsAppConnectionPage.js +392 -459
- package/src/react/pages/WhatsAppConnectionPage.styles.js +312 -279
- package/src/react/pages/WhatsAppProviderForm.js +115 -0
- package/src/react/pages/whatsAppConnectionHelpers.js +105 -0
- package/src/store/connections/customActions.js +73 -26
- package/src/store/connections/index.js +109 -105
|
@@ -1,459 +1,392 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
showError(
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
<View style={styles.countBadge}>
|
|
394
|
-
<Text style={styles.countBadgeText}>{connections.length} item(ns)</Text>
|
|
395
|
-
</View>
|
|
396
|
-
</View>
|
|
397
|
-
|
|
398
|
-
<View style={styles.listWrap}>
|
|
399
|
-
{connections.length > 0 ? (
|
|
400
|
-
connections.map(item => (
|
|
401
|
-
<TouchableOpacity
|
|
402
|
-
key={item.id}
|
|
403
|
-
activeOpacity={0.92}
|
|
404
|
-
onPress={() => handleUseExistingConnection(item)}
|
|
405
|
-
style={[styles.connectionCard, shadowStyle]}>
|
|
406
|
-
<View style={styles.connectionTop}>
|
|
407
|
-
<View style={[styles.connectionIconWrap, { backgroundColor: withOpacity('#10b981', 0.12) }]}>
|
|
408
|
-
<Icon name="phone-call" size={18} color="#10b981" />
|
|
409
|
-
</View>
|
|
410
|
-
<View style={[styles.connectionStatusBadge, { backgroundColor: withOpacity('#2563EB', 0.12) }]}>
|
|
411
|
-
<Text style={styles.connectionStatusText}>{item.status}</Text>
|
|
412
|
-
</View>
|
|
413
|
-
</View>
|
|
414
|
-
|
|
415
|
-
<Text style={styles.connectionName}>{item.name}</Text>
|
|
416
|
-
|
|
417
|
-
<View style={styles.connectionMetaRow}>
|
|
418
|
-
<View style={styles.connectionMetaItem}>
|
|
419
|
-
<Text style={styles.connectionMetaLabel}>Telefone</Text>
|
|
420
|
-
<Text style={styles.connectionMetaValue}>{item.phoneLabel}</Text>
|
|
421
|
-
</View>
|
|
422
|
-
<View style={styles.connectionMetaItem}>
|
|
423
|
-
<Text style={styles.connectionMetaLabel}>Tipo</Text>
|
|
424
|
-
<Text style={styles.connectionMetaValue}>{item.type}</Text>
|
|
425
|
-
</View>
|
|
426
|
-
</View>
|
|
427
|
-
|
|
428
|
-
<View style={styles.connectionMetaRow}>
|
|
429
|
-
<View style={styles.connectionMetaItem}>
|
|
430
|
-
<Text style={styles.connectionMetaLabel}>Canal</Text>
|
|
431
|
-
<Text style={styles.connectionMetaValue}>{item.channel}</Text>
|
|
432
|
-
</View>
|
|
433
|
-
<View style={styles.connectionMetaItem}>
|
|
434
|
-
<Text style={styles.connectionMetaLabel}>Ultima sessao</Text>
|
|
435
|
-
<Text style={styles.connectionMetaValue}>
|
|
436
|
-
{extractDigits(item.phone?.phone || '') &&
|
|
437
|
-
lastRequestedPhone &&
|
|
438
|
-
lastRequestedPhone.endsWith(extractDigits(item.phone?.phone || ''))
|
|
439
|
-
? 'Selecionada'
|
|
440
|
-
: 'Disponivel'}
|
|
441
|
-
</Text>
|
|
442
|
-
</View>
|
|
443
|
-
</View>
|
|
444
|
-
</TouchableOpacity>
|
|
445
|
-
))
|
|
446
|
-
) : (
|
|
447
|
-
<View style={[styles.emptyStateBox, { marginTop: 4 }]}>
|
|
448
|
-
<Icon name="inbox" size={24} color="#94A3B8" />
|
|
449
|
-
<Text style={styles.emptyStateTitle}>Nenhuma conexao cadastrada</Text>
|
|
450
|
-
<Text style={styles.emptyStateText}>
|
|
451
|
-
O primeiro QR Code gerado vai iniciar a configuracao deste canal para a empresa.
|
|
452
|
-
</Text>
|
|
453
|
-
</View>
|
|
454
|
-
)}
|
|
455
|
-
</View>
|
|
456
|
-
</ScrollView>
|
|
457
|
-
</SafeAreaView>
|
|
458
|
-
);
|
|
459
|
-
}
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ScrollView,
|
|
4
|
+
Text,
|
|
5
|
+
View,
|
|
6
|
+
} from 'react-native';
|
|
7
|
+
import { SafeAreaView } from 'react-native-safe-area-context';
|
|
8
|
+
import { useFocusEffect } from '@react-navigation/native';
|
|
9
|
+
import Icon from 'react-native-vector-icons/Feather';
|
|
10
|
+
import QRCode from 'react-native-qrcode-svg';
|
|
11
|
+
|
|
12
|
+
import { useStore } from '@store';
|
|
13
|
+
import { colors } from '@controleonline/../../src/styles/colors';
|
|
14
|
+
import { createStyles } from './WhatsAppConnectionPage.styles';
|
|
15
|
+
import {
|
|
16
|
+
resolveThemePalette,
|
|
17
|
+
withOpacity,
|
|
18
|
+
} from '@controleonline/../../src/styles/branding';
|
|
19
|
+
import DefaultTable from '@controleonline/ui-default/src/react/components/table/DefaultTable';
|
|
20
|
+
import WhatsAppProviderForm from './WhatsAppProviderForm';
|
|
21
|
+
import {
|
|
22
|
+
CHANNEL_WHATSAPP_OFFICIAL,
|
|
23
|
+
PROVIDER_OFFICIAL,
|
|
24
|
+
PROVIDER_UNOFFICIAL,
|
|
25
|
+
extractDigits,
|
|
26
|
+
formatApiError,
|
|
27
|
+
formatPhoneLabel,
|
|
28
|
+
normalizeConnection,
|
|
29
|
+
toArray,
|
|
30
|
+
} from './whatsAppConnectionHelpers';
|
|
31
|
+
|
|
32
|
+
export default function WhatsAppConnectionPage() {
|
|
33
|
+
const peopleStore = useStore('people');
|
|
34
|
+
const themeStore = useStore('theme');
|
|
35
|
+
const connectionsStore = useStore('connections');
|
|
36
|
+
const connectionsActions = connectionsStore.actions || {};
|
|
37
|
+
|
|
38
|
+
const { currentCompany } = peopleStore.getters || {};
|
|
39
|
+
const { colors: themeColors } = themeStore.getters || {};
|
|
40
|
+
const brandColors = useMemo(
|
|
41
|
+
() =>
|
|
42
|
+
resolveThemePalette(
|
|
43
|
+
{ ...themeColors, ...(currentCompany?.theme?.colors || {}) },
|
|
44
|
+
colors,
|
|
45
|
+
),
|
|
46
|
+
[themeColors, currentCompany],
|
|
47
|
+
);
|
|
48
|
+
const styles = useMemo(() => createStyles(brandColors), [brandColors]);
|
|
49
|
+
|
|
50
|
+
const providerId = currentCompany?.id;
|
|
51
|
+
const peopleIri = useMemo(
|
|
52
|
+
() => (providerId ? `/people/${String(providerId).replace(/\D/g, '')}` : ''),
|
|
53
|
+
[providerId],
|
|
54
|
+
);
|
|
55
|
+
const requestParams = useMemo(
|
|
56
|
+
() => (peopleIri ? { people: peopleIri } : {}),
|
|
57
|
+
[peopleIri],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const [providerMode, setProviderMode] = useState(PROVIDER_UNOFFICIAL);
|
|
61
|
+
const [ddi, setDdi] = useState('55');
|
|
62
|
+
const [ddd, setDdd] = useState('');
|
|
63
|
+
const [phone, setPhone] = useState('');
|
|
64
|
+
const [accessToken, setAccessToken] = useState('');
|
|
65
|
+
const [phoneNumberId, setPhoneNumberId] = useState('');
|
|
66
|
+
const [apiVersion, setApiVersion] = useState('v22.0');
|
|
67
|
+
const [selectedId, setSelectedId] = useState(null);
|
|
68
|
+
const [selectedStatusIri, setSelectedStatusIri] = useState(null);
|
|
69
|
+
const [qrValue, setQrValue] = useState('');
|
|
70
|
+
const [connectionStatus, setConnectionStatus] = useState('');
|
|
71
|
+
const [submitting, setSubmitting] = useState(false);
|
|
72
|
+
const [feedback, setFeedback] = useState(null);
|
|
73
|
+
const pollingRef = useRef(null);
|
|
74
|
+
|
|
75
|
+
const completePhone = useMemo(
|
|
76
|
+
() => `${extractDigits(ddi)}${extractDigits(ddd)}${extractDigits(phone)}`,
|
|
77
|
+
[ddi, ddd, phone],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const showError = useCallback(message => {
|
|
81
|
+
setFeedback({ type: 'error', message: String(message || 'Erro') });
|
|
82
|
+
}, []);
|
|
83
|
+
const showSuccess = useCallback(message => {
|
|
84
|
+
setFeedback({ type: 'success', message: String(message || 'OK') });
|
|
85
|
+
}, []);
|
|
86
|
+
const showInfo = useCallback(message => {
|
|
87
|
+
setFeedback({ type: 'info', message: String(message || '') });
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
const clearPolling = useCallback(() => {
|
|
91
|
+
if (pollingRef.current) {
|
|
92
|
+
clearInterval(pollingRef.current);
|
|
93
|
+
pollingRef.current = null;
|
|
94
|
+
}
|
|
95
|
+
}, []);
|
|
96
|
+
|
|
97
|
+
const resolveStatusIri = useCallback(
|
|
98
|
+
item => {
|
|
99
|
+
const status = item?.status;
|
|
100
|
+
if (!status) return selectedStatusIri;
|
|
101
|
+
if (typeof status === 'string' && status.startsWith('/')) return status;
|
|
102
|
+
if (typeof status === 'object') {
|
|
103
|
+
return status['@id'] || status.id || selectedStatusIri;
|
|
104
|
+
}
|
|
105
|
+
return selectedStatusIri;
|
|
106
|
+
},
|
|
107
|
+
[selectedStatusIri],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const requestQrCode = useCallback(
|
|
111
|
+
async phoneNumber => {
|
|
112
|
+
const targetPhone = extractDigits(phoneNumber);
|
|
113
|
+
if (!targetPhone) {
|
|
114
|
+
showError('Informe DDI, DDD e telefone para gerar o QR Code.');
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
setSubmitting(true);
|
|
119
|
+
const response = await connectionsActions.createWhatsappConnection({
|
|
120
|
+
phone: targetPhone,
|
|
121
|
+
});
|
|
122
|
+
const nextStatus = String(response?.status || '').trim();
|
|
123
|
+
const nextQr = String(response?.qr || '').trim();
|
|
124
|
+
setConnectionStatus(nextStatus || 'Aguardando autenticacao');
|
|
125
|
+
if (nextQr) setQrValue(nextQr);
|
|
126
|
+
if (nextStatus === 'CONNECTED') {
|
|
127
|
+
clearPolling();
|
|
128
|
+
setQrValue('');
|
|
129
|
+
showSuccess('WhatsApp conectado com sucesso.');
|
|
130
|
+
connectionsStore.actions?.getItems?.({ people: peopleIri });
|
|
131
|
+
}
|
|
132
|
+
return response;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
clearPolling();
|
|
135
|
+
showError(formatApiError(error));
|
|
136
|
+
return null;
|
|
137
|
+
} finally {
|
|
138
|
+
setSubmitting(false);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
[
|
|
142
|
+
clearPolling,
|
|
143
|
+
connectionsActions,
|
|
144
|
+
connectionsStore.actions,
|
|
145
|
+
peopleIri,
|
|
146
|
+
showError,
|
|
147
|
+
showSuccess,
|
|
148
|
+
],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const startPolling = useCallback(
|
|
152
|
+
async selectedPhone => {
|
|
153
|
+
const targetPhone = extractDigits(selectedPhone || completePhone);
|
|
154
|
+
if (!targetPhone) {
|
|
155
|
+
showError('Informe DDI, DDD e telefone para gerar o QR Code.');
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
clearPolling();
|
|
159
|
+
const response = await requestQrCode(targetPhone);
|
|
160
|
+
if (response?.status === 'CONNECTED') return;
|
|
161
|
+
showInfo('Aguardando autenticacao do WhatsApp pelo QR Code.');
|
|
162
|
+
pollingRef.current = setInterval(() => {
|
|
163
|
+
requestQrCode(targetPhone);
|
|
164
|
+
}, 5000);
|
|
165
|
+
},
|
|
166
|
+
[clearPolling, completePhone, requestQrCode, showError, showInfo],
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const saveOfficialConnection = useCallback(async () => {
|
|
170
|
+
const token = String(accessToken || '').trim();
|
|
171
|
+
const pnid = String(phoneNumberId || '').trim();
|
|
172
|
+
if (!token || !pnid) {
|
|
173
|
+
showError('Informe access token e phone_number_id da Cloud API.');
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
if (!peopleIri) {
|
|
177
|
+
showError('Selecione uma empresa.');
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const items = toArray(connectionsStore.getters?.items).map(normalizeConnection);
|
|
182
|
+
const statusIri =
|
|
183
|
+
selectedStatusIri ||
|
|
184
|
+
items.map(i => i.statusIri).find(Boolean) ||
|
|
185
|
+
null;
|
|
186
|
+
|
|
187
|
+
if (!selectedId && !statusIri) {
|
|
188
|
+
showError(
|
|
189
|
+
'Nao foi possivel resolver o status. Selecione uma conexao existente na lista ou crie uma nao oficial antes.',
|
|
190
|
+
);
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
setSubmitting(true);
|
|
195
|
+
try {
|
|
196
|
+
const payload = {
|
|
197
|
+
id: selectedId || undefined,
|
|
198
|
+
accessToken: token,
|
|
199
|
+
phoneNumberId: pnid,
|
|
200
|
+
apiVersion: apiVersion || 'v22.0',
|
|
201
|
+
people: peopleIri,
|
|
202
|
+
name: `WA Oficial ${pnid}`.slice(0, 50),
|
|
203
|
+
type: 'relationship',
|
|
204
|
+
status: statusIri || undefined,
|
|
205
|
+
};
|
|
206
|
+
const response = await connectionsActions.saveOfficialWhatsappConnection(payload);
|
|
207
|
+
showSuccess(
|
|
208
|
+
selectedId
|
|
209
|
+
? 'Conexao atualizada para canal oficial (Cloud API).'
|
|
210
|
+
: 'Conexao oficial salva. Envios usarao a Cloud API.',
|
|
211
|
+
);
|
|
212
|
+
setConnectionStatus('CONNECTED');
|
|
213
|
+
setQrValue('');
|
|
214
|
+
setProviderMode(PROVIDER_OFFICIAL);
|
|
215
|
+
connectionsStore.actions?.getItems?.({ people: peopleIri });
|
|
216
|
+
return response;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
showError(formatApiError(error));
|
|
219
|
+
return null;
|
|
220
|
+
} finally {
|
|
221
|
+
setSubmitting(false);
|
|
222
|
+
}
|
|
223
|
+
}, [
|
|
224
|
+
accessToken,
|
|
225
|
+
phoneNumberId,
|
|
226
|
+
apiVersion,
|
|
227
|
+
peopleIri,
|
|
228
|
+
selectedId,
|
|
229
|
+
selectedStatusIri,
|
|
230
|
+
connectionsActions,
|
|
231
|
+
connectionsStore.getters?.items,
|
|
232
|
+
connectionsStore.actions,
|
|
233
|
+
showError,
|
|
234
|
+
showSuccess,
|
|
235
|
+
]);
|
|
236
|
+
|
|
237
|
+
const handlePrimaryAction = useCallback(async () => {
|
|
238
|
+
if (providerMode === PROVIDER_OFFICIAL) {
|
|
239
|
+
return saveOfficialConnection();
|
|
240
|
+
}
|
|
241
|
+
return startPolling(completePhone);
|
|
242
|
+
}, [providerMode, saveOfficialConnection, startPolling, completePhone]);
|
|
243
|
+
|
|
244
|
+
const handleUseExistingConnection = useCallback(item => {
|
|
245
|
+
const raw = item || {};
|
|
246
|
+
const normalized = normalizeConnection(raw);
|
|
247
|
+
const nextDdi = String(raw?.phone?.ddi || '55');
|
|
248
|
+
const nextDdd = String(raw?.phone?.ddd || '');
|
|
249
|
+
const nextPhone = extractDigits(raw?.phone?.phone || '');
|
|
250
|
+
setDdi(nextDdi);
|
|
251
|
+
setDdd(nextDdd);
|
|
252
|
+
setPhone(nextPhone);
|
|
253
|
+
setConnectionStatus(normalized.status || 'Pendente');
|
|
254
|
+
setQrValue('');
|
|
255
|
+
setSelectedId(normalized.numericId || null);
|
|
256
|
+
setSelectedStatusIri(normalized.statusIri || null);
|
|
257
|
+
setProviderMode(
|
|
258
|
+
normalized.channel === CHANNEL_WHATSAPP_OFFICIAL
|
|
259
|
+
? PROVIDER_OFFICIAL
|
|
260
|
+
: PROVIDER_UNOFFICIAL,
|
|
261
|
+
);
|
|
262
|
+
setAccessToken('');
|
|
263
|
+
setPhoneNumberId('');
|
|
264
|
+
setFeedback({
|
|
265
|
+
type: 'info',
|
|
266
|
+
message: `Conexao #${normalized.numericId || '—'} selecionada. Ajuste o canal e salve.`,
|
|
267
|
+
});
|
|
268
|
+
}, []);
|
|
269
|
+
|
|
270
|
+
useFocusEffect(
|
|
271
|
+
useCallback(() => {
|
|
272
|
+
if (peopleIri) {
|
|
273
|
+
connectionsStore.actions?.getItems?.({ people: peopleIri });
|
|
274
|
+
}
|
|
275
|
+
return () => clearPolling();
|
|
276
|
+
}, [peopleIri, clearPolling]),
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
useEffect(() => () => clearPolling(), [clearPolling]);
|
|
280
|
+
|
|
281
|
+
const renderConnectionCard = useCallback(
|
|
282
|
+
({ item }) => {
|
|
283
|
+
if (!item) return null;
|
|
284
|
+
const n = normalizeConnection(item);
|
|
285
|
+
return (
|
|
286
|
+
<View>
|
|
287
|
+
<Text style={styles.previewName}>{n.name}</Text>
|
|
288
|
+
<Text style={styles.previewMeta}>
|
|
289
|
+
{n.phoneLabel} · {n.channel} · {n.status}
|
|
290
|
+
</Text>
|
|
291
|
+
</View>
|
|
292
|
+
);
|
|
293
|
+
},
|
|
294
|
+
[styles],
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
if (!providerId) {
|
|
298
|
+
return (
|
|
299
|
+
<SafeAreaView style={styles.container} edges={['bottom']}>
|
|
300
|
+
<View style={styles.centerState}>
|
|
301
|
+
<Icon name="building" size={32} color="#94A3B8" />
|
|
302
|
+
<Text style={styles.centerStateTitle}>Selecione uma empresa</Text>
|
|
303
|
+
</View>
|
|
304
|
+
</SafeAreaView>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return (
|
|
309
|
+
<SafeAreaView
|
|
310
|
+
style={[styles.container, { backgroundColor: brandColors.background }]}
|
|
311
|
+
edges={['bottom']}>
|
|
312
|
+
<ScrollView
|
|
313
|
+
contentContainerStyle={{ padding: 12, paddingBottom: 32 }}
|
|
314
|
+
keyboardShouldPersistTaps="handled">
|
|
315
|
+
{feedback ? (
|
|
316
|
+
<View
|
|
317
|
+
style={[
|
|
318
|
+
styles.feedbackBox,
|
|
319
|
+
{
|
|
320
|
+
backgroundColor:
|
|
321
|
+
feedback.type === 'error'
|
|
322
|
+
? withOpacity('#ef4444', 0.12)
|
|
323
|
+
: feedback.type === 'success'
|
|
324
|
+
? withOpacity('#10b981', 0.12)
|
|
325
|
+
: withOpacity('#2563eb', 0.1),
|
|
326
|
+
},
|
|
327
|
+
]}>
|
|
328
|
+
<Text style={styles.feedbackText}>{feedback.message}</Text>
|
|
329
|
+
</View>
|
|
330
|
+
) : null}
|
|
331
|
+
|
|
332
|
+
{selectedId ? (
|
|
333
|
+
<Text style={styles.selectedHint}>
|
|
334
|
+
Editando conexao #{selectedId}
|
|
335
|
+
{providerMode === PROVIDER_OFFICIAL ? ' · modo oficial' : ''}
|
|
336
|
+
</Text>
|
|
337
|
+
) : null}
|
|
338
|
+
|
|
339
|
+
<WhatsAppProviderForm
|
|
340
|
+
styles={styles}
|
|
341
|
+
brandColors={brandColors}
|
|
342
|
+
providerMode={providerMode}
|
|
343
|
+
setProviderMode={setProviderMode}
|
|
344
|
+
ddi={ddi}
|
|
345
|
+
setDdi={setDdi}
|
|
346
|
+
ddd={ddd}
|
|
347
|
+
setDdd={setDdd}
|
|
348
|
+
phone={phone}
|
|
349
|
+
setPhone={setPhone}
|
|
350
|
+
accessToken={accessToken}
|
|
351
|
+
setAccessToken={setAccessToken}
|
|
352
|
+
phoneNumberId={phoneNumberId}
|
|
353
|
+
setPhoneNumberId={setPhoneNumberId}
|
|
354
|
+
apiVersion={apiVersion}
|
|
355
|
+
setApiVersion={setApiVersion}
|
|
356
|
+
submitting={submitting}
|
|
357
|
+
onPrimaryAction={handlePrimaryAction}
|
|
358
|
+
/>
|
|
359
|
+
|
|
360
|
+
{connectionStatus ? (
|
|
361
|
+
<Text style={styles.statusLine}>Status: {connectionStatus}</Text>
|
|
362
|
+
) : null}
|
|
363
|
+
|
|
364
|
+
{qrValue ? (
|
|
365
|
+
<View style={styles.qrWrap}>
|
|
366
|
+
<QRCode value={qrValue} size={180} />
|
|
367
|
+
</View>
|
|
368
|
+
) : null}
|
|
369
|
+
|
|
370
|
+
<View style={{ minHeight: 280, marginTop: 16 }}>
|
|
371
|
+
<DefaultTable
|
|
372
|
+
storeName="connections"
|
|
373
|
+
requestParams={requestParams}
|
|
374
|
+
initialViewMode="table"
|
|
375
|
+
forceCardsOnCompact
|
|
376
|
+
showToolbar
|
|
377
|
+
showRowActions={false}
|
|
378
|
+
showColumnFiltersButton={false}
|
|
379
|
+
showSearch
|
|
380
|
+
searchKey="search"
|
|
381
|
+
searchPlaceholder="Buscar conexao"
|
|
382
|
+
onRowPress={handleUseExistingConnection}
|
|
383
|
+
renderCard={renderConnectionCard}
|
|
384
|
+
totalItemsLabel="connections"
|
|
385
|
+
visibleColumnsPreferenceKey="whatsapp-connections"
|
|
386
|
+
accentColor={brandColors.primary}
|
|
387
|
+
/>
|
|
388
|
+
</View>
|
|
389
|
+
</ScrollView>
|
|
390
|
+
</SafeAreaView>
|
|
391
|
+
);
|
|
392
|
+
}
|