-
Notifications
You must be signed in to change notification settings - Fork 101
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
ESW1234
wants to merge
13
commits into
master
Choose a base branch
from
ESW1234-miaw-custom-prechat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0264edc
Add files via upload
ESW1234 afd603b
Update customPrechatForm.js
ESW1234 dd9ccb8
Update customPrechatForm.js
ESW1234 7c83a71
Update customPrechatForm.js-meta.xml
ESW1234 8e68745
Update customPrechatFormField.js
ESW1234 f47e6ae
Update customPrechatFormField.js-meta.xml
ESW1234 3f4f188
Rename customPrechatForm.css to customPreChatForm.css
ESW1234 535e235
Update and rename customPrechatForm.html to customPreChatForm.html
ESW1234 dd3752c
Update and rename customPrechatForm.js to customPreChatForm.js
ESW1234 5118b65
Rename customPrechatForm.js-meta.xml to customPreChatForm.js-meta.xml
ESW1234 b09323f
Rename customPrechatFormField.html to customPreChatFormField.html
ESW1234 fbffe0e
Update and rename customPrechatFormField.js to customPreChatFormField.js
ESW1234 ad1befd
Rename customPrechatFormField.js-meta.xml to customPreChatFormField.j…
ESW1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||||||
* @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). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @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 } | ||||||
} | ||||||
)); | ||||||
} | ||||||
} | ||||||
} |
8 changes: 8 additions & 0 deletions
8
npachpande/async/miawCustomPrechat/customPreChatForm.js-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
npachpande/async/miawCustomPrechat/customPreChatFormField.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
npachpande/async/miawCustomPrechat/customPreChatFormField.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
npachpande/async/miawCustomPrechat/customPreChatFormField.js-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.