New to gmail add ons and have a question which I am sure is pretty basic: how do I change the UI of my add on from a callback function?
More specifically, I have a button which renders in a section on my card:
section.addWidget(
CardService.newTextButton()
.setText('UNSUBSCRIBE')
.setOnClickAction(
CardService.newAction()
.setFunctionName('unsubscribe')
.setParameters({email: sender.email})));
As you can see, I call a function called "unsubscribe" when it is clicked which in turn calls a 3rd party API endpoint. Upon the 200 response from that endpoint, I want to hide the "unsubscribe" button and show a message to the user stating that the unsubscribe request went through successfully. Currently, I don't know how to access the card sections so I am showing a notification in stead:
function unsubscribe(e){
var parameters = e.parameters;
var email = parameters['email'];
var data = {
"code":"XXXX",
"email": email
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch('https://someurl.com', options);
if(response.getResponseCode() == 200){
return CardService.newActionResponseBuilder()
.setNotification(CardService.newNotification().setType(CardService.NotificationType.INFO)
.setText(email + " has been unsubscribed from future notices."))
.build();
}
return CardService.newActionResponseBuilder()
.setNotification(CardService.newNotification()
.setType(CardService.NotificationType.WARNING)
.setText("Something went wrong"))
.build();
}
It would be much better to hide the UNSUBSCRIBE button on success instead and display a success message in line. How would I do this?
Thanks in advance.
As of now, we cannot dynamically update sections of the card. Hence, you cannot directly hide the button and display some message. But as a workaround, you can redraw the whole card(which won't include the button and will include the message) using Navigation class and updateCard method.
If you havn't used navigation class yet, this link might be useful.