fix(ui): correctly populate edit modal for unlimited users
This commit is contained in:
@ -243,7 +243,6 @@
|
|||||||
<script>
|
<script>
|
||||||
$(function () {
|
$(function () {
|
||||||
|
|
||||||
//** Username validation */
|
|
||||||
const usernameRegex = /^[a-zA-Z0-9]+$/;
|
const usernameRegex = /^[a-zA-Z0-9]+$/;
|
||||||
|
|
||||||
function validateUsername(username, errorElementId) {
|
function validateUsername(username, errorElementId) {
|
||||||
@ -254,16 +253,14 @@
|
|||||||
errorElement.text("Usernames can only contain letters and numbers.");
|
errorElement.text("Usernames can only contain letters and numbers.");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
errorElement.text(""); // Clear any previous error
|
errorElement.text("");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable submit buttons by default
|
|
||||||
$("#addSubmitButton").prop("disabled", true);
|
$("#addSubmitButton").prop("disabled", true);
|
||||||
// $("#editSubmitButton").prop("disabled", true);
|
// $("#editSubmitButton").prop("disabled", true);
|
||||||
|
|
||||||
//** Add Username validation on Add User Modal */
|
|
||||||
$("#addUsername").on("input", function () {
|
$("#addUsername").on("input", function () {
|
||||||
const username = $(this).val();
|
const username = $(this).val();
|
||||||
const isValid = validateUsername(username, "addUsernameError");
|
const isValid = validateUsername(username, "addUsernameError");
|
||||||
@ -271,7 +268,6 @@
|
|||||||
$("#addSubmitButton").prop("disabled", !isValid);
|
$("#addSubmitButton").prop("disabled", !isValid);
|
||||||
});
|
});
|
||||||
|
|
||||||
//** Add Username validation on Edit User Modal */
|
|
||||||
$("#editUsername").on("input", function () {
|
$("#editUsername").on("input", function () {
|
||||||
const username = $(this).val();
|
const username = $(this).val();
|
||||||
const isValid = validateUsername(username, "editUsernameError");
|
const isValid = validateUsername(username, "editUsernameError");
|
||||||
@ -279,11 +275,9 @@
|
|||||||
$("#editSubmitButton").prop("disabled", !isValid);
|
$("#editSubmitButton").prop("disabled", !isValid);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Filter Buttons Functionality
|
|
||||||
$(".filter-button").on("click", function () {
|
$(".filter-button").on("click", function () {
|
||||||
const filter = $(this).data("filter");
|
const filter = $(this).data("filter");
|
||||||
|
|
||||||
// Deselect "Select All" checkbox when a filter is applied
|
|
||||||
$("#selectAll").prop("checked", false);
|
$("#selectAll").prop("checked", false);
|
||||||
|
|
||||||
$("#userTable tbody tr").each(function () {
|
$("#userTable tbody tr").each(function () {
|
||||||
@ -342,7 +336,6 @@
|
|||||||
confirmButtonText: "Yes, delete them!",
|
confirmButtonText: "Yes, delete them!",
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
if (result.isConfirmed) {
|
if (result.isConfirmed) {
|
||||||
//AJAX request for each user selected
|
|
||||||
Promise.all(selectedUsers.map(username => {
|
Promise.all(selectedUsers.map(username => {
|
||||||
const removeUserUrl = "{{ url_for('remove_user_api', username='USERNAME_PLACEHOLDER') }}";
|
const removeUserUrl = "{{ url_for('remove_user_api', username='USERNAME_PLACEHOLDER') }}";
|
||||||
const url = removeUserUrl.replace("USERNAME_PLACEHOLDER", encodeURIComponent(username));
|
const url = removeUserUrl.replace("USERNAME_PLACEHOLDER", encodeURIComponent(username));
|
||||||
@ -386,7 +379,6 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add User Form Submit
|
|
||||||
$("#addUserForm").on("submit", function (e) {
|
$("#addUserForm").on("submit", function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!validateUsername($("#addUsername").val(), "addUsernameError")) {
|
if (!validateUsername($("#addUsername").val(), "addUsernameError")) {
|
||||||
@ -437,21 +429,31 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Edit User Form Populate and Submit
|
|
||||||
$(document).on("click", ".edit-user", function () {
|
$(document).on("click", ".edit-user", function () {
|
||||||
const username = $(this).data("user");
|
const username = $(this).data("user");
|
||||||
const row = $(this).closest("tr");
|
const row = $(this).closest("tr");
|
||||||
const quota = row.find("td:eq(4)").text().trim();
|
const trafficUsageText = row.find("td:eq(4)").text().trim();
|
||||||
const expiry_days = row.find("td:eq(6)").text().trim();
|
const expiryDaysText = row.find("td:eq(6)").text().trim();
|
||||||
const blocked = row.find("td:eq(7) i").hasClass("text-danger");
|
const blocked = row.find("td:eq(7) i").hasClass("text-danger");
|
||||||
|
|
||||||
const quotaMatch = quota.match(/\/\s*([\d.]+)/);
|
const expiryDaysValue = (expiryDaysText.toLowerCase() === 'unlimited') ? 0 : parseInt(expiryDaysText, 10);
|
||||||
const quotaValue = quotaMatch ? parseFloat(quotaMatch[1]) : 0;
|
|
||||||
|
let trafficLimitValue = 0;
|
||||||
|
if (!trafficUsageText.toLowerCase().includes('/unlimited')) {
|
||||||
|
const parts = trafficUsageText.split('/');
|
||||||
|
if (parts.length > 1) {
|
||||||
|
const limitPart = parts[1].trim();
|
||||||
|
const match = limitPart.match(/^[\d.]+/);
|
||||||
|
if (match) {
|
||||||
|
trafficLimitValue = parseFloat(match[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$("#originalUsername").val(username);
|
$("#originalUsername").val(username);
|
||||||
$("#editUsername").val(username);
|
$("#editUsername").val(username);
|
||||||
$("#editTrafficLimit").val(quotaValue);
|
$("#editTrafficLimit").val(trafficLimitValue);
|
||||||
$("#editExpirationDays").val(expiry_days);
|
$("#editExpirationDays").val(expiryDaysValue);
|
||||||
$("#editBlocked").prop("checked", blocked);
|
$("#editBlocked").prop("checked", blocked);
|
||||||
|
|
||||||
const isValid = validateUsername(username, "editUsernameError");
|
const isValid = validateUsername(username, "editUsernameError");
|
||||||
@ -480,7 +482,6 @@
|
|||||||
data: JSON.stringify(jsonData),
|
data: JSON.stringify(jsonData),
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
if (typeof response === 'string' && response.includes("User updated successfully")) {
|
if (typeof response === 'string' && response.includes("User updated successfully")) {
|
||||||
// Hide the modal
|
|
||||||
$("#editUserModal").modal("hide");
|
$("#editUserModal").modal("hide");
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
title: "Success!",
|
title: "Success!",
|
||||||
|
|||||||
Reference in New Issue
Block a user