Skip to content

Instantly share code, notes, and snippets.

@albarivas
Created November 20, 2020 09:31
Show Gist options
  • Save albarivas/c03d49a2a7dc416385df33a966d245f8 to your computer and use it in GitHub Desktop.
Save albarivas/c03d49a2a7dc416385df33a966d245f8 to your computer and use it in GitHub Desktop.
import { LightningElement, api, track } from 'lwc';
/******************
* APEX METHODS
******************/
import toogleValidationRule from '@salesforce/apex/DataloadSwitchController.toogleValidationRule';
export default class ToogleValidationRule extends LightningElement {
/*****************************
* PUBLIC REACTIVE PROPERTIES
*****************************/
@api rowId;// Id of validation rule.
@api manageableState;// Indicates the manageable state of the specified component that is contained in a package: beta, deleted, deprecated, deprecatedEditable, installed, installedEditable, released, unmanaged
@api validationName;// Api name of validation rule.
@api entityDefinitionId;// The entity definition for the object associated with the validation rule.
/*****************************
* PUBLIC FUNCTIONS
*****************************/
// Indicate if validation rule is active or inactive.
@api
get checked()
{
return this._checked;
}
set checked(value)
{
this._checked = value;
}
/*****************************
* PRIVATE REACTIVE PROPERTIES
*****************************/
@track isLoading = false;// Indicate if spinner is enabled (true).
@track _checked;// Is used in public function checked().
/*****************************
* PRIVATE PROPERTIES
*****************************/
hasRendered = false;// Indicate if component rendered the first time.
/******************
* LIFECYCLE HOOKS
******************/
connectedCallback()
{
console.log('>>>ToogleValidationRule (' + this.validationName + ') -- connectedCallback.');
this.isLoading = true;
console.log('>this.isLoading: ' + JSON.stringify(this.isLoading, null, '\t'));
}
renderedCallback()
{
console.log('>this.isLoading: ' + JSON.stringify(this.isLoading, null, '\t'));
console.log('>this.hasRendered: ' + JSON.stringify(this.hasRendered, null, '\t'));
if( ! this.hasRendered)
{
console.log('>>>ToogleValidationRule (' + this.validationName + ') -- renderedCallback.');
this.hasRendered = ! this.hasRendered;
this.isLoading = false;
console.log('>this.isLoading: ' + JSON.stringify(this.isLoading, null, '\t'));
}
}
/******************
* TEMPLATE
******************/
get buttonDisabled()
{
return this.manageableState !== 'unmanaged' ? true : false;
}
/*handleClick()
{
this.checked = ! this.checked;
this.isLoading = ! this.isLoading;
}*/
/******************
* EVENT HANDLER
******************/
handleToggle(event)
{
console.log('>>>BEGIN ToogleValidationRule -- handleToggle.');
console.log('>event.detail: ' + JSON.stringify(event.detail, null, '\t'));
const checked = event.detail.checked;
console.log('>#1');
this.isLoading = true;
console.log('>this.isLoading: ' + JSON.stringify(this.isLoading, null, '\t'));
setTimeout( () => {
toogleValidationRule({ validationRuleId: this.rowId, state: checked })
.then(result => {
console.log('>#2');
//console.log('>>>ToogleValidationRule -- then().');
//console.log('>result: ' + JSON.stringify(result, null, '\t'));
const httpResponse = JSON.parse(result);
this.handleResponse(httpResponse, checked);
console.log('>#4');
//this.isLoading = false;
console.log('>this.isLoading: ' + JSON.stringify(this.isLoading, null, '\t'));
})
.catch(error => {
console.error('>error: ' + JSON.stringify(error, null, '\t'));
/*notifyUser(
'Error!',
reduceErrors(error).join(', '),
'error',
'sticky'
);*/
this.isLoading = false;
this.checked = ! checked;
})
}, 3000);
console.log('>>>END ToogleValidationRule -- handleToggle.');
}
/******************
* HELPER FUNCTIONS
******************/
handleResponse(httpResponse, checked)
{
console.log('>#3');
console.log('>>>BEGIN ToogleValidationRule -- handleResponse.');
console.log('>httpResponse: ' + JSON.stringify(httpResponse, null, '\t'));
console.log('>checked: ' + JSON.stringify(checked, null, '\t'));
const statusCode = Number(httpResponse.statusCode);
console.log('>statusCode: ' + JSON.stringify(statusCode, null, '\t'));
let notifyMessage = {};
switch (statusCode) {
case 204:
console.log('>204');
this.checked = checked;
this.isLoading = false;
console.log('>this.checked: ' + JSON.stringify(this.checked, null, '\t'));
/*notifyMessage = createNotifyMessage(
`${this.validationName} on ${this.entityDefinitionId} was updated.`,
null,
'success',
'pester'
);*/
break;
default:
const body = JSON.parse(httpResponse.body);
console.log('>body: ' + JSON.stringify(body, null, '\t'));
this.checked = ! checked;// Revert change state when it is not allowed.
this.isLoading = false;
console.log('>this.checked: ' + JSON.stringify(this.checked, null, '\t'));
/*notifyMessage = createNotifyMessage(
'Error!',
reduceErrors(body).join(', '),
'error',
'sticky'
)*/
break;
}
console.log('>notifyMessage: ' + JSON.stringify(notifyMessage, null, '\t'));
/*notifyUser(
notifyMessage.title,
notifyMessage.message,
notifyMessage.variant,
notifyMessage.mode
);*/
console.log('>>>END ToogleValidationRule -- handleResponse.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment