Problem with switch case

apexiflightning-web-componentsswitch-case

I share with you my code which appears to work well the problem I have is that:

If for example, I choose the status as closed and I choose dates, I can't save and that's what I want, but when I try to change the status value and I don't change the dates, it doesn't ignore their values. (I'm just talking about this case apart when it comes to normal processing, I choose the value of the status and the dates, I retrieve the values, I'm just talking about the case where for example I chose first a value for the status and the dates too but even before registering I change the value of the status without changing the value of the dates, in this case, I do not recover their values)

@track endDate ;    
@track startDate = new Date().toISOString().substring(0, 10);

    handleChange(event) {
        switch( event.target.fieldName) {
            case 'Status__c':{
                this.info = event.target.value;
                if (this.info == 'Closed') {
                    let desc = this.template.querySelector('[data-id="description"]').value;
                    this.disableButton2 = true;
                }
                else {
                    this.info = event.target.value;
                    this.disableButton2 = false;
                    this.required = true;
                }

                break;
            }
            case 'Start_Date__c': {
                 this.startDate = event.target.value;
                
              
                break;
            }
            case 'EndDate__c': {
                this.endDate = event.target.value;
              
            }

            if( (this.endDate < this.startDate ) || (this.info == 'Closed') )
            {
                
                console.log(this.info);
                this.canSubmit = false;
            }
            else{
                this.canSubmit = true;
                        }

           
        }
    } 

Best Answer

The if statement shouldn't be within the switch statement:

switch (event.target.fieldName) {
  case "Status__c": {
    this.info = event.target.value;
    if (this.info === "Closed") {
      let desc = this.template.querySelector(
        '[data-id="description"]'
      ).value;
      this.disableButton2 = true;
    } else {
      this.info = event.target.value;
      this.disableButton2 = false;
      this.required = true;
    }
    break;
  }
  case "Start_Date__c": {
    this.startDate = event.target.value;
    break;
  }
  case "EndDate__c": {
    this.endDate = event.target.value;
    break;
  }
}
if (this.endDate < this.startDate || this.info === "Closed") {
  this.canSubmit = false;
} else {
  this.canSubmit = true;
}
Related Topic