From 40a80f7bf3c6743ce97bb2a5e19f707870565bd5 Mon Sep 17 00:00:00 2001 From: Iam54r1n4 Date: Mon, 3 Feb 2025 00:52:17 +0000 Subject: [PATCH] Change JS in frontend to use API --- core/scripts/webpanel/templates/users.html | 69 +++++++++++++++------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/core/scripts/webpanel/templates/users.html b/core/scripts/webpanel/templates/users.html index 5d2e8e8..2cde061 100644 --- a/core/scripts/webpanel/templates/users.html +++ b/core/scripts/webpanel/templates/users.html @@ -67,7 +67,7 @@ {{ user.expiry_date }} {{ user.expiry_days }} - {% if user.enable == "enabled" %} + {% if user.enable %} {% else %} @@ -211,22 +211,31 @@ // Add User Form Submit $("#addUserForm").on("submit", function (e) { e.preventDefault(); - const formData = $(this).serialize(); + + const formData = $(this).serializeArray(); + const jsonData = {}; + formData.forEach(field => { + jsonData[field.name] = field.value; + }); + $.ajax({ - url: "{{ url_for('add_user') }}", + url: " {{ url_for('add_user_api') }} ", method: "POST", - data: formData, + contentType: "application/json", + data: JSON.stringify(jsonData), success: function (response) { - if (response.message) { + debugger; + if (response.detail) { Swal.fire({ title: "Success!", - text: response.message, + text: response.detail, icon: "success", confirmButtonText: "OK", }).then(() => { location.reload(); }); } else { + debugger; Swal.fire({ title: "Error!", text: response.error || "Failed to add user", @@ -269,11 +278,21 @@ $("#editUserForm").on("submit", function (e) { e.preventDefault(); - const formData = $(this).serialize(); + + const formData = $(this).serializeArray(); + const jsonData = {}; + formData.forEach(field => { + jsonData[field.name] = field.value; + }); + + const editUserUrl = "{{ url_for('edit_user_api', username='USERNAME_PLACEHOLDER') }}"; + const url = editUserUrl.replace("USERNAME_PLACEHOLDER", encodeURIComponent($("#originalUsername").val())); + $.ajax({ - url: "{{ url_for('edit_user') }}", - method: "POST", - data: formData, + url: url, + method: "PATCH", + contentType: "application/json", + data: JSON.stringify(jsonData), success: function (response) { if (typeof response === 'string' && response.includes("User updated successfully")) { const username = $("#originalUsername").val(); @@ -297,7 +316,7 @@ confirmButtonText: "OK", }); } - else if (response && response.message) { + else if (response && response.detail) { const username = $("#originalUsername").val(); const row = $(`td[data-username='${username}']`).closest("tr"); row.find("td:eq(1)").text($("#editUsername").val()); @@ -316,7 +335,7 @@ // Show a success message Swal.fire({ title: "Success!", - text: response.message, + text: response.detail, icon: "success", confirmButtonText: "OK", }); @@ -362,15 +381,18 @@ confirmButtonText: "Yes, reset it!", }).then((result) => { if (result.isConfirmed) { + const resetUserUrl = "{{ url_for('reset_user_api', username='USERNAME_PLACEHOLDER') }}"; + const url = resetUserUrl.replace("USERNAME_PLACEHOLDER", encodeURIComponent(username)); $.ajax({ - url: "{{ url_for('reset_user') }}", - method: "POST", - data: { username: username }, + url: url, + method: "GET", + contentType: "application/json", + data: JSON.stringify({ username: username }), success: function (response) { - if (response.message) { + if (response.detail) { Swal.fire({ title: "Success!", - text: response.message, + text: response.detail, icon: "success", confirmButtonText: "OK", }).then(() => { @@ -412,15 +434,18 @@ confirmButtonText: "Yes, delete it!", }).then((result) => { if (result.isConfirmed) { + const removeUserUrl = "{{ url_for('remove_user_api', username='USERNAME_PLACEHOLDER') }}"; + const url = removeUserUrl.replace("USERNAME_PLACEHOLDER", encodeURIComponent(username)); $.ajax({ - url: "{{ url_for('delete_user') }}", - method: "POST", - data: { username: username }, + url: url, + method: "DELETE", + contentType: "application/json", + data: JSON.stringify({ username: username }), success: function (response) { - if (response.message) { + if (response.detail) { Swal.fire({ title: "Success!", - text: response.message, + text: response.detail, icon: "success", confirmButtonText: "OK", }).then(() => {