@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.
@@ -0,0 +1,115 @@
1
+ import React from 'react';
2
+ import { ActivityIndicator, Text, TextInput, TouchableOpacity, View } from 'react-native';
3
+ import Icon from 'react-native-vector-icons/Feather';
4
+ import { withOpacity } from '@controleonline/../../src/styles/branding';
5
+ import { PROVIDER_OFFICIAL, PROVIDER_UNOFFICIAL } from './whatsAppConnectionHelpers';
6
+
7
+ export default function WhatsAppProviderForm({
8
+ styles,
9
+ brandColors,
10
+ providerMode,
11
+ setProviderMode,
12
+ accessToken,
13
+ setAccessToken,
14
+ phoneNumberId,
15
+ setPhoneNumberId,
16
+ apiVersion,
17
+ setApiVersion,
18
+ submitting,
19
+ onPrimaryAction,
20
+ }) {
21
+ return (
22
+ <>
23
+ <View style={{ flexDirection: 'row', marginBottom: 12 }}>
24
+ <TouchableOpacity
25
+ onPress={() => setProviderMode(PROVIDER_UNOFFICIAL)}
26
+ style={[
27
+ styles.statusBadge,
28
+ {
29
+ marginRight: 8,
30
+ backgroundColor:
31
+ providerMode === PROVIDER_UNOFFICIAL
32
+ ? withOpacity('#2563EB', 0.18)
33
+ : withOpacity('#94A3B8', 0.12),
34
+ },
35
+ ]}>
36
+ <Text style={styles.statusBadgeText}>Nao oficial (QR)</Text>
37
+ </TouchableOpacity>
38
+ <TouchableOpacity
39
+ onPress={() => setProviderMode(PROVIDER_OFFICIAL)}
40
+ style={[
41
+ styles.statusBadge,
42
+ {
43
+ backgroundColor:
44
+ providerMode === PROVIDER_OFFICIAL
45
+ ? withOpacity('#10b981', 0.18)
46
+ : withOpacity('#94A3B8', 0.12),
47
+ },
48
+ ]}>
49
+ <Text style={styles.statusBadgeText}>Oficial (Cloud API)</Text>
50
+ </TouchableOpacity>
51
+ </View>
52
+
53
+ {providerMode === PROVIDER_OFFICIAL ? (
54
+ <View style={{ marginBottom: 12 }}>
55
+ <Text style={styles.inputLabel}>Access token (Meta)</Text>
56
+ <TextInput
57
+ style={styles.input}
58
+ value={accessToken}
59
+ onChangeText={setAccessToken}
60
+ placeholder="EAAG..."
61
+ placeholderTextColor="#94A3B8"
62
+ autoCapitalize="none"
63
+ secureTextEntry
64
+ />
65
+ <Text style={[styles.inputLabel, { marginTop: 8 }]}>Phone number ID</Text>
66
+ <TextInput
67
+ style={styles.input}
68
+ value={phoneNumberId}
69
+ onChangeText={setPhoneNumberId}
70
+ placeholder="123456789012345"
71
+ placeholderTextColor="#94A3B8"
72
+ autoCapitalize="none"
73
+ keyboardType="number-pad"
74
+ />
75
+ <Text style={[styles.inputLabel, { marginTop: 8 }]}>API version</Text>
76
+ <TextInput
77
+ style={styles.input}
78
+ value={apiVersion}
79
+ onChangeText={setApiVersion}
80
+ placeholder="v22.0"
81
+ placeholderTextColor="#94A3B8"
82
+ autoCapitalize="none"
83
+ />
84
+ </View>
85
+ ) : null}
86
+
87
+ <TouchableOpacity
88
+ activeOpacity={0.9}
89
+ disabled={submitting}
90
+ onPress={onPrimaryAction}
91
+ style={[
92
+ styles.primaryButton,
93
+ { backgroundColor: brandColors.primary },
94
+ submitting && styles.primaryButtonDisabled,
95
+ ]}>
96
+ {submitting ? (
97
+ <ActivityIndicator color="#FFFFFF" />
98
+ ) : (
99
+ <>
100
+ <Icon
101
+ name={providerMode === PROVIDER_OFFICIAL ? 'cloud' : 'smartphone'}
102
+ size={18}
103
+ color="#FFFFFF"
104
+ />
105
+ <Text style={styles.primaryButtonText}>
106
+ {providerMode === PROVIDER_OFFICIAL
107
+ ? 'Salvar conexao oficial'
108
+ : 'Solicitar QR Code'}
109
+ </Text>
110
+ </>
111
+ )}
112
+ </TouchableOpacity>
113
+ </>
114
+ );
115
+ }
@@ -0,0 +1,105 @@
1
+ export const CHANNEL_WHATSAPP = 'whatsapp';
2
+ export const CHANNEL_WHATSAPP_OFFICIAL = 'whatsapp_official';
3
+
4
+ /** @deprecated use CHANNEL_* */
5
+ export const PROVIDER_UNOFFICIAL = CHANNEL_WHATSAPP;
6
+ /** @deprecated use CHANNEL_* */
7
+ export const PROVIDER_OFFICIAL = CHANNEL_WHATSAPP_OFFICIAL;
8
+
9
+ export const extractDigits = value => String(value || '').replace(/\D/g, '');
10
+
11
+ export const formatPhoneLabel = phone => {
12
+ const ddd = String(phone?.ddd || '').trim();
13
+ const digits = extractDigits(phone?.phone || '');
14
+ if (!ddd && !digits) return 'Nao informado';
15
+ if (!digits) return `(${ddd})`;
16
+ if (digits.length <= 4) return ddd ? `(${ddd}) ${digits}` : digits;
17
+ const lastFour = digits.slice(-4);
18
+ const firstPart = digits.slice(0, -4);
19
+ return ddd ? `(${ddd}) ${firstPart}-${lastFour}` : `${firstPart}-${lastFour}`;
20
+ };
21
+
22
+ const resolveStatusIri = status => {
23
+ if (!status) return null;
24
+ if (typeof status === 'string') {
25
+ if (status.startsWith('/') || status.startsWith('http')) return status;
26
+ return null;
27
+ }
28
+ if (typeof status === 'object') {
29
+ const iri = status['@id'] || status.id;
30
+ if (iri == null) return null;
31
+ const asString = String(iri);
32
+ if (asString.startsWith('/') || asString.startsWith('http')) return asString;
33
+ return `/statuses/${asString}`;
34
+ }
35
+ return null;
36
+ };
37
+
38
+ const resolveNumericId = item => {
39
+ const raw = item?.id ?? item?.['@id'];
40
+ if (raw == null) return null;
41
+ const digits = String(raw).replace(/\D/g, '');
42
+ return digits ? Number(digits) : null;
43
+ };
44
+
45
+ export const normalizeConnection = item => {
46
+ const channel =
47
+ item?.channel === CHANNEL_WHATSAPP_OFFICIAL
48
+ ? CHANNEL_WHATSAPP_OFFICIAL
49
+ : CHANNEL_WHATSAPP;
50
+ return {
51
+ id: item?.id || item?.['@id'] || `connection-${Math.random()}`,
52
+ numericId: resolveNumericId(item),
53
+ name: item?.name || 'Sem nome',
54
+ status:
55
+ (typeof item?.status === 'object'
56
+ ? item?.status?.status || item?.status?.name
57
+ : item?.status) || 'Pendente',
58
+ statusIri: resolveStatusIri(item?.status),
59
+ type: item?.type || 'Nenhum',
60
+ channel,
61
+ provider: channel,
62
+ phone: item?.phone || {},
63
+ phoneLabel: formatPhoneLabel(item?.phone),
64
+ };
65
+ };
66
+
67
+ export const toArray = response => {
68
+ if (Array.isArray(response)) return response;
69
+ if (Array.isArray(response?.member)) return response.member;
70
+ if (Array.isArray(response?.['hydra:member'])) return response['hydra:member'];
71
+ return [];
72
+ };
73
+
74
+ export const formatApiError = error => {
75
+ if (!error) return 'Nao foi possivel concluir a operacao.';
76
+ if (typeof error === 'string') return error;
77
+ const violations = error?.violations || error?.data?.violations;
78
+ if (Array.isArray(violations) && violations.length) {
79
+ return violations
80
+ .map(v => v?.message || v?.propertyPath)
81
+ .filter(Boolean)
82
+ .join(' · ');
83
+ }
84
+ return (
85
+ error?.message ||
86
+ error?.description ||
87
+ error?.errmsg ||
88
+ error?.hydra?.description ||
89
+ 'Nao foi possivel concluir a operacao.'
90
+ );
91
+ };
92
+
93
+ export const buildOfficialChannelConfig = ({
94
+ accessToken,
95
+ phoneNumberId,
96
+ apiVersion,
97
+ }) => {
98
+ const config = {
99
+ access_token: String(accessToken || '').trim(),
100
+ phone_number_id: String(phoneNumberId || '').trim(),
101
+ };
102
+ const version = String(apiVersion || '').trim();
103
+ if (version) config.api_version = version;
104
+ return config;
105
+ };
@@ -1,26 +1,73 @@
1
- import { api } from "@controleonline/ui-common/src/api";
2
- import * as types from "@controleonline/ui-default/src/store/default/mutation_types";
3
-
4
- export const createWhatsappConnection = ({ commit, getters }, params = {}) => {
5
- commit(types.SET_ISLOADING, true);
6
-
7
- let options = {
8
- method: "POST",
9
- body: params,
10
- };
11
-
12
- return api
13
- .fetch("/whatsapp/create-session" , options)
14
- .then((data) => {
15
- commit(types.SET_ITEM, data);
16
-
17
- return data;
18
- })
19
- .catch((e) => {
20
- commit(types.SET_ERROR, e.message);
21
- throw e;
22
- })
23
- .finally(() => {
24
- commit(types.SET_ISLOADING, false);
25
- });
26
- };
1
+ import { api } from "@controleonline/ui-common/src/api";
2
+ import * as types from "@controleonline/ui-default/src/store/default/mutation_types";
3
+
4
+ export const createWhatsappConnection = ({ commit, getters }, params = {}) => {
5
+ commit(types.SET_ISLOADING, true);
6
+
7
+ const options = {
8
+ method: "POST",
9
+ body: params,
10
+ };
11
+
12
+ return api
13
+ .fetch("/whatsapp/create-session", options)
14
+ .then(data => {
15
+ commit(types.SET_ITEM, data);
16
+ return data;
17
+ })
18
+ .catch(e => {
19
+ commit(types.SET_ERROR, e.message);
20
+ throw e;
21
+ })
22
+ .finally(() => {
23
+ commit(types.SET_ISLOADING, false);
24
+ });
25
+ };
26
+
27
+ /**
28
+ * Persist official Meta Cloud API WhatsApp connection.
29
+ * channel=whatsapp_official + channelConfig JSON (write-only on API).
30
+ */
31
+ export const saveOfficialWhatsappConnection = ({ commit }, params = {}) => {
32
+ commit(types.SET_ISLOADING, true);
33
+
34
+ const body = {
35
+ name: params.name || `WA Oficial ${params.phoneNumberId || ""}`.trim().slice(0, 50),
36
+ channel: "whatsapp_official",
37
+ type: params.type || "relationship",
38
+ people: params.people,
39
+ channelConfig: JSON.stringify({
40
+ access_token: params.accessToken,
41
+ phone_number_id: params.phoneNumberId,
42
+ api_version: params.apiVersion || "v22.0",
43
+ }),
44
+ };
45
+
46
+ if (params.status) {
47
+ body.status = params.status;
48
+ }
49
+ if (params.phone) {
50
+ body.phone = params.phone;
51
+ }
52
+
53
+ const options = {
54
+ method: params.id ? "PUT" : "POST",
55
+ body,
56
+ };
57
+
58
+ const endpoint = params.id ? `/connections/${params.id}` : "/connections";
59
+
60
+ return api
61
+ .fetch(endpoint, options)
62
+ .then(data => {
63
+ commit(types.SET_ITEM, data);
64
+ return data;
65
+ })
66
+ .catch(e => {
67
+ commit(types.SET_ERROR, e.message);
68
+ throw e;
69
+ })
70
+ .finally(() => {
71
+ commit(types.SET_ISLOADING, false);
72
+ });
73
+ };
@@ -1,101 +1,105 @@
1
- import * as actions from "@controleonline/ui-default/src/store/default/actions";
2
- import * as getters from "@controleonline/ui-default/src/store/default/getters";
3
- import mutations from "@controleonline/ui-default/src/store/default/mutations";
4
- import * as customActions from "./customActions";
5
-
6
- export default {
7
- namespaced: true,
8
- state: {
9
- item: {},
10
- items: [],
11
- resourceEndpoint: "connections",
12
- isLoading: false,
13
- reload: false,
14
- error: "",
15
- totalItems: 0,
16
- summary: {},
17
- messages: [],
18
- message: {},
19
- filters: {},
20
- columns: [
21
- {
22
- editable: false,
23
- isIdentity: true,
24
- sortable: true,
25
- name: "id",
26
- align: "left",
27
- label: "id",
28
- sum: false,
29
- format: function (value) {
30
- return "#" + value;
31
- },
32
- },
33
- {
34
- externalFilter: false,
35
- sortable: true,
36
- name: "name",
37
- align: "left",
38
- label: "name",
39
- format: function (value) {
40
- return value;
41
- },
42
- },
43
- {
44
- externalFilter: false,
45
- sortable: true,
46
- editable: false,
47
- name: "phone",
48
- align: "left",
49
- label: "phone",
50
- format: function (value, row, column) {
51
- const phone = String(value.phone);
52
- const lastFour = phone.slice(-4);
53
- const firstPart = phone.slice(0, -4);
54
- return `(${value?.ddd}) ${firstPart}-${lastFour}`;
55
- },
56
- },
57
-
1
+ import * as actions from "@controleonline/ui-default/src/store/default/actions";
2
+ import * as getters from "@controleonline/ui-default/src/store/default/getters";
3
+ import mutations from "@controleonline/ui-default/src/store/default/mutations";
4
+ import * as customActions from "./customActions";
5
+
6
+ export default {
7
+ namespaced: true,
8
+ state: {
9
+ item: {},
10
+ items: [],
11
+ resourceEndpoint: "connections",
12
+ isLoading: false,
13
+ reload: false,
14
+ error: "",
15
+ totalItems: 0,
16
+ summary: {},
17
+ messages: [],
18
+ message: {},
19
+ filters: {},
20
+ columns: [
21
+ {
22
+ editable: false,
23
+ isIdentity: true,
24
+ sortable: true,
25
+ name: "id",
26
+ align: "left",
27
+ label: "id",
28
+ sum: false,
29
+ format: function (value) {
30
+ return "#" + value;
31
+ },
32
+ },
33
+ {
34
+ externalFilter: false,
35
+ sortable: true,
36
+ name: "name",
37
+ align: "left",
38
+ label: "name",
39
+ format: function (value) {
40
+ return value;
41
+ },
42
+ },
43
+ {
44
+ externalFilter: false,
45
+ sortable: true,
46
+ editable: false,
47
+ name: "phone",
48
+ align: "left",
49
+ label: "phone",
50
+ format: function (value, row, column) {
51
+ const phone = String(value.phone);
52
+ const lastFour = phone.slice(-4);
53
+ const firstPart = phone.slice(0, -4);
54
+ return `(${value?.ddd}) ${firstPart}-${lastFour}`;
55
+ },
56
+ },
57
+
58
58
  {
59
59
  externalFilter: false,
60
60
  sortable: true,
61
61
  name: "type",
62
62
  align: "left",
63
63
  label: "type",
64
- list: [
65
- {
66
- value: "crm",
67
- label: "CRM",
68
- },
69
- {
70
- value: "support",
71
- label: "Suporte",
72
- },
73
- {
74
- value: null,
75
- label: "Nenhum",
76
- },
77
- ],
78
- format: function (value) {
79
- return value;
80
- },
81
- },
82
-
64
+ list: [
65
+ {
66
+ value: "crm",
67
+ label: "CRM",
68
+ },
69
+ {
70
+ value: "support",
71
+ label: "Suporte",
72
+ },
73
+ {
74
+ value: null,
75
+ label: "Nenhum",
76
+ },
77
+ ],
78
+ format: function (value) {
79
+ return value;
80
+ },
81
+ },
82
+
83
83
  {
84
84
  sortable: true,
85
85
  editable: false,
86
86
  name: "channel",
87
87
  align: "left",
88
88
  label: "channel",
89
- list: [
90
- {
91
- value: "whatsapp",
92
- label: "Whatsapp",
93
- },
94
- ],
95
- format: function (value) {
96
- return value;
97
- },
98
- },
89
+ list: [
90
+ {
91
+ value: "whatsapp",
92
+ label: "WhatsApp",
93
+ },
94
+ {
95
+ value: "whatsapp_official",
96
+ label: "WhatsApp Oficial",
97
+ },
98
+ ],
99
+ format: function (value) {
100
+ return value;
101
+ },
102
+ },
99
103
  {
100
104
  editable: false,
101
105
  externalFilter: false,
@@ -103,22 +107,22 @@ export default {
103
107
  name: "status",
104
108
  align: "left",
105
109
  label: "status",
106
- list: "status/getItems",
107
- searchParam: "status",
108
- format: function (value) {
109
- return value?.status;
110
- },
111
- saveFormat: function (value) {
112
- return value ? "/statuses/" + (value.value || value) : null;
113
- },
114
- },
115
- {
116
- name: "connection",
117
- },
118
- ],
119
- print: [],
120
- },
121
- actions: { ...actions, ...customActions },
122
- getters,
123
- mutations,
124
- };
110
+ list: "status/getItems",
111
+ searchParam: "status",
112
+ format: function (value) {
113
+ return value?.status;
114
+ },
115
+ saveFormat: function (value) {
116
+ return value ? "/statuses/" + (value.value || value) : null;
117
+ },
118
+ },
119
+ {
120
+ name: "connection",
121
+ },
122
+ ],
123
+ print: [],
124
+ },
125
+ actions: { ...actions, ...customActions },
126
+ getters,
127
+ mutations,
128
+ };