-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathcreate-new-chat.js
118 lines (102 loc) · 3.71 KB
/
create-new-chat.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
115
116
117
118
const $targetEl = document.getElementById('crud-modal');
const modal = new Modal($targetEl);
const appId = "<Add your App ID here!>";
Talk.ready.then(function () {
const me = new Talk.User({
id: '123457',
name: 'Alice',
email: '[email protected]',
photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
role: 'default',
welcomeMessage: 'Hey there! How are you? :-)',
});
window.talkSession = new Talk.Session({
appId: appId,
me: me,
});
const conversation = talkSession.getOrCreateConversation("createNewChatExampleConv");
conversation.setParticipant(me);
// Create some test users for this example
let testUsers = getTalkJSUsers();
for (let id in testUsers) {
let user = new Talk.User(testUsers[id]);
conversation.setParticipant(user);
}
const inbox = talkSession.createInbox({ theme: 'Create-New-Chat-From-Conv-Header' });
inbox.select(conversation);
inbox.mount(document.getElementById('talkjs-container'));
inbox.onCustomConversationAction('createNewChat', showNewConversationModal);
});
function createNewChat(event) {
event.preventDefault();
const conv_id = "" + Date.now();
const conversation = talkSession.getOrCreateConversation(conv_id);
const me = new Talk.User(talkSession.me.id);
conversation.setParticipant(me);
// Get the selected values from the multi-select dropdown
let selectElement = document.getElementById('participants');
for (let i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].selected) {
let user = new Talk.User(selectElement.options[i].value);
conversation.setParticipant(user);
}
}
if (event.target.elements.subject.value) {
conversation.setAttributes({
subject: event.target.elements.subject.value
});
}
let inbox = talkSession.createInbox({ theme: 'Create-New-Chat-From-Conv-Header' });
inbox.select(conversation);
inbox.mount(document.getElementById('talkjs-container'));
inbox.onCustomConversationAction('createNewChat', showNewConversationModal);
modal.hide();
return false
}
function showNewConversationModal() {
let talkJsUsers = getTalkJSUsers();
participantsSelect = document.getElementById('participants');
for (let i = 0; i < talkJsUsers.length; i++) {
participantsSelect.options[i] = new Option(talkJsUsers[i].name, talkJsUsers[i].id);
}
modal.show();
}
function getTalkJSUsers() {
return [
{
id: "New_Zafar",
name: "Zafar",
role: "default",
email: "[email protected]",
photoUrl: "https://i.pravatar.cc/[email protected]",
},
{
id: "New_Simba",
name: "Simba",
role: "default",
email: "[email protected]",
photoUrl: "https://i.pravatar.cc/[email protected]",
},
{
id: "New_Xiang",
name: "Xiang",
role: "default",
email: "[email protected]",
photoUrl: "https://i.pravatar.cc/[email protected]",
},
{
id: "New_Patel",
name: "Patel",
role: "default",
email: "[email protected]",
photoUrl: "https://i.pravatar.cc/[email protected]",
},
{
id: "New_Sapnesh",
name: "Sapnesh",
role: "default",
email: "[email protected]",
photoUrl: "https://i.pravatar.cc/[email protected]",
}
];
}