-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (94 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const openNewTab = () => {
const loc = window.location;
const href = `https://${loc.host}/${loc.pathname}?color=white`;
window.open(href, '_blank').focus();
};
const maybeSetBackgroundColor = () => {
const urlParams = new URLSearchParams(window.location.search);
const color = urlParams.get('color');
if (color) document.body.style.backgroundColor = color;
};
const setInfoValue = (elementId, value) => {
const element = document.getElementById(elementId);
element.innerText = value;
};
const copyElementTextOnClick = elementId => {
const element = document.getElementById(elementId);
if (element) {
element.addEventListener('click', () => {
navigator.clipboard.writeText(element.innerText);
});
}
};
const copyOnClick = (elementId, getValue) => {
const element = document.getElementById(elementId);
if (element) {
element.addEventListener('click', () => {
navigator.clipboard.writeText(getValue());
});
}
};
const onUpdateInformation = customAttributesUpdateMethod => {
const information = JSON.parse(document.getElementById('updateInformation').value);
sm.getApi({version: 'v1'}).then(glia => {
glia.updateInformation({...information, customAttributesUpdateMethod});
});
};
const defaultVisitorInformation = `{
"name": "new name",
"phone": "+155555555",
"email": "[email protected]",
"note": "new note",
"customAttributes": {
"key": "value"
}
}`;
const boot = () => {
maybeSetBackgroundColor();
copyElementTextOnClick('siteId');
copyElementTextOnClick('visitorId');
copyElementTextOnClick('engagementId');
copyElementTextOnClick('operatorId');
copyElementTextOnClick('visitorCode');
copyOnClick('accessToken', () => sm.accessToken)
document.getElementById('updateInformation').value = defaultVisitorInformation;
sm.getApi({version: 'v1'}).then(glia => {
setInfoValue('siteId', glia.getSiteId());
setInfoValue('visitorId', glia.getVisitorId());
const onEngagementStart = engagement => {
setInfoValue('engagementId', engagement.engagementId);
setInfoValue('operatorId', engagement.operator.id);
};
const onEngagementEnd = engagement => {
setInfoValue('engagementId', '');
setInfoValue('operatorId', '');
};
glia.addEventListener(glia.EVENTS.ENGAGEMENT_START, onEngagementStart);
glia.addEventListener(glia.EVENTS.ENGAGEMENT_END, onEngagementEnd);
glia.omnibrowse.getVisitorCode().then(visitorCodeResponse => {
const element = document.getElementById('visitorCode');
element.innerText = visitorCodeResponse.code;
});
glia.getQueues().then(queues => {
const queuesEl = document.getElementById('queues');
queues.forEach(queue => {
const id = queue.id;
const name = queue.name;
const status = queue.state.status;
const queueEl = document.createElement('li');
queueEl.innerText = `${name} - ${status}`;
queueEl.addEventListener('click', _event => {
glia.queueForEngagement('text', {queueId: id});
});
queuesEl.append(queueEl);
});
});
});
};
const externalSessionId = (new URLSearchParams(window.location.search)).get('external_session_id');
window.getGliaContext = () => {
return {
sessionId: externalSessionId
};
};
window.addEventListener('load', boot);