Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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;
Copy link
Owner Author

Choose a reason for hiding this comment

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

just some basic styling here. planning to just add a note about configuration.branding so that customers can consume the branding information from config as they seem fit.

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-prechat-form-field key={field.name}
Copy link
Owner Author

@ESW1234 ESW1234 Aug 14, 2023

Choose a reason for hiding this comment

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

created a separate component to render each formField, mainly to demonstrate how a customer can distinguish between a text field vs a dropdown (choiceList) field.

field-info={field}>
</c-custom-prechat-form-field>
</template>
<lightning-button label={startConversationLabel}
title={startConversationLabel}
onclick={handleStartConversation}
class="slds-m-left_x-small">
</lightning-button>
</template>
61 changes: 61 additions & 0 deletions npachpande/async/miawCustomPrechat/customPrechatForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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() {
Copy link
Owner Author

Choose a reason for hiding this comment

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

thinking of renaming this to isFormValid

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 }
}
));
}
}
}
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>55.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}>
Copy link
Owner Author

Choose a reason for hiding this comment

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

ChoiceLists can be rendered using lightning-combobox while the rest of the fields can simply be lightning-input with the appropriate value for type attribute.

<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>
60 changes: 60 additions & 0 deletions npachpande/async/miawCustomPrechat/customPrechatFormField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { track, api, LightningElement } from "lwc";

export default class CustomPrechatFormField extends LightningElement {
choiceListDefaultValue;

@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() {
Copy link
Owner Author

Choose a reason for hiding this comment

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

required for validating each formField on Start Conversation click. A customer can choose their own way of validating the form but I just thought we can show them an example.

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();
}

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>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
</targets>
</LightningComponentBundle>