Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created July 16, 2023 15:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arun12209/c99f2b37a24f6f1c4264efd7d26005b2 to your computer and use it in GitHub Desktop.
subscribedAccountsList.js
import { LightningElement, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import getSubscribedAccounts from '@salesforce/apex/AccountController.getSubscribedAccounts';
import removeAccountSubscription from '@salesforce/apex/AccountController.removeAccountSubscription';
import { refreshApex } from '@salesforce/apex';
export default class SubscribedAccountsList extends NavigationMixin(LightningElement) {
accounts = [];
wiredAccountsResult;
isLoading = false;
@wire(getSubscribedAccounts)
wiredSubscribedAccounts(result) {
this.wiredAccountsResult = result;
if (result.data) {
this.accounts = result.data;
} else if (result.error) {
console.error('Error retrieving subscribed accounts:', result.error);
}
}
handleRefresh() {
this.isLoading = true; // Show the spinner
refreshApex(this.wiredAccountsResult)
.finally(() => {
this.isLoading = false; // Hide the spinner
});
}
handleAccountClick(event) {
const accountId = event.target.dataset.id;
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: accountId,
objectApiName: 'Account',
actionName: 'view'
}
});
}
handleRemoveClick(event) {
const accountId = event.target.dataset.id;
removeAccountSubscription({ accountId })
.then(result => {
if (result) {
this.accounts = this.accounts.filter(account => account.Id !== accountId);
localStorage.setItem('subscribedAccounts', JSON.stringify(this.accounts));
this.showToast('Success', 'Account subscription removed.', 'success');
} else {
this.showToast('Error', 'Failed to remove account subscription.', 'error');
}
})
.catch(error => {
console.error('Error removing account subscription:', error);
this.showToast('Error', 'Failed to remove account subscription.', 'error');
});
}
showToast(title, message, variant) {
const event = new ShowToastEvent({
title: title,
message: message,
variant: variant
});
this.dispatchEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment