-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathcustomPreChatForm.js
More file actions
80 lines (70 loc) · 2.34 KB
/
customPreChatForm.js
File metadata and controls
80 lines (70 loc) · 2.34 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
import { track, api, LightningElement } from "lwc";
export default class CustomPreChatForm extends LightningElement {
/**
* Deployment configuration data.
* @type {Object}
*/
@api configuration = {};
startConversationLabel;
get prechatForm() {
const forms = this.configuration.forms || [];
return forms.find(form => form.formType === "PreChat") || {};
}
get prechatFormFields() {
return this.prechatForm.formFields || [];
}
/**
* Returns prechat form fields sorted by their display order.
* @type {Object[]}
*/
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";
}
/**
* Adds values to fields of type choiceList (dropdown).
*/
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 || [];
}
}
}
/**
* Iterates over and reports validity for each form field. Returns true if all the fields are valid.
* @type {boolean}
*/
isValid() {
let isFormValid = true;
this.template.querySelectorAll("c-custom-pre-chat-form-field").forEach(formField => {
if (!formField.reportValidity()) {
isFormValid = false;
}
});
return isFormValid;
}
/**
* Gathers and submits prechat data to the app on start converation button click.
* @type {boolean}
*/
onStartConversationClick() {
const prechatData = {};
if (this.isValid()) {
this.template.querySelectorAll("c-custom-pre-chat-form-field").forEach(formField => {
prechatData[formField.name] = String(formField.value);
});
this.dispatchEvent(new CustomEvent(
"prechatsubmit",
{
detail: { value: prechatData }
}
));
}
}
}