let codeGetDepartments = ''; let codeGetUsers = ''; let codeDeleteUser = ''; let codeOk = ''; document.addEventListener('DOMContentLoaded', async () => { const backButton = document.getElementById('backButton'); backButton.addEventListener('click', async () => { await window.electronAPI.closeUcSocket(); fadeOut('main_menu'); }); }); async function fetchData() { const operationCodes = await window.electronAPI.getOperationsCodes(); if (!operationCodes) { await window.electronAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.'); return; } codeGetDepartments = operationCodes.GET_DEPARTMENTS; codeGetUsers = operationCodes.GET_USERS; codeDeleteUser = operationCodes.DELETE_USER; codeOk = operationCodes.OK; // Open the TCP socket right after fetching the operation codes if (!await window.electronAPI.openUcSocket()) { alert('Failed to open socket. Internal error of the application.'); return; } // Fetch departments const departmentsResponse = await fetchDepartments(); if (!departmentsResponse || departmentsResponse.operationCode !== codeOk) { alert('Failed to fetch departments. Redirecting to main menu...'); await redirectToMainMenu(); return; } const departments = departmentsResponse.metaInfo.departments; // Fetch users const usersResponse = await fetchUsers(); if (!usersResponse || usersResponse.operationCode !== codeOk) { alert('Failed to fetch users. Redirecting to main menu...'); await redirectToMainMenu(); return; } const users = usersResponse.metaInfo.users; const usersContainer = document.getElementById('users-container'); usersContainer.innerHTML = ''; // Clear the container // Filter out users in the CEO department const ceoDepartment = departments.find(dept => dept.name.toLowerCase() === 'ceo'); const filteredUsers = users.filter(user => user.departmentId !== ceoDepartment.id); // Check if there are no users after filtering 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 filteredUsers.forEach(user => { const userDiv = document.createElement('div'); userDiv.classList.add('user-card'); // Create user info div const userInfoDiv = document.createElement('div'); userInfoDiv.classList.add('user-info'); // Create user name paragraph const nameP = document.createElement('p'); nameP.classList.add('user-name'); nameP.textContent = user.name; console.log(user) console.log(departments) // Find and display user's department name const department = departments.find(dept => dept.id === user.departmentId); const departmentName = department ? department.name : 'Unknown'; // Create user department paragraph const departmentP = document.createElement('p'); departmentP.classList.add('user-department'); departmentP.textContent = `Department: ${departmentName}`; // Append name and department to user info div userInfoDiv.appendChild(nameP); userInfoDiv.appendChild(departmentP); // Create delete button const deleteButton = document.createElement('button'); deleteButton.textContent = 'Delete'; deleteButton.classList.add('delete-button'); deleteButton.onclick = () => deleteUser(user.id); // Append user info div and delete button to user card userDiv.appendChild(userInfoDiv); userDiv.appendChild(deleteButton); // Append user card to the container usersContainer.appendChild(userDiv); }); } async function fetchDepartments() { // Send the request to get departments if (!await window.electronAPI.sendUcMessage(codeGetDepartments)) { return null; } return await waitForResponse(); } async function fetchUsers() { // Send the request to get users if (!await window.electronAPI.sendUcMessage(codeGetUsers)) { return null; } return await waitForResponse(); } async function deleteUser(userId) { // Send the request to delete the user if (!await window.electronAPI.sendUcMessage(codeDeleteUser, {id: userId})) { alert('Failed to send request to delete user.'); return; } const response = await waitForResponse(); if (!response || response.operationCode !== codeOk) { alert('Failed to delete user.'); return; } await fetchData(); } async function redirectToMainMenu() { await window.electronAPI.closeUcSocket(); fadeOut('main_menu'); }