I am running into an issue with calling an apex method from a lightning aura component when a lightning date field is changed. Every time I update the field it seems to enter some sort of recursive process that prints out the console.log but never actually calls the controller Please see my code below
Aura Component
<aura:component controller="ContactSchedulingComponentCtl" implements="force:hasRecordId,force:hasSObjectName,flexipage:availableForRecordHome,flexipage:availableForAllPageTypes" >
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="showModal" type="Boolean" default="false"/>
<aura:attribute name="programs" type="List" />
<aura:attribute name="program" type="String" />
<aura:attribute name="programTypes" type="List" />
<aura:attribute name="programType" type="String" />
<aura:attribute name="selectDate" type="Date" />
<aura:attribute name="showLesson" type="Boolean" default="false"/>
<aura:attribute name="showCourse" type="Boolean" default="false"/>
<aura:attribute name="lesson" type="SObject" />
<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<button class="slds-button slds-button_brand" onclick="{!c.toggleModal}" >Schedule Participant</button>
<aura:if isTrue="{!v.showModal}" >
<section role="dialog" tabindex="-1" class="slds-modal slds-fade-in-open slds-modal_large" aria-labelledby="modal-heading-01" aria-modal="true">
<div class="slds-modal__container">
<lightning:buttonIcon iconName="utility:close" variant="bare" alternativeText="Close" title="Close" />
<div class="slds-modal__header">
<h1 id="modal-heading-01" class="slds-modal__title slds-hyphenate">Schedule Participant</h1>
</div>
<div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
<div class="slds-grid slds-gutters">
<div class="slds-col slds-size_1-of-4">
<lightning:select name="program" label="Program:" aura:id="program" value="{!v.program}" variant="label-inline" onchange="{!c.getProgramType}">
<option value="">choose one...</option>
<aura:iteration items="{!v.programs}" var="option">
<option text="{!option.Name}" value="{!option.Id}" />
</aura:iteration>
</lightning:select>
</div>
<div class="slds-col slds-size_1-of-4">
</div>
<div class="slds-col slds-size_1-of-4">
</div>
<div class="slds-col slds-size_1-of-4">
</div>
</div>
<br/>
<aura:if isTrue="{!v.programType == 'Lesson'}">
<h2 class="slds-card__header-title">
<a href="#" class="slds-card__header-link slds-truncate" title="Accounts">
<span>Schedule Lesson</span>
</a>
</h2>
<div class="slds-grid slds-gutters">
<div class="slds-col">
<lightning:input type="date" name="input1" label="Select a Date" placeholder="select date..." value="{!v.selectDate}" oncommit="{!c.getLesson}"/>
</div>
<div class="slds-col">
Number of Participant: {!v.lesson.Number_of_Participants__c}
</div>
<div class="slds-col">
<button class="slds-button slds-button_brand">Schedule</button>
</div>
</div>
<br/>
</aura:if>
<div class="slds-modal__footer">
<button class="slds-button slds-button_neutral" aria-label="Cancel and close" onclick="{!c.toggleModal}">Cancel</button>
<button class="slds-button slds-button_brand">Save</button>
</div>
</div>
</div>
</section>
<div class="slds-backdrop slds-backdrop_open" role="presentation">
</div>
</aura:if>
</aura:component>
JS Controller Method
getLesson : function(component, event, helper) {
var programId = component.get("v.program");
var selectDate = component.get("v.selectDate");
console.log(programId+' '+selectDate);
helper.getLesson(component, event, programId, selectDate);
},
Helper Method
getLesson : function(component, event, helper, programId, selectDate) {
console.log('get lessons '+programId+' '+selectDate);
var action1 = component.get('c.getLesson');
action1.setParams({
programId: programId,
lessonDate: selectDate
});
action1.setCallback(this, function(response) {
var state = response.getState();
var data = response.getReturnValue();
if(response.getState() == "SUCCESS"){
console.log('RESPONSE is '+response);
console.log('state is '+state);
console.log(data);
component.set('v.lesson', data);
}
});
$A.enqueueAction(action1);
},
Apex Controller
public with sharing class ContactSchedulingComponentCtl {
@AuraEnabled
public static List<pmdm__Program__c> getPrograms() {
System.debug('get programs ');
List<pmdm__Program__c> programs = [Select Name, Id, RecordType.Name, Program_Type__c From pmdm__Program__c where pmdm__Status__c = 'Active'];
System.debug('programs '+programs);
return programs;
}
@AuraEnabled
public static Lesson__c getLesson(Id programId, Date lessonDate) {
System.debug('get lessons ');
Lesson__c returnLesson = new Lesson__c();
Lesson__c currentLesson = [Select Id, Date__c, Name, Program__c, Number_of_Participants__c, Number_of_Instructors__c
from Lesson__c
Where Date__c = :lessonDate AND Program__c = :programId limit 1];
System.debug(currentLesson);
if(currentLesson != null){
returnLesson = currentLesson;
} else {
Lesson__c l = new Lesson__c();
l.Id = null;
l.Date__c = lessonDate;
l.Program__c = programId;
l.Number_of_Participants__c = 0;
returnLesson = l;
}
System.debug(returnLesson);
return returnLesson;
}
}