edit user

This commit is contained in:
2024-08-26 13:37:05 +02:00
parent 591525e97f
commit bced6d7036
4 changed files with 113 additions and 4 deletions

View File

@ -79,3 +79,53 @@ export function showUser(api, userID) {
})
}
export function saveUser(api, event) {
const token = localStorage.getItem("token");
let form = document.getElementById("user_form");
let id = window.location.pathname.split("/").pop();
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
form.classList.add("was-validated");
return;
}
form.classList.add("was-validated");
if (document.getElementById("password").value !== document.getElementById("verify_password").value) {
Swal.fire("Password do not match!");
return
}
let payload = {
id: parseInt(id),
first_name: document.getElementById("first_name").value,
last_name: document.getElementById("last_name").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value,
}
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
body: JSON.stringify(payload)
}
fetch(api + `/api/admin/all-users/edit/${id}`, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
if (data.ok === false) {
Swal.fire("Error" + data.message)
} else {
location.href = "/admin/all-users"
}
});
}