0

My LWC code looks like:

@api recordId;

labels = {
    title,
    refresh
};

@track showSpinner = true;
@track checklists = [];

dataForRefresh;
@wire(getChecklists, {objectId: '$recordId'})
wiredChecklists(result){
    this.dataForRefresh = result;
    if(result.data){
        console.log(result.data);
        this.checklists = JSON.parse(result.data);
        this.showSpinner = false;
    } else if(result.error){
        console.log(result.error);
        this.showSpinner = false;
        //Do something with error
    }
}

updateChecklists(updatedChecklist){
    console.log(updatedChecklist);
    updateChecklistItems({checklistObject: JSON.stringify(updatedChecklist)})
        .then(result => {
            console.log(result);
            refreshApex(this.dataForRefresh);
            if(result !== 'Success'){
                let toastMessage = new ShowToastEvent({
                    title: 'Error',
                    message: result,
                    variant: 'error'
                });
                this.dispatchEvent(toastMessage);
            }
        }).catch(error => {
            console.log(error);
            refreshApex(this.dataForRefresh);
            let toastMessage = new ShowToastEvent({
                title: 'Error',
                message: error,
                variant: 'error'
            });
            this.dispatchEvent(toastMessage);
            //Do something with error
        })
}

check(e){
    let index = this.checklists.findIndex(item => item.stepId === e.currentTarget.dataset.stepId);

    if(this.checklists[index] !== null){
        let checklistGroupValue = e.detail.value;

        let percentComplete = Math.ceil((checklistGroupValue.length / this.checklists[index].options.length) * 100);

        this.checklists[index].progress = percentComplete;
        this.checklists[index].value = checklistGroupValue;

        this.updateChecklists(this.checklists[index]);
    }
}

It loads fine. Then when a user completes the entire checklist, they are unable to uncheck anything (working with the lightning-checklist-group). So when they try to uncheck something, I throw an error toast, then call refreshApex, it correctly calls my wire function, grabs the checklist info on the server (where all items are checked), assigns it to checklists then... no rerendering happens.

A change to checklists should be causing a rerendering of my html as it is bound to a template for-each, but nothing happens even though I am setting it to a new value because the value changed in the check function. How can I get a refreshApex to actually trigger a rerender when it sets a @track value?

  • Not related to your checklists track, but to the showSpinner: note that you can get calls back from the wire with neither the result.data nor result.error. IMHO you should move the clearing of showSpinner to outside and after the if/else if in wiredChecklists. – Phil W Sep 20 '19 at 14:30
  • Probably a good idea. But it works like I want if I call a method that does an imperative apex call to regather my checklist data instead of trying refreshApex, wish I could actually use the wire service though.... – Tyler Dahle Sep 20 '19 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.