-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathchatbot-widget.component.ts
More file actions
131 lines (110 loc) · 3.46 KB
/
chatbot-widget.component.ts
File metadata and controls
131 lines (110 loc) · 3.46 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ChangeDetectorRef, Component, ElementRef, ViewEncapsulation } from '@angular/core';
import {NgClass} from "@angular/common";
import { LocalStorageService } from '../services/local-storage.service';
import { LoginInfo } from '../models/interfaces';
import { environment } from 'src/environments/environment';
@Component({
selector: 'app-chatbot-widget',
standalone: true,
imports: [NgClass],
templateUrl: './chatbot-widget.component.html',
styleUrl: './chatbot-widget.component.css',
encapsulation: ViewEncapsulation.None
})
export class ChatbotWidgetComponent {
args: any;
state: Boolean;
messages: Array<any>;
chatbox: any;
constructor(
private cdr: ChangeDetectorRef,
private el: ElementRef,
private localStorage: LocalStorageService
){}
ngOnInit() {
this.chatbox = this.el.nativeElement.querySelector('.chatbox__support')
this.state = false;
this.messages = [];
}
onKeyUp(event: KeyboardEvent) {
if (event.key === "Enter") {
this.onSendButton()
}
}
toggleState() {
this.state = !this.state;
this.cdr.detectChanges();
this.chatbox = this.el.nativeElement.querySelector('.chatbox__support')
this.updateChatText();
// show or hides the box
/*if(this.state) {
this.chatbox.classList.add('chatbox--active')
} else {
this.chatbox.classList.remove('chatbox--active')
}*/
}
onSendButton() {
const textField = this.chatbox.querySelector('input');
let text1 = textField.value
if (text1 === "") {
return;
}
// Build the message depending on the user role
let name = "guest"
let role = environment.BUYER_ROLE; // FIXME: Default role must be guest when supported
const userInfo = this.localStorage.getObject('login_items') as LoginInfo;
// The user is logged in
if (userInfo.id) {
let roles = []
role = environment.BUYER_ROLE;
name = userInfo.username
if (userInfo.logged_as !== userInfo.id) {
let loggedOrg = userInfo.organizations.find((element: { id: any; }) => element.id == userInfo.logged_as)
roles = loggedOrg.roles.map((elem: any) => {
return elem.name
})
} else {
roles = userInfo.roles.map((elem: any) => {
return elem.name
})
}
if (roles.includes(environment.SELLER_ROLE)) {
role = "Provider"
}
}
let msg1 = { name: name, message: text1, role: role }
this.messages.push(msg1);
this.updateChatText()
textField.value = ''
fetch(environment.CHAT_API, {
method: 'POST',
body: JSON.stringify({ message: text1, role: role }),
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
})
.then(r => r.json())
.then(r => {
let msg2 = { name: "DomeGPT", message: r.answer };
this.messages.push(msg2);
this.updateChatText()
}).catch((error) => {
console.error('Error:', error);
this.updateChatText()
});
}
updateChatText() {
var html = '';
this.messages.slice().reverse().forEach(function(item, index) {
if (item.name === "DomeGPT") {
html += '<div class="messages__item messages__item--visitor">' + item.message + '</div>'
} else {
html += '<div class="messages__item messages__item--operator">' + item.message + '</div>'
}
});
const chatmessage = this.chatbox.querySelector('.chatbox__messages');
chatmessage.innerHTML = html;
this.cdr.detectChanges();
}
}