automatic fetch of users in overview_users.js
This commit is contained in:
@@ -2,15 +2,36 @@ let codeGetDepartments = '';
|
|||||||
let codeGetUsers = '';
|
let codeGetUsers = '';
|
||||||
let codeDeleteUser = '';
|
let codeDeleteUser = '';
|
||||||
let codeOk = '';
|
let codeOk = '';
|
||||||
|
let fetchInterval = null; // Store interval ID
|
||||||
|
const userElementsMap = new Map(); // Track user elements by ID
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
const backButton = document.getElementById('backButton');
|
const backButton = document.getElementById('backButton');
|
||||||
backButton.addEventListener('click', async () => {
|
backButton.addEventListener('click', async () => {
|
||||||
|
await stopFetchingUsers(); // Stop interval before leaving
|
||||||
await window.networkAPI.closeUcSocket();
|
await window.networkAPI.closeUcSocket();
|
||||||
fadeOut('main_menu');
|
fadeOut('main_menu');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await startFetchingUsers(); // Start fetching users every 5 seconds
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function startFetchingUsers() {
|
||||||
|
await fetchData(); // Initial fetch
|
||||||
|
|
||||||
|
// Set interval to fetch users every 5 seconds
|
||||||
|
fetchInterval = setInterval(async () => {
|
||||||
|
await fetchData();
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function stopFetchingUsers() {
|
||||||
|
if (fetchInterval) {
|
||||||
|
clearInterval(fetchInterval); // Stop interval when leaving the page
|
||||||
|
fetchInterval = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchData() {
|
async function fetchData() {
|
||||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||||
if (!operationCodes) {
|
if (!operationCodes) {
|
||||||
@@ -23,13 +44,11 @@ async function fetchData() {
|
|||||||
codeDeleteUser = operationCodes.DELETE_USER;
|
codeDeleteUser = operationCodes.DELETE_USER;
|
||||||
codeOk = operationCodes.OK;
|
codeOk = operationCodes.OK;
|
||||||
|
|
||||||
// Open the TCP socket right after fetching the operation codes
|
|
||||||
if (!await window.networkAPI.openUcSocket()) {
|
if (!await window.networkAPI.openUcSocket()) {
|
||||||
alert('Failed to open socket. Internal error of the application.');
|
alert('Failed to open socket. Internal error of the application.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch departments
|
|
||||||
const departmentsResponse = await fetchDepartments();
|
const departmentsResponse = await fetchDepartments();
|
||||||
if (!departmentsResponse || departmentsResponse.operationCode !== codeOk) {
|
if (!departmentsResponse || departmentsResponse.operationCode !== codeOk) {
|
||||||
alert('Failed to fetch departments. Redirecting to main menu...');
|
alert('Failed to fetch departments. Redirecting to main menu...');
|
||||||
@@ -39,7 +58,6 @@ async function fetchData() {
|
|||||||
|
|
||||||
const departments = departmentsResponse.metaInfo.departments;
|
const departments = departmentsResponse.metaInfo.departments;
|
||||||
|
|
||||||
// Fetch users
|
|
||||||
const usersResponse = await fetchUsers();
|
const usersResponse = await fetchUsers();
|
||||||
if (!usersResponse || usersResponse.operationCode !== codeOk) {
|
if (!usersResponse || usersResponse.operationCode !== codeOk) {
|
||||||
alert('Failed to fetch users. Redirecting to main menu...');
|
alert('Failed to fetch users. Redirecting to main menu...');
|
||||||
@@ -48,72 +66,83 @@ async function fetchData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const users = usersResponse.metaInfo.users;
|
const users = usersResponse.metaInfo.users;
|
||||||
|
updateUsersList(users, departments);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateUsersList(users, departments) {
|
||||||
const usersContainer = document.getElementById('users-container');
|
const usersContainer = document.getElementById('users-container');
|
||||||
usersContainer.innerHTML = ''; // Clear the container
|
|
||||||
|
|
||||||
// Filter out users in the CEO department
|
// Filter out users in the CEO department
|
||||||
const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
|
const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo');
|
||||||
const filteredUsers = users.filter(user => user.departmentId !== ceoDepartment.id);
|
const filteredUsers = users.filter(user => user.departmentId !== ceoDepartment.id);
|
||||||
|
|
||||||
// Check if there are no users after filtering
|
const existingUserIds = new Set(filteredUsers.map(user => user.id));
|
||||||
if (!filteredUsers.length) {
|
|
||||||
const noUsersMessage = document.createElement('p');
|
|
||||||
noUsersMessage.textContent = 'No users exist.';
|
|
||||||
noUsersMessage.classList.add('no-users-message'); // Add a class for styling
|
|
||||||
usersContainer.appendChild(noUsersMessage);
|
|
||||||
fadeIn(); // Fade in the container when the message is loaded
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render users if there are any
|
// Remove users that no longer exist
|
||||||
|
userElementsMap.forEach((element, userId) => {
|
||||||
|
if (!existingUserIds.has(userId)) {
|
||||||
|
element.remove();
|
||||||
|
userElementsMap.delete(userId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update or add users
|
||||||
filteredUsers.forEach(user => {
|
filteredUsers.forEach(user => {
|
||||||
|
if (!userElementsMap.has(user.id)) {
|
||||||
|
// Create new user element
|
||||||
const userDiv = document.createElement('div');
|
const userDiv = document.createElement('div');
|
||||||
userDiv.classList.add('user-card');
|
userDiv.classList.add('user-card');
|
||||||
|
|
||||||
// Create user info div
|
|
||||||
const userInfoDiv = document.createElement('div');
|
const userInfoDiv = document.createElement('div');
|
||||||
userInfoDiv.classList.add('user-info');
|
userInfoDiv.classList.add('user-info');
|
||||||
|
|
||||||
// Create user name paragraph
|
|
||||||
const nameP = document.createElement('p');
|
const nameP = document.createElement('p');
|
||||||
nameP.classList.add('user-name');
|
nameP.classList.add('user-name');
|
||||||
nameP.textContent = user.name;
|
nameP.textContent = user.name;
|
||||||
|
|
||||||
console.log(user)
|
console.log(user);
|
||||||
|
console.log(departments);
|
||||||
|
|
||||||
console.log(departments)
|
|
||||||
|
|
||||||
// Find and display user's department name
|
|
||||||
const department = departments.find(dept => dept.id === user.departmentId);
|
const department = departments.find(dept => dept.id === user.departmentId);
|
||||||
const departmentName = department ? department.name : 'Unknown';
|
const departmentName = department ? department.name : 'Unknown';
|
||||||
|
|
||||||
// Create user department paragraph
|
|
||||||
const departmentP = document.createElement('p');
|
const departmentP = document.createElement('p');
|
||||||
departmentP.classList.add('user-department');
|
departmentP.classList.add('user-department');
|
||||||
departmentP.textContent = `Department: ${departmentName}`;
|
departmentP.textContent = `Department: ${departmentName}`;
|
||||||
|
|
||||||
// Append name and department to user info div
|
|
||||||
userInfoDiv.appendChild(nameP);
|
userInfoDiv.appendChild(nameP);
|
||||||
userInfoDiv.appendChild(departmentP);
|
userInfoDiv.appendChild(departmentP);
|
||||||
|
|
||||||
// Create delete button
|
|
||||||
const deleteButton = document.createElement('button');
|
const deleteButton = document.createElement('button');
|
||||||
deleteButton.textContent = 'Delete';
|
deleteButton.textContent = 'Delete';
|
||||||
deleteButton.classList.add('delete-button');
|
deleteButton.classList.add('delete-button');
|
||||||
deleteButton.onclick = () => deleteUser(user.id);
|
deleteButton.onclick = () => deleteUser(user.id);
|
||||||
|
|
||||||
// Append user info div and delete button to user card
|
|
||||||
userDiv.appendChild(userInfoDiv);
|
userDiv.appendChild(userInfoDiv);
|
||||||
userDiv.appendChild(deleteButton);
|
userDiv.appendChild(deleteButton);
|
||||||
|
|
||||||
// Append user card to the container
|
|
||||||
usersContainer.appendChild(userDiv);
|
usersContainer.appendChild(userDiv);
|
||||||
|
|
||||||
|
userElementsMap.set(user.id, userDiv); // Store reference
|
||||||
|
} else {
|
||||||
|
// Update existing user info if necessary
|
||||||
|
const existingDiv = userElementsMap.get(user.id);
|
||||||
|
const nameElement = existingDiv.querySelector('.user-name');
|
||||||
|
const departmentElement = existingDiv.querySelector('.user-department');
|
||||||
|
|
||||||
|
if (nameElement.textContent !== user.name) {
|
||||||
|
nameElement.textContent = user.name;
|
||||||
|
}
|
||||||
|
const department = departments.find(dept => dept.id === user.departmentId);
|
||||||
|
const departmentName = department ? department.name : 'Unknown';
|
||||||
|
|
||||||
|
if (departmentElement.textContent !== `Department: ${departmentName}`) {
|
||||||
|
departmentElement.textContent = `Department: ${departmentName}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchDepartments() {
|
async function fetchDepartments() {
|
||||||
// Send the request to get departments
|
|
||||||
if (!await window.networkAPI.sendUcMessage(codeGetDepartments)) {
|
if (!await window.networkAPI.sendUcMessage(codeGetDepartments)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -121,7 +150,6 @@ async function fetchDepartments() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchUsers() {
|
async function fetchUsers() {
|
||||||
// Send the request to get users
|
|
||||||
if (!await window.networkAPI.sendUcMessage(codeGetUsers)) {
|
if (!await window.networkAPI.sendUcMessage(codeGetUsers)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -129,7 +157,6 @@ async function fetchUsers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function deleteUser(userId) {
|
async function deleteUser(userId) {
|
||||||
// Send the request to delete the user
|
|
||||||
if (!await window.networkAPI.sendUcMessage(codeDeleteUser, { id: userId })) {
|
if (!await window.networkAPI.sendUcMessage(codeDeleteUser, { id: userId })) {
|
||||||
alert('Failed to send request to delete user.');
|
alert('Failed to send request to delete user.');
|
||||||
return;
|
return;
|
||||||
@@ -141,10 +168,15 @@ async function deleteUser(userId) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await fetchData();
|
// Remove user from UI
|
||||||
|
if (userElementsMap.has(userId)) {
|
||||||
|
userElementsMap.get(userId).remove();
|
||||||
|
userElementsMap.delete(userId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function redirectToMainMenu() {
|
async function redirectToMainMenu() {
|
||||||
|
await stopFetchingUsers();
|
||||||
await window.networkAPI.closeUcSocket();
|
await window.networkAPI.closeUcSocket();
|
||||||
fadeOut('main_menu');
|
fadeOut('main_menu');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ app.whenReady().then(async () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.removeMenu()
|
//mainWindow.removeMenu()
|
||||||
|
|
||||||
await ensureDirectoryExists(pathToClientsBackups)
|
await ensureDirectoryExists(pathToClientsBackups)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user