2024-08-23 12:26:12 +00:00
|
|
|
export function showUsers(api) {
|
|
|
|
const tbody = document.getElementById("user-table").getElementsByTagName("tbody")[0];
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
|
|
|
|
const requestOptions = {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': `application/json`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch(api + "/api/admin/all-users", requestOptions)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(function (data) {
|
|
|
|
if (data) {
|
|
|
|
data.forEach(i => {
|
|
|
|
let newRow = tbody.insertRow();
|
|
|
|
let newCell = newRow.insertCell();
|
|
|
|
|
|
|
|
newCell.innerHTML = `<a href="/admin/all-users/${i.id}">${i.last_name}, ${i.first_name}</a>`;
|
|
|
|
|
|
|
|
newCell = newRow.insertCell();
|
|
|
|
let item = document.createTextNode(i.email);
|
|
|
|
newCell.appendChild(item);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let newRow = tbody.insertRow();
|
|
|
|
let newCell = newRow.insertCell();
|
|
|
|
newCell.setAttribute("colspan", "2");
|
|
|
|
newCell.innerHTML = "no data available";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-08-23 19:07:13 +00:00
|
|
|
|
2024-08-23 19:26:06 +00:00
|
|
|
export function showUser(api, userID) {
|
2024-08-23 19:07:13 +00:00
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
let id = window.location.pathname.split("/").pop();
|
2024-08-23 19:26:06 +00:00
|
|
|
let delBtn = document.getElementById("deleteBtn");
|
2024-08-23 19:07:13 +00:00
|
|
|
|
2024-08-23 19:26:06 +00:00
|
|
|
if (id === "0") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userID !== parseInt(id, 10)) {
|
|
|
|
delBtn.classList.remove("d-none")
|
|
|
|
}
|
2024-08-23 19:07:13 +00:00
|
|
|
const requestOptions = {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': `application/json`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch(api + `/api/admin/all-users/${id}`, requestOptions)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(function (data) {
|
|
|
|
console.log(data);
|
2024-08-23 19:26:06 +00:00
|
|
|
document.getElementById("first_name").value = data.first_name;
|
|
|
|
document.getElementById("last_name").value = data.last_name;
|
|
|
|
document.getElementById("email").value = data.email;
|
|
|
|
});
|
|
|
|
|
|
|
|
delBtn.addEventListener("click", function () {
|
|
|
|
Swal.fire({
|
|
|
|
title: "Are you sure?",
|
|
|
|
text: "You won't be able to undo this!",
|
|
|
|
icon: "warning",
|
|
|
|
showCancelButton: true,
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
cancelButtonColor: "#d33",
|
|
|
|
confirmButtonText: 'Delete User',
|
|
|
|
}).then((result) => {
|
|
|
|
console.log("would delete user id", id);
|
2024-08-23 19:07:13 +00:00
|
|
|
});
|
2024-08-23 19:26:06 +00:00
|
|
|
})
|
2024-08-23 19:07:13 +00:00
|
|
|
|
|
|
|
}
|
2024-08-26 11:37:05 +00:00
|
|
|
|
|
|
|
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);
|
2024-08-26 12:10:18 +00:00
|
|
|
if (!data.ok) {
|
2024-08-26 11:37:05 +00:00
|
|
|
Swal.fire("Error" + data.message)
|
|
|
|
} else {
|
|
|
|
location.href = "/admin/all-users"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2024-08-26 12:10:18 +00:00
|
|
|
|
|
|
|
export function deleteUser(api) {
|
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
let id = window.location.pathname.split("/").pop();
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
title: "Are you sure?",
|
|
|
|
text: "You won't be able to undo this!",
|
|
|
|
icon: "warning",
|
|
|
|
showCancelButton: true,
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
cancelButtonColor: "#d33",
|
|
|
|
confirmButtonText: "Delete user"
|
|
|
|
}).then((result) => {
|
|
|
|
if (result.isConfirmed) {
|
|
|
|
const requestOptions = {
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': `application/json`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch(api + `/api/admin/all-users/delete/${id}`, requestOptions)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(function (data) {
|
|
|
|
console.log(data);
|
|
|
|
if (!data.ok) {
|
|
|
|
Swal.fire("Error" + data.message)
|
|
|
|
} else {
|
|
|
|
location.href = "/admin/all-users"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|