Cancel email send from a modal

209 views
Skip to first unread message

Olivier Droz

unread,
Apr 7, 2018, 5:52:10 PM4/7/18
to InboxSDK
Hi, 
I'm trying to warn the user that if there isn't exactly 1 recipient, my service would have a unwanted behaviour. 
I can intercept the send with the presend event, I can display the modal in which I have 2 buttons: Send anyway and Edit. Edit should cancel the email send and return to to ConposeView. 
However, i'm seeing that the email is sent anyways even if I click Edit. 
I don't want to cancel the send before opening the modal because if my server is down, I want the user to be able the send the email anyways (without having to uninstall my extension)
Can you help me? 
Here is my code: 

function showRecipientModal(view, event, composeView, draftId){
    const el = document.createElement('div');
    el.style.width = '250px';
    el.style.height = '100px';
    el.innerHTML = 'Blah blah...';
    const modal= view.showModalView({
        el,
        title: 'blah',
        buttons: [
                {
                    text: "Send Anyway",
                    title: "Send Anyway", 
                    onClick: function(click){
                        console.log("OK  button clicked");
                        sendEmail(composeView, event, draftId);
                        modal.close();
                    },
                    type: 'PRIMARY_ACTION',
                    color: 'green'   
                },
                {
                    text: "Edit",
                    title: "Edit", 
                    onClick: function(click){
//Here, I want to return to the ComposeView and edit my email
                        console.log("Edit  button clicked");
                        modal.close();
                        event.cancel();
                    },
                    type: 'SECONDARY',
                    color: 'grey'   
                }
                ]
            });
}
function sendEmail(composeView, event, draftId){
    var params = {
        tracking: computeTrackingChoice(),
        threadId: draftId
    };

   // Call My API to send the email using the gmail API
    .done(function(data) {
      console.log('sendEmail API call success, data: ', data);
      
        event.cancel();
    })
    .fail(function(e) {
      console.log('failed to sendEmail to API', e);
        //Server is down/ service is unavailable -> Send the email anyway!
    });

    composeView.close();
}


InboxSDK.load(2, 'sdk_Saleswings_c25da028c8').then(function(sdk){
console.log('SDK loaded');
    checkIfLinkedWithGmail();
    console.log("gmaiAddress", gmailAddress);
  // the SDK has been loaded, now do something with it!  
  sdk.Compose.registerComposeViewHandler(function(composeView){

     composeView.addButton({
          title: 'SalesWings tracking',
          iconUrl: chrome.extension.getURL(SMALL_WING),
          iconClass: 'sw-tracking-button',
          hasDropdown: true,
          onClick: function(event){
            event.dropdown.el.innerHTML = showPopup(gmailAddress);
            if(readyToSend){addCheckBoxListeners(event.dropdown.el)};
          }
      });
      
    var draftId = '';
    composeView.getDraftID().then(function(id){
        console.log('DraftID: ',id); 
        draftId = id;
        
    });
    composeView.on('presending', event =>{
        console.log('Cancel sending message '+draftId);
        console.log("event", event);
        var recipients = composeView.getToRecipients();
        if(recipients.length != 1){
            showRecipientModal(sdk.Widgets, event, composeView, draftId);
        }else{
            sendEmail(composeView, event, draftId);
        }

    });  

  });
    
});



Many thanks for your help!

Olivier

Chris Cowan

unread,
Apr 9, 2018, 2:49:22 PM4/9/18
to InboxSDK
You need to cancel the event while the event is still being processed. You can't make a network request and wait for its result and then cancel the send event, because the send will have already happened by then. You should cancel the send immediately, add a status bar or a butter bar with a message explaining that the message is sending via your service, and if that request is successful, close the compose. If that request fails, then show that to the user, and show the user a "send with gmail" button that calls composeView.send() but first turns on a flag that causes your presending event handler to not process the event.

At the first place you call sendEmail, there is no event variable.

During your presending event, the draftId variable may still be set to '' if the getDraftID() promise hasn't resolved yet. You should wait on that promise to resolve before trying to process the draft id at all.

Olivier Droz

unread,
Apr 9, 2018, 2:52:42 PM4/9/18
to Chris Cowan, InboxSDK
Thanks Chris, 
That makes sense. 
I'll implement what you suggest. 

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/inboxsdk/082d9cda-7628-4dc4-b984-638723e2e2f9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
>> Going Out-of-Office over holidays? Check our tips on writing a super OOO message (share if you like!)

http://www.saleswingsapp.com/how-to/3-useful-out-of-office-message-sales-tips/


 

SalesWings has been selected as a TOP SALES TOOL by smartsellingtools.com


Olivier Droz
CTO & Co-Founder
Europe    +41 61 539 18 91
USA        +1 216 600 0927


SD Technologies SA
Route de la Feuillère 29
1010 Lausanne
Switzerland


Reply all
Reply to author
Forward
0 new messages