import { LightningElement, wire, api, track } from "lwc";
import getPartners from "@salesforce/apex/AccountController.getPartners";
import getRecipients from "@salesforce/apex/AccountController.getRecipients";
import getEmailBody from "@salesforce/apex/AccountController.getEmailBody";
import sendEmail from "@salesforce/apex/AccountController.sendEmail";
import emailTemplateLabel from "@salesforce/label/c.AccountTemplates";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
const columns = [
{ label: "First Name", fieldName: "FirstName", type: "text" },
{ label: "Last Name", fieldName: "LastName", type: "text" },
{ label: "Email", fieldName: "Email", type: "text" },
{ label: "Phone", fieldName: "Phone", type: "Text" },
{ label: "Country", fieldName: "MailingCountry", type: "Text" }
];
const DELAY = 300;
export default class AccountsTable extends LightningElement {
@api recordId = null;
@track showLoading = true;
@track showModelLoading = true;
@track searchKey = "";
@track page = 1;
@track items = [];
@track data = [];
@track columns;
@track startingRecord = 1;
@track endingRecord = 0;
@track pageSize = 10;
@track totalRecountCount = 0;
@track totalPage = 0;
@track disableEmail = true;
@track allSelectedRows = [];
@track error;
@track showModal = false;
@track recipients;
@track selectedrecipients;
@track selectedTemplate;
@track subject;
@track htmlBody;
isPageChanged = false;
initialLoad = true;
@wire(getPartners, {
recId: "$recordId",
searchKey: "$searchKey"
})
wiredPartners({ error, data }) {
if (data) {
this.processRecords(data);
this.error = undefined;
this.showLoading = false;
} else if (error) {
this.error = error;
this.data = undefined;
this.showLoading = false;
}
}
processRecords(data) {
this.items = data;
this.totalRecountCount = data.length;
this.totalPage = Math.ceil(this.totalRecountCount / this.pageSize);
const arr = this.items.slice(0, this.pageSize);
const newArr = arr.map((object) => {
return { ...object, Id: object.Id + "-1" };
});
this.data = newArr;
this.endingRecord = this.pageSize;
this.columns = columns;
}
get disbalePrevious() {
return this.page == 1;
}
get disbaleNext() {
return this.page == this.totalPage;
}
previousHandler() {
this.showLoading = true;
this.isPageChanged = true;
if (this.page > 1) {
this.page = this.page - 1;
this.displayRecordPerPage(this.page);
}
this.showLoading = false;
}
nextHandler() {
this.showLoading = true;
this.isPageChanged = true;
if (this.page < this.totalPage && this.page !== this.totalPage) {
this.page = this.page + 1;
this.displayRecordPerPage(this.page);
}
this.showLoading = false;
}
displayRecordPerPage(page) {
this.startingRecord = (page - 1) * this.pageSize;
this.endingRecord = this.pageSize * page;
this.endingRecord =
this.endingRecord > this.totalRecountCount
? this.totalRecountCount
: this.endingRecord;
const arr = this.items.slice(this.startingRecord, this.endingRecord);
const newArr = arr.map((object) => {
return { ...object, Id: object.Id + "-" + page };
});
this.data = newArr;
this.startingRecord = this.startingRecord + 1;
this.template.querySelector(
'[data-id="table"]'
).selectedRows = this.allSelectedRows;
}
onRowSelection(event) {
if (!this.isPageChanged || this.initialLoad) {
this.initialLoad = false;
var selectedRows = event.target.selectedRows;
var allSelectedRows = this.allSelectedRows;
var currentPageNumber = this.page;
//Process the rows now
//Condition 1 -> If any new row selected, add to our allSelectedRows attribute
//Condition 2 -> If any row is deselected, remove from allSelectedRows attribute
//Solution - Remove all rows from current page from allSelectedRows attribute and then add again
//Removing all rows coming from curent page from allSelectedRows
var i = allSelectedRows.length;
if (i > 0) {
while (i--) {
var pageNumber = allSelectedRows[i].split("-")[1];
if (pageNumber && pageNumber == currentPageNumber) {
allSelectedRows.splice(i, 1);
}
}
}
selectedRows.forEach(function (row) {
allSelectedRows.push(row);
});
if (allSelectedRows.length > 0) {
this.disableEmail = false;
} else {
this.disableEmail = true;
}
this.allSelectedRows = allSelectedRows;
} else {
this.isPageChanged = false;
}
}
handleSearchChange(event) {
this.showLoading = true;
window.clearTimeout(this.delayTimeout);
const searchKey = event.target.value;
this.delayTimeout = setTimeout(() => {
this.searchKey = searchKey;
var data = [];
for (var i = 0; i < this.items.length; i++) {
if (
this.items[i] != undefined &&
this.items[i].FirstName.includes(this.searchKey)
) {
data.push(this.items[i]);
}
}
this.processRecords(data);
this.page = 1;
this.showLoading = false;
}, DELAY);
}
openEmailPopup() {
this.showModal = true;
this.showModelLoading = true;
var recs = [];
for (var i = 0; i < this.allSelectedRows.length; i++) {
recs.push(this.allSelectedRows[i].split("-")[0]);
}
getRecipients({ recIds: recs })
.then((result) => {
this.showModelLoading = false;
this.recipients = result;
this.selectedrecipients = result;
this.error = undefined;
})
.catch((error) => {
this.showModelLoading = false;
this.recipients = undefined;
this.selectedrecipients = undefined;
this.error = error;
});
}
get templateOptions() {
var options = emailTemplateLabel.split(";");
var resultMap = [];
for (let i = 0; i < options.length; i++) {
resultMap.push({
class: "optionClass",
label: options[i],
value: options[i]
});
}
return resultMap;
}
onChangeRecipient(event) {
this.recipients = event.detail.value;
}
onChangeSubject(event) {
this.subject = event.detail.value;
}
onChangeContent(event) {
this.htmlBody = event.detail.value;
}
handleTemplateChange(event) {
this.showModelLoading = true;
this.selectedTemplate = event.detail.value;
getEmailBody({ leadId: this.recordId, templateName: this.selectedTemplate })
.then((result) => {
this.showModelLoading = false;
for (var key in result) {
this.subject = key;
this.htmlBody = result[key];
}
this.error = undefined;
})
.catch((error) => {
this.showModelLoading = false;
this.error = error;
this.subject = undefined;
this.htmlBody = undefined;
});
}
get noData() {
return this.items.length == 0 ? true : false;
}
closeModal() {
this.closeEmailModel();
}
onClickSendEmail() {
if (
this.recipients == undefined ||
this.recipients == null ||
this.recipients == ""
) {
this.showToast("error", "Error", "Enter the Recipients");
} else if (
this.subject == undefined ||
this.subject == null ||
this.subject == ""
) {
this.showToast("error", "Error", "Enter the Subject");
} else if (
this.htmlBody == undefined ||
this.htmlBody == null ||
this.htmlBody == ""
) {
this.showToast("error", "Error", "Enter the Content");
} else {
this.showModelLoading = true;
sendEmail({
leadId: this.recordId,
toAddress: this.recipients,
subject: this.subject,
body: this.htmlBody
})
.then((result) => {
this.showToast("success", "Success!", "Email sent successfully.");
this.error = undefined;
this.closeEmailModel();
})
.catch((error) => {
this.showModelLoading = false;
this.error = error;
this.showToast("error", "Email sending failed!", this.error);
});
}
}
closeEmailModel() {
this.recipients = this.selectedrecipients;
this.selectedTemplate = undefined;
this.subject = undefined;
this.htmlBody = undefined;
this.showModelLoading = false;
this.showModal = false;
}
showToast(type, title, msg) {
const evt = new ShowToastEvent({
title: title,
message: msg,
variant: type,
mode: "dismissable"
});
this.dispatchEvent(evt);
}
}