Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain pagination when an user role changes #3120

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions assets/javascripts/admin_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@ function setup_admin_user() {
var username = $(this).parents('tr').find('.name').text();
var role = $(this).attr('id');
role = $('label[for="' + role + '"]').text();
if (confirm("Are you sure to put " + username + " into role: " + $.trim(role) + "?")) {
$(this).parent('form').submit();
} else {
$(this).parent('form').find('input[class="default"]').first().prop('checked', 'checked');

function findDefault(form) {
return form.find('input[class="default"]').first();
}

function rollback(form) {
findDefault(form).prop('checked', 'checked');
}
});

var form = $(this).parent('form');

if (confirm("Are you sure to put " + username + " into role: " + $.trim(role) + "?")) {
var data = form.serializeArray();
var newRole = data[1].value;

$.ajax({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the rollback() function relies on the class being set to default that class needs to be updated when this AJAX call succeeds.

Copy link
Contributor Author

@ilausuch ilausuch May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is solved now. I am using removeClass and addClass from jquery, but if is necessary could be replaced by native javascript operations

type: 'POST',
url: form.attr('action'),
data: jQuery.param(data),
success: function(data){
findDefault(form).removeClass('default');
form.find('input[value="' + newRole + '"]').addClass('default');
},
error: function(err){
rollback(form);
addFlash('danger', 'An error occurred when changing the user role');
}
});
} else
rollback(form);
});
}