jQuery(function($){
$('button[name="sf_change_frequency_save"]').click(function(e){
e.preventDefault();
var $saveButton=$(this);
if($saveButton.prop('disabled') ){
return;
}
var originalText=$saveButton.text();
$saveButton
.prop('disabled', true)
.addClass('disabled')
.text('Saving...');
$.ajax({
url: sf_change_frequency.root + '/update',
method: 'POST',
data: {
frequency: $('#sf_subscription_frequency option:selected').val(),
subscription_id: $('#sf_subscription_frequency').attr('data-subscription_id')
},
beforeSend: function(xhr){
xhr.setRequestHeader('X-WP-Nonce', sf_change_frequency.nonce)
},
complete: function(){
$saveButton
.prop('disabled', false)
.removeClass('disabled')
.text(originalText);
},
}).done(function(response){
$('#sf_change_frequency_subscription_row .view span').text(response.data.frequency_text);
$("#sf_change_frequency_subscription_row .view").show();
$("#sf_change_frequency_subscription_row .edit").hide();
try {
var noticeType=(response&&response.error) ? 'error':'success';
window.sessionStorage.setItem('ssd_persisted_notice',
JSON.stringify({
message: $('<div />').html(response.data.html).text().trim(),
type: noticeType,
ts: Date.now(),
})
);
} catch(e){
}
if(1!=sf_change_frequency.ssd_trigger_shipping){
location.reload();
}else{
ssd_trigger_shipping_modal($('#sf_subscription_frequency').attr('data-subscription_id'));
}}).fail(function(){
try {
window.sessionStorage.setItem('ssd_persisted_notice',
JSON.stringify({
message: 'An error occurred while saving. Please try again.',
type: 'error',
ts: Date.now(),
})
);
} catch(e){
}
location.reload();
});
});
$("#sf_change_frequency_subscription_row a#sf_change_frequency_edit_button").click(function(e){
e.preventDefault();
$("#sf_change_frequency_subscription_row .edit").show();
$("#sf_change_frequency_subscription_row .view").hide();
});
$("#sf_change_frequency_subscription_row a#sf_change_frequency_cancel").click(function(e){
e.preventDefault();
$("#sf_change_frequency_subscription_row .view").show();
$("#sf_change_frequency_subscription_row .edit").hide();
});
});