+ Start a Discussion
golugolu 

making javascript reusable?

Can anyone please help me with making the javascript code reusable?
There is almost same piece of code in both the functions.
Please provide the snippet
({
    sendLater : function(component, event, helper) { 
        var recId = component.get("v.recordId");
        console.log(recId);
        var smsBody = component.get("v.selectedTemplateBody");
        console.log(smsBody);
        var scheduleDateTime = component.get("v.scheduleDateTime");
        console.log(scheduleDateTime);
        $A.util.toggleClass(component.find("mySpinner"),"slds-show");
        var action = component.get("c.sendLaterButton");
        console.log(action);
        action.setParams({ 
            scheduleDateTime :   component.get("v.scheduleDateTime"),
            message          :   smsBody,
            recordId         :   component.get("v.recordId"),
            objName          :   component.get("v.sObjectName")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                
                $A.util.toggleClass(component.find("mySpinner"),"slds-hide");
                var toastEvent = $A.get("e.force:showToast"); //
                /**/
                toastEvent.setParams({
                    title: "Success!",
                    message: "The record has been created successfully ."
                    
                });
                toastEvent.fire();
                var modal = component.find("Modal");
                $A.util.addClass(modal, 'hideDiv');
                var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                dismissActionPanel.fire(); 
                
                
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                        dismissActionPanel.fire(); 
                        var toastEvent = $A.get("e.force:showToast"); // //
                        toastEvent.setParams({
                            title: "Failed",
                            message: "The record was not saved"
                            
                        });
                        toastEvent.fire();
                    }
                } 
            }     
        });
        $A.enqueueAction(action);
    },
    
    sendNow : function(component, event, helper) { 
        var recId = component.get("v.recordId");
        console.log(recId);
        var smsBody = component.get("v.selectedTemplateBody");
        console.log(smsBody);
        var scheduleDateTime = component.get("v.scheduleDateTime");
        console.log(scheduleDateTime);
        $A.util.toggleClass(component.find("mySpinner"),"slds-show");
        var action = component.get("c.sendNowButton");
        console.log(action);
        action.setParams({ 
            scheduleDateTime :   component.get("v.scheduleDateTime"),
            message          :   smsBody,
            recordId         :   component.get("v.recordId"),
            objName          :   component.get("v.sObjectName")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                
                $A.util.toggleClass(component.find("mySpinner"),"slds-hide");
                var toastEvent = $A.get("e.force:showToast"); //
                /**/
                toastEvent.setParams({
                    title: "Success!",
                    message: "The record has been created successfully ."
                    
                });
                toastEvent.fire();
                var modal = component.find("Modal");
                $A.util.addClass(modal, 'hideDiv');
                var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                dismissActionPanel.fire(); 
                
                
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                        dismissActionPanel.fire(); 
                        var toastEvent = $A.get("e.force:showToast");
                        toastEvent.setParams({
                            title: "Failed",
                            message: "The record was not saved."
                            
                        });
                        toastEvent.fire();
                    }
                } 
            }     
        });
        $A.enqueueAction(action);
    }
})

 
Best Answer chosen by golu
Ravi Dutt SharmaRavi Dutt Sharma
Hi,

You can test using below code:
 
({
    sendLater : function(component, event, helper) { 
        helper.sendNowOrLater(component, event, helper, "sendLater");
    },
    
    sendNow : function(component, event, helper) { 
        helper.sendNowOrLater(component, event, helper, "sendNow");
    },

    // The fourth param caller can have either of the 2 values -  sendNow or sendLater
    sendNowOrLater : function(component, event, helper, caller){
        var recId = component.get("v.recordId");
        console.log(recId);
        var smsBody = component.get("v.selectedTemplateBody");
        console.log(smsBody);
        var scheduleDateTime = component.get("v.scheduleDateTime");
        console.log(scheduleDateTime);
        $A.util.toggleClass(component.find("mySpinner"),"slds-show");
        var action;
        if(caller == 'sendNow'){
            action = component.get("c.sendNowButton");
        }else{
            action = component.get("c.sendLaterButton");
        }
        console.log(action);
        action.setParams({ 
            scheduleDateTime :   component.get("v.scheduleDateTime"),
            message          :   smsBody,
            recordId         :   component.get("v.recordId"),
            objName          :   component.get("v.sObjectName")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                
                $A.util.toggleClass(component.find("mySpinner"),"slds-hide");
                var toastEvent = $A.get("e.force:showToast"); //
                /**/
                toastEvent.setParams({
                    title: "Success!",
                    message: "The record has been created successfully ."
                    
                });
                toastEvent.fire();
                var modal = component.find("Modal");
                $A.util.addClass(modal, 'hideDiv');
                var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                dismissActionPanel.fire(); 
                
                
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                        dismissActionPanel.fire(); 
                        var toastEvent = $A.get("e.force:showToast"); // //
                        toastEvent.setParams({
                            title: "Failed",
                            message: "The record was not saved"
                            
                        });
                        toastEvent.fire();
                    }
                } 
            }     
        });
        $A.enqueueAction(action);
    }
})

 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi,

You can test using below code:
 
({
    sendLater : function(component, event, helper) { 
        helper.sendNowOrLater(component, event, helper, "sendLater");
    },
    
    sendNow : function(component, event, helper) { 
        helper.sendNowOrLater(component, event, helper, "sendNow");
    },

    // The fourth param caller can have either of the 2 values -  sendNow or sendLater
    sendNowOrLater : function(component, event, helper, caller){
        var recId = component.get("v.recordId");
        console.log(recId);
        var smsBody = component.get("v.selectedTemplateBody");
        console.log(smsBody);
        var scheduleDateTime = component.get("v.scheduleDateTime");
        console.log(scheduleDateTime);
        $A.util.toggleClass(component.find("mySpinner"),"slds-show");
        var action;
        if(caller == 'sendNow'){
            action = component.get("c.sendNowButton");
        }else{
            action = component.get("c.sendLaterButton");
        }
        console.log(action);
        action.setParams({ 
            scheduleDateTime :   component.get("v.scheduleDateTime"),
            message          :   smsBody,
            recordId         :   component.get("v.recordId"),
            objName          :   component.get("v.sObjectName")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                
                $A.util.toggleClass(component.find("mySpinner"),"slds-hide");
                var toastEvent = $A.get("e.force:showToast"); //
                /**/
                toastEvent.setParams({
                    title: "Success!",
                    message: "The record has been created successfully ."
                    
                });
                toastEvent.fire();
                var modal = component.find("Modal");
                $A.util.addClass(modal, 'hideDiv');
                var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                dismissActionPanel.fire(); 
                
                
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                        dismissActionPanel.fire(); 
                        var toastEvent = $A.get("e.force:showToast"); // //
                        toastEvent.setParams({
                            title: "Failed",
                            message: "The record was not saved"
                            
                        });
                        toastEvent.fire();
                    }
                } 
            }     
        });
        $A.enqueueAction(action);
    }
})

 
This was selected as the best answer
golugolu
works perfectly fine