Skip to content

Instantly share code, notes, and snippets.

@chynkm
Created June 20, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chynkm/1dee7b05c936ed0ab9b860662b70d35a to your computer and use it in GitHub Desktop.
Save chynkm/1dee7b05c936ed0ab9b860662b70d35a to your computer and use it in GitHub Desktop.
$(function() {
APP.thankYou.init();
});
var APP = APP || {};
APP.thankYou = {
init: function() {
this.ajaxToken();
this.enableSendButton();
this.sendResponseMessage();
},
ajaxToken: function() {
// append csrf token to AJAX requests
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
},
enableSendButton: function() {
$('#response_message').keyup(function() {
if($(this).val().length < 1) {
$('#response_message_send_button').addClass('disabled');
} else {
$('#response_message_send_button').removeClass('disabled');
}
});
},
sendResponseMessage: function() {
$('#response_message_send_button').click(function() {
$('#response_message').removeClass('is-invalid')
.parent()
.find('.invalid-feedback')
.html('')
.addClass('d-none');
$.post(appRoutes.responseMessage, { response_message: $('#response_message').val() })
.done(function(data) {
$('#response_message').val('');
$('#response_message_send_button').addClass('disabled');
$('#response_conversation').removeClass('d-none').append(data.html);
var lastGroupItem = $('#response_conversation').find('.list-group-item').last();
lastGroupItem.removeClass('list-group-item-secondary').addClass('list-group-item-success');
setTimeout(function() {
lastGroupItem.removeClass('list-group-item-success').addClass('list-group-item-secondary');
}, appVariables.timeout);
})
.fail(function(data) {
var data = data.responseJSON;
if (data.errors) {
for (var i in data.errors) {
$('#'+i).addClass('is-invalid')
.parent()
.find('.invalid-feedback')
.html(data.errors[i])
.removeClass('d-none');
};
};
});
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment