-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathcustomPrechatForm.js
More file actions
61 lines (51 loc) · 1.82 KB
/
customPrechatForm.js
File metadata and controls
61 lines (51 loc) · 1.82 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
import { track, api, LightningElement } from "lwc";
export default class CustomPrechatForm extends LightningElement {
@api configuration = {};
startConversationLabel;
get prechatForm() {
const forms = this.configuration.forms || [];
return forms.find(form => form.formType === "PreChat") || {};
}
get prechatFormFields() {
return this.prechatForm.formFields || [];
}
get fields() {
let fields = JSON.parse(JSON.stringify(this.prechatFormFields));
this.addChoiceListValues(fields);
return fields.sort((fieldA, fieldB) => fieldA.order - fieldB.order);
}
connectedCallback() {
this.startConversationLabel = "Start Conversation";
}
addChoiceListValues(fields) {
for (let field of fields) {
if (field.type === "ChoiceList") {
const valueList = this.configuration.choiceListConfig.choiceList.find(list => list.choiceListId === field.choiceListId) || {};
field.choiceListValues = valueList.choiceListValues || [];
}
}
}
isValid() {
let isFormValid = true;
this.template.querySelectorAll("c-custom-prechat-form-field").forEach(formField => {
if (!formField.reportValidity()) {
isFormValid = false;
}
});
return isFormValid;
}
handleStartConversation() {
const prechatData = {};
if (this.isValid()) {
this.template.querySelectorAll("c-custom-prechat-form-field").forEach(formField => {
prechatData[formField.name] = String(formField.value);
});
this.dispatchEvent(new CustomEvent(
"prechatsubmit",
{
detail: { value: prechatData }
}
));
}
}
}