-
Notifications
You must be signed in to change notification settings - Fork 0
/
floating-button-sdk copy.js
104 lines (93 loc) · 3.83 KB
/
floating-button-sdk copy.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
class FloatingButton {
constructor(clientId, udid = undefined, authCode = undefined) {
this.clientId = clientId;
this.userId;
}
init() {
this.handleAuth('test', 'test')
.then(res => {
console.log(res);
this.userId = res;
console.log('userId', this.userId);
this.chatUrl = `https://accio-webclient-git-test-sdk-waddle.vercel.app/${this.clientId}/sdk/${this.userId}`;
});
console.log('this userId', this.userId);
// Create floating button
this.button = document.createElement('div');
this.button.className = 'floating-button-common button-image-shrink';
this.button.type = 'button';
this.expandedButton = document.createElement('div');
this.expandedButton.className = 'expanded-button';
this.expandedText = document.createElement('p');
this.expandedButton.appendChild(this.expandedText);
this.expandedText.innerText = '테스트 중인 문장입니다 테스트 중인 문장입니다';
this.expandedText.className = 'expanded-text';
// Button click event
this.button.addEventListener('click', (e) => {
e.stopPropagation();
e.preventDefault();
// this.handleAuth('test', 'test')
// .then(res => this.userId = res);
this.openChat(e);
});
document.body.appendChild(this.button);
document.body.appendChild(this.expandedButton);
setTimeout(() => {
this.expandedButton.innerText = '';
this.expandedButton.style.width = '50px';
this.expandedButton.style.padding = 0;
this.button.className = 'floating-button-common button-image';
}, [2000])
}
openChat(e) {
e.stopPropagation();
e.preventDefault();
console.log('openChat');
const targetElem = document.getElementsByClassName('floating-button-common')[0];
const iframeContainer = document.createElement('div');
iframeContainer.className = 'iframe-container-shrink';
const chatHeader = document.createElement('div');
chatHeader.className = 'chat-header';
const iframe = document.createElement('iframe');
console.log('chatUrl', this.chatUrl);
iframe.src = this.chatUrl;
iframe.className = 'chat-iframe';
iframeContainer.appendChild(chatHeader);
iframeContainer.appendChild(iframe);
targetElem.appendChild(iframeContainer);
chatHeader.addEventListener('click', (e) => {
e.stopPropagation();
e.preventDefault();
console.log(e.target);
// targetElem.removeChild(iframeContainer);
iframeContainer.className = 'iframe-container';
})
window.addEventListener('message', (e) => {
iframeContainer.className = 'iframe-container';
})
}
async handleAuth(udid, authCode) {
try {
const response = await fetch(
'https://hg5eey52l4.execute-api.ap-northeast-2.amazonaws.com/dev/auth', {
method: "POST",
headers: {
'Content-Type': 'application/json',
'x-api-key': 'G4J2wPnd643wRoQiK52PO9ZAtaD6YNCAhGlfm1Oc',
'udid': udid,
'authCode': authCode,
},
body: '',
}
);
const result = await response.json();
console.log('dlst res, ', result.body.randomId);
return result.body.randomId
} catch (error) {
console.error(`Error while calling auth API: ${error.message}`);
return null
}
}
}
// Export as a global variable
window.FloatingButton = FloatingButton;