Dec 19, 2024
0
Level 1
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
document.querySelectorAll(".updateUser").forEach(function (button) { button.addEventListener("click", function (e) { e.preventDefault();
const userId = this.getAttribute("data-user-id");
const agencyId = this.getAttribute("data-agency-id");
const firstName = this.getAttribute("data-first-name");
const lastName = this.getAttribute("data-last-name");
const userName = this.getAttribute("data-user-name");
const status = this.getAttribute("data-status");
console.log("Initial Values Extracted from Data Attributes:", {
userId, agencyId, firstName, lastName, userName, status
});
Swal.fire({
title: "Edit User",
html: `
<form id="updateForm" novalidate>
<input type="hidden" id="user-id" value="${userId}">
<input type="hidden" id="agency-id" value="${agencyId}">
<div class="form-group">
<label for="first-name">First Name</label>
<input type="text" class="form-control" id="first-name" value="${firstName}">
</div>
<div class="form-group">
<label for="last-name">Last Name</label>
<input type="text" class="form-control" id="last-name" value="${lastName}">
</div>
<div class="form-group">
<label for="user-name">Username</label>
<input type="text" class="form-control" id="user-name" value="${userName}">
</div>
<div class="form-group">
<label for="status">Status</label>
<select id="status" class="form-control">
<option value="ACTIVE" ${status === "ACTIVE" ? "selected" : ""}>Active</option>
<option value="INACTIVE" ${status === "INACTIVE" ? "selected" : ""}>Inactive</option>
<option value="NEW" ${status === "NEW" ? "selected" : ""}>New</option>
</select>
</div>
</form>
`,
showCancelButton: true,
confirmButtonText: "Save Changes",
preConfirm: () => {
const updatedData = {
user_id: document.getElementById("user-id").value,
agency_id: document.getElementById("agency-id").value,
first_name: document.getElementById("first-name").value,
last_name: document.getElementById("last-name").value,
user_name: document.getElementById("user-name").value,
status: document.getElementById("status").value,
};
console.log("Values Submitted in Popup:", updatedData);
if (!updatedData.user_id || !updatedData.agency_id) {
Swal.showValidationMessage("User ID and Agency ID are required.");
}
return updatedData;
},
}).then((result) => {
if (result.isConfirmed) {
const updatedData = result.value;
const inputData = {
ajax_source: AGENCY_AJAX_SOURCE,
from_ajax: true,
form_source: "updateUser",
userId: updatedData.user_id,
agencyId: updatedData.agency_id,
firstName: updatedData.first_name,
lastName: updatedData.last_name,
userName: updatedData.user_name,
userStatus: updatedData.status,
};
console.log("Final Data for AJAX Request:", inputData);
$.ajax({
url: inputData.ajax_source,
type: "POST",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify(inputData),
success: function (responseData) {
console.log("AJAX Success Response:", responseData);
ajaxSuccessResponse(responseData, "", inputData);
},
error: function (error) {
console.log("AJAX Error Response:", error);
ajaxFailedResponse(error, "", inputData);
},
});
}
});
}); });
Please or to participate in this conversation.