Files
PasswordManager/public/search.js
RochesterX 6e820464d5 Initial
2025-11-12 10:13:24 -05:00

82 lines
2.1 KiB
JavaScript

import { postData, verifyLogin } from "./client.js";
const searchForm = document.getElementById("searchForm");
const token = window.localStorage.getItem("token");
const tableBody = document.getElementById("results");
const table = document.getElementById("table");
updateTableVisibility();
if (searchForm)searchForm.onsubmit = async e => {
e.preventDefault();
if (!verifyLogin()) return;
let resultObject = await postData("https://134.209.36.64:3000/getUsers", {
query: document.getElementById("query").value,
category: document.getElementById("category").value
}, token);
tableBody.innerHTML = "";
resultObject.matches.forEach(user => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${user.Id}</td>
<td>${formatText(user.Username)}</td>
<td>${formatText(user.FirstName)} ${formatText(user.LastName)}</td>
<td>${formatDate(user.DOB)}</td>
<td>${formatText(user.Role)}</td>
<td>${formatDateTime(user.LastLogin)}</td>
<td>${formatDate(user.CreatedAt)}</td>
`;
tableBody.appendChild(row);
});
updateTableVisibility();
};
function updateTableVisibility() {
if (!tableBody) return;
const rows = tableBody.querySelectorAll("tr");
console.log(rows);
if (rows == null) return;
if (rows.length != 0) {
table.style.display = "";
} else {
table.style.display = "none";
}
}
function formatText(text) {
if (text == null) return "—";
if (typeof(text) === "string") {
return text;
}
else {
return "Unknown format";
}
}
function formatDate(date) {
if (date == null) return "—";
if (typeof(date) === "string") {
return date.split("T")[0];
}
else {
return "Unknown format";
}
}
function formatDateTime(date) {
if (date == null) return "—";
if (typeof(date) === "string") {
return date.split("T")[0] + " at " + date.split("T")[1].split(".")[0];
}
else {
return "Unknown format";
}
}