Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIAW custom pre-chat example #155

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions npachpande/async/miawCustomPrechat/customPreChatForm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:host {
display: flex;
flex-direction: column;
flex: 1 1 auto;
overflow: hidden;
background: #FFFFFF;
padding: 2em;
}

lightning-button {
padding-top: 2em;
text-align: center;
}
12 changes: 12 additions & 0 deletions npachpande/async/miawCustomPrechat/customPreChatForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<template class="slds-m-around_medium" for:each={fields} for:item="field">
<c-custom-pre-chat-form-field key={field.name}
field-info={field}>
</c-custom-pre-chat-form-field>
</template>
<lightning-button label={startConversationLabel}
title={startConversationLabel}
onclick={onStartConversationClick}
class="slds-m-left_x-small">
</lightning-button>
</template>
80 changes: 80 additions & 0 deletions npachpande/async/miawCustomPrechat/customPreChatForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns prechat form fields sorted by their display order.
* Returns pre-chat 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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Adds values to fields of type choiceList (dropdown).
* Adds values to choiceList (dropdown) fields.

*/
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Iterates over and reports validity for each form field. Returns true if all the fields are valid.
* Iterates over and validates 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Gathers and submits prechat data to the app on start converation button click.
* Gathers and submits pre-chat data to the app on start-conversation-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 }
}
));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightningSnapin__MessagingPreChat</target>
</targets>
</LightningComponentBundle>
18 changes: 18 additions & 0 deletions npachpande/async/miawCustomPrechat/customPreChatFormField.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<template lwc:if={isTypeChoiceList}>
<lightning-combobox key={fieldInfo.name}
label={fieldInfo.labels.display}
options={choiceListOptions}
value={choiceListDefaultValue}
required={fieldInfo.required}>
</lightning-combobox>
</template>
<template lwc:else>
<lightning-input key={fieldInfo.name}
type={type}
label={fieldInfo.labels.display}
max-length={fieldInfo.maxLength}
required={fieldInfo.required}>
</lightning-input>
</template>
</template>
68 changes: 68 additions & 0 deletions npachpande/async/miawCustomPrechat/customPreChatFormField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { track, api, LightningElement } from "lwc";

export default class CustomPreChatFormField extends LightningElement {
choiceListDefaultValue;

/**
* Form field data.
* @type {Object}
*/
@api fieldInfo = {};

@api
get name() {
return this.fieldInfo.name;
}

@api
get value() {
const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input");
return this.isTypeCheckbox ? lightningCmp.checked : lightningCmp.value;
}

@api
reportValidity() {
const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input");
return lightningCmp.reportValidity();
}

get type() {
switch (this.fieldInfo.type) {
case "Phone":
return "tel";
case "Text":
case "Email":
case "Number":
case "Checkbox":
case "ChoiceList":
return this.fieldInfo.type.toLowerCase();
default:
return "text";
}
}

get isTypeCheckbox() {
return this.type === "Checkbox".toLowerCase();
}

get isTypeChoiceList() {
return this.type === "ChoiceList".toLowerCase();
}

/**
* Formats choiceList options and sets the default value.
* @type {Array}
*/
get choiceListOptions() {
let choiceListOptions = [];
const choiceListValues = [...this.fieldInfo.choiceListValues];
choiceListValues.sort((valueA, valueB) => valueA.order - valueB.order);
for (const listValue of choiceListValues) {
if (listValue.isDefaultValue) {
this.choiceListDefaultValue = listValue.choiceListValueName;
}
choiceListOptions.push({ label: listValue.label, value: listValue.choiceListValueName });
}
return choiceListOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<isExposed>true</isExposed>
<targets>
</targets>
</LightningComponentBundle>