Merged CEO and Client App
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const submitButton = document.getElementById('submitAnnouncement');
|
||||
const buttonBack = document.getElementById('backButton');
|
||||
const announcementTextarea = document.getElementById('announcementMessage');
|
||||
|
||||
buttonBack.addEventListener('click', function () {
|
||||
fadeOut('main_menu');
|
||||
});
|
||||
|
||||
submitButton.addEventListener('click', async function () {
|
||||
const message = announcementTextarea.value.trim();
|
||||
|
||||
if (message === '') {
|
||||
await window.uiAPI.showAlert('Please enter a message before sending.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the announcement message via the electronAPI
|
||||
window.workersAPI.startAnnouncementWorker(message);
|
||||
fadeOut('send_announcement');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
function fadeIn() {
|
||||
document.querySelector('.container').classList.remove('fade-out');
|
||||
document.querySelector('.container').classList.add('fade-in');
|
||||
}
|
||||
|
||||
function fadeOut(destination) {
|
||||
const container = document.querySelector('.container');
|
||||
container.classList.remove('fade-in');
|
||||
container.classList.add('fade-out');
|
||||
|
||||
console.log(destination);
|
||||
setTimeout(() => {}, 5000);
|
||||
|
||||
container.addEventListener('animationend', async () => {
|
||||
try {
|
||||
await window.uiAPI.changeContent(destination);
|
||||
console.log('Navigated to', destination);
|
||||
} catch (error) {
|
||||
console.error('Error navigating:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForResponse() {
|
||||
return new Promise((resolve) => {
|
||||
const idResponseCheck = setInterval(async () => {
|
||||
if (await window.networkAPI.hasResponseArrived()) {
|
||||
clearInterval(idResponseCheck);
|
||||
resolve(await window.networkAPI.getLastUcResult()); // Resolve the response or null if not available
|
||||
}
|
||||
}, 100); // Check every 100 milliseconds if the response has arrived
|
||||
});
|
||||
}
|
||||
|
||||
function toggleScroll(idComponent, scrollHeight = 180) {
|
||||
const element = document.getElementById(idComponent); // Using getElementById
|
||||
if (!element) {
|
||||
console.error(`Element with ID '${idComponent}' not found.`);
|
||||
return;
|
||||
}
|
||||
if (element.scrollHeight > scrollHeight) {
|
||||
element.style.overflowY = 'auto'; // Enable scroll if content overflows
|
||||
} else {
|
||||
element.style.overflowY = 'hidden'; // Disable scroll if content fits
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
let codeLogin = '';
|
||||
let codeFindByEmail = '';
|
||||
let codeFindKeyByUser = '';
|
||||
let codeOk = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
const submitButton = document.getElementById('submit');
|
||||
const resetPasswordButton = document.getElementById('resetPassword');
|
||||
|
||||
// Retrieve the operation codes via IPC
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.');
|
||||
return;
|
||||
}
|
||||
|
||||
codeLogin = operationCodes.LOGIN;
|
||||
codeFindByEmail = operationCodes.FIND_BY_EMAIL;
|
||||
codeFindKeyByUser = operationCodes.FIND_KEY_BY_USER_ID;
|
||||
codeOk = operationCodes.OK;
|
||||
|
||||
resetPasswordButton.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
window.uiAPI.changeContent('reset_password');
|
||||
});
|
||||
|
||||
// Submit button logic (handle login)
|
||||
submitButton.addEventListener('click', async function (e) {
|
||||
e.preventDefault();
|
||||
console.log('Submit button clicked');
|
||||
|
||||
const form = document.getElementById('loginForm');
|
||||
const formData = new FormData(form);
|
||||
|
||||
const email = formData.get('email');
|
||||
const password = formData.get('password');
|
||||
|
||||
// Open a TCP socket to the stored IP
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Unable to open socket.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Attempt login
|
||||
if (!await attemptLogin(email, password)) {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch and store user info
|
||||
if (!await fetchAndStoreUserInfo(email)) {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch user info from local storage
|
||||
const userInfo = await window.databaseAPI.getUserInfo('user_info');
|
||||
if (!userInfo) {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
await window.uiAPI.showAlert('Failed to fetch user info.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch and store encryption key
|
||||
if (!await fetchAndStoreEncryptionKey(userInfo.id)) {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
// Close the socket and navigate to main menu after success
|
||||
await window.networkAPI.closeUcSocket();
|
||||
await window.workersAPI.startWorkers();
|
||||
await window.databaseAPI.setLoginStatus(true);
|
||||
await window.uiAPI.changeContent('main_menu');
|
||||
});
|
||||
});
|
||||
|
||||
async function attemptLogin(email, password) {
|
||||
const app_type = await window.databaseAPI.getAppType();
|
||||
const messageData = {email, password, app_type};
|
||||
|
||||
// Send the login message to the server
|
||||
if (!await window.networkAPI.sendUcMessage(codeLogin, messageData)) {
|
||||
await window.uiAPI.showAlert('Failed to send login request.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
|
||||
if (!response) {
|
||||
await window.uiAPI.showAlert('No response from server.');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.operationCode !== codeOk) {
|
||||
await window.uiAPI.showAlert(response.metaInfo.message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async function fetchAndStoreUserInfo(userEmail) {
|
||||
if (!await window.networkAPI.sendUcMessage(codeFindByEmail, {email: userEmail})) {
|
||||
await window.uiAPI.showAlert('Failed to send request to fetch user info.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode === codeOk) {
|
||||
const userInfo = {};
|
||||
userInfo.id = response.metaInfo.id;
|
||||
userInfo.email = response.metaInfo.email;
|
||||
userInfo.departmentId = response.metaInfo.departmentId;
|
||||
userInfo.name = response.metaInfo.name || 'User';
|
||||
|
||||
await window.databaseAPI.writeUserInfo(userInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
await window.uiAPI.showAlert('Failed to fetch user info from server.');
|
||||
return false;
|
||||
}
|
||||
|
||||
async function fetchAndStoreEncryptionKey(userId) {
|
||||
if (!await window.networkAPI.sendUcMessage(codeFindKeyByUser, {userId})) {
|
||||
await window.uiAPI.showAlert('Failed to send request to fetch encryption key.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode === codeOk) {
|
||||
const encryptionKey = {
|
||||
key: response.metaInfo.key.key,
|
||||
iv: response.metaInfo.key.iv,
|
||||
};
|
||||
|
||||
await window.databaseAPI.writeEncryptionKey(encryptionKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
await window.uiAPI.showAlert('Failed to fetch encryption key from server.');
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
setInterval(loadReceivedFiles, 3000); // Call loadReceivedFiles every 3000 ms (3 seconds)
|
||||
|
||||
const backupButton = document.getElementById('backup-dir');
|
||||
const shareButton = document.getElementById('share-dir');
|
||||
|
||||
const changeInfoButton = document.getElementById('change-info');
|
||||
const shareFileButton = document.getElementById('share-file');
|
||||
const logoutButton = document.getElementById('logout');
|
||||
const restoreBackupButton = document.getElementById('restore-backup');
|
||||
const buttonOverviewDepartments = document.getElementById('overview-departments');
|
||||
const buttonOverviewUsers = document.getElementById('overview-users');
|
||||
const buttonResetDatabase = document.getElementById('reset-database');
|
||||
const buttonSendAnnouncement = document.getElementById('send-announcement');
|
||||
|
||||
backupButton.addEventListener('click', async function () {
|
||||
await setPath('backupDirectory');
|
||||
});
|
||||
|
||||
shareButton.addEventListener('click', async function () {
|
||||
await setPath('shareDirectory');
|
||||
});
|
||||
|
||||
buttonResetDatabase.addEventListener('click', async function () {
|
||||
// Show a confirmation dialog
|
||||
const userConfirmed = await showConfirmationDialog("Are you sure you want to reset the database? This action cannot be undone.");
|
||||
|
||||
// If the user confirms, perform the database reset
|
||||
if (userConfirmed) {
|
||||
await resetDatabase();
|
||||
} else {
|
||||
window.uiAPI.showAlert("Database reset canceled.");
|
||||
}
|
||||
});
|
||||
|
||||
buttonSendAnnouncement.addEventListener('click', async function () {
|
||||
fadeOut('announcement');
|
||||
});
|
||||
|
||||
buttonOverviewDepartments.addEventListener('click', async function () {
|
||||
fadeOut('overview_departments');
|
||||
});
|
||||
|
||||
buttonOverviewUsers.addEventListener('click', async function () {
|
||||
fadeOut('overview_users');
|
||||
});
|
||||
|
||||
changeInfoButton.addEventListener('click', function () {
|
||||
fadeOut('profile');
|
||||
});
|
||||
|
||||
shareFileButton.addEventListener('click', function () {
|
||||
fadeOut('share_file');
|
||||
});
|
||||
|
||||
logoutButton.addEventListener('click', async function () {
|
||||
fadeOut('login');
|
||||
});
|
||||
|
||||
// Add event listener for the restore backup button
|
||||
restoreBackupButton.addEventListener('click', async function () {
|
||||
await restoreBackup();
|
||||
});
|
||||
});
|
||||
|
||||
async function initialSetup(){
|
||||
await checkAndSetAllDirectories();
|
||||
await loadReceivedFiles();
|
||||
await fetchUserInfo();
|
||||
}
|
||||
|
||||
async function restoreBackup() {
|
||||
const backupDirectory = await window.databaseAPI.isBackupSet();
|
||||
|
||||
if (backupDirectory) {
|
||||
await window.uiAPI.showAlert('You have already set a backup directory. You cannot restore again.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Prompt the user to choose the destination for the restored backup
|
||||
const destinationPath = await window.uiAPI.selectDirectory();
|
||||
if (!destinationPath) {
|
||||
return; // User canceled the directory selection
|
||||
}
|
||||
|
||||
// Call the IPC method to initiate the backup retrieval process
|
||||
window.workersAPI.startBackupRetrieval(destinationPath);
|
||||
|
||||
// Switch the content to the 'backup_retrieve' page
|
||||
fadeOut('backup_retrieve');
|
||||
}
|
||||
|
||||
async function checkAndSetAllDirectories() {
|
||||
const directorySchemes = await window.databaseAPI.getLocalResources();
|
||||
|
||||
backupDirId = directorySchemes.backup.id;
|
||||
shareDirId = directorySchemes.shared.id;
|
||||
departmentDirId = directorySchemes.department.id;
|
||||
|
||||
await attachNotificationButton(backupDirId, 'Set your backup directory!', 'backup_alert', 'alert');
|
||||
await attachNotificationButton(shareDirId, 'Set your share directory!', 'share_alert', 'alert');
|
||||
await attachNotificationButton(departmentDirId, 'Set your department directory!', 'department_alert', 'alert');
|
||||
}
|
||||
|
||||
async function checkPathExistence(id) {
|
||||
const dirInfo = await window.databaseAPI.getDirectoryInfo(id);
|
||||
return dirInfo.path !== ''
|
||||
}
|
||||
|
||||
async function setPath(id){
|
||||
const path = await window.uiAPI.selectDirectory();
|
||||
if (path === undefined) return false;
|
||||
|
||||
return await window.databaseAPI.writeDirectoryPath(id, path);
|
||||
}
|
||||
|
||||
async function attachNotificationButton(entryId, buttonText, buttonId, buttonName) {
|
||||
const path = await checkPathExistence(entryId);
|
||||
if (!path) {
|
||||
const notificationsDiv = document.getElementById('notifications');
|
||||
const button = document.createElement('button');
|
||||
button.className='alert';
|
||||
button.id = buttonId;
|
||||
button.name = buttonName;
|
||||
button.textContent = buttonText;
|
||||
button.addEventListener('click', async function () {
|
||||
if(await setPath(entryId)) button.remove();
|
||||
});
|
||||
notificationsDiv.appendChild(button);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUserInfo() {
|
||||
const usernameField = document.getElementById('username-field');
|
||||
|
||||
// Read the user credentials from the userConfig
|
||||
let userInfo = await window.databaseAPI.getUserInfo();
|
||||
if (userInfo && userInfo.name) {
|
||||
usernameField.textContent = userInfo.name;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the greeting with the fetched user's name
|
||||
if (usernameField) {
|
||||
usernameField.textContent = userInfo.name;
|
||||
} else {
|
||||
console.error("Username field is not available in the DOM.");
|
||||
}
|
||||
}
|
||||
|
||||
async function loadReceivedFiles() {
|
||||
// Read the shareDirectory from applicationInfo
|
||||
const shareDirData = await window.databaseAPI.getDirectoryInfo(shareDirId);
|
||||
|
||||
console.log('Loading received files:', shareDirData);
|
||||
|
||||
const notificationsDiv = document.getElementById('notifications');
|
||||
const existingButtons = Array.from(notificationsDiv.children).map(button => button.getAttribute('data-filepath'));
|
||||
|
||||
// Iterate over each user and their files in the structure
|
||||
Object.keys(shareDirData.structure).forEach(userName => {
|
||||
const userFiles = shareDirData.structure[userName];
|
||||
|
||||
// Iterate over each file of the user
|
||||
Object.keys(userFiles).forEach(fileName => {
|
||||
const filePath = userFiles[fileName];
|
||||
|
||||
// Check if a button for this file path already exists
|
||||
if (!existingButtons.includes(filePath)) {
|
||||
const button = document.createElement('button');
|
||||
button.setAttribute('data-filepath', filePath); // Set a custom attribute to track the file path
|
||||
button.className = 'notification';
|
||||
button.name = 'notification';
|
||||
button.textContent = `You received a file "${fileName}" from ${userName}`; // Display the userName and file name
|
||||
button.addEventListener('click', () => handleFileReceivedButtonPressed(filePath, button));
|
||||
notificationsDiv.appendChild(button);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function handleFileReceivedButtonPressed(filePath, button) {
|
||||
console.log('Notification button clicked!');
|
||||
|
||||
// Open the file in the file explorer
|
||||
await window.uiAPI.showFileInExplorer(filePath)
|
||||
.then(() => console.log('File explorer opened for: ' + filePath))
|
||||
.catch(error => console.error('Error opening file explorer:', error));
|
||||
|
||||
// Remove the button after opening the file
|
||||
button.remove();
|
||||
}
|
||||
|
||||
async function showConfirmationDialog(message) {
|
||||
return new Promise((resolve) => {
|
||||
const userResponse = window.confirm(message); // Show native confirmation dialog
|
||||
resolve(userResponse); // Resolve with true (OK) or false (Cancel)
|
||||
});
|
||||
}
|
||||
|
||||
async function resetDatabase(){
|
||||
fadeOut('reset_database');
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
let codeOk = '';
|
||||
let codeGetDepartments = '';
|
||||
let codeCreateDepartment = '';
|
||||
let codeModifyDepartment = '';
|
||||
let codeDeleteDepartment = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const backButton = document.getElementById('backButton');
|
||||
const createButton = document.getElementById('createDepartmentButton');
|
||||
const closeCreateModalButton = document.getElementById('closeCreateModal');
|
||||
const confirmCreateButton = document.getElementById('confirmCreateButton');
|
||||
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Unable to open socket.');
|
||||
return;
|
||||
}
|
||||
|
||||
backButton.addEventListener('click', async () => {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
fadeOut('main_menu');
|
||||
});
|
||||
|
||||
// Show the Create Department Modal
|
||||
createButton.addEventListener('click', () => {
|
||||
document.getElementById('createDepartmentModal').style.display = 'block';
|
||||
});
|
||||
|
||||
// Close Modals
|
||||
closeCreateModalButton.addEventListener('click', () => {
|
||||
document.getElementById('createDepartmentModal').style.display = 'none';
|
||||
});
|
||||
|
||||
// Confirm Create Department
|
||||
confirmCreateButton.addEventListener('click', async () => {
|
||||
const departmentName = document.getElementById('newDepartmentName').value.trim();
|
||||
if (!departmentName) {
|
||||
window.uiAPI.showAlert("Please enter a department name.");
|
||||
return;
|
||||
}
|
||||
|
||||
await createDepartment(departmentName);
|
||||
document.getElementById('createDepartmentModal').style.display = 'none';
|
||||
});
|
||||
});
|
||||
|
||||
// Example usage to render these dummy departments
|
||||
async function fetchDepartments() {
|
||||
const departmentsContainer = document.getElementById('departments-container');
|
||||
departmentsContainer.innerHTML = ''; // Clear any existing data
|
||||
|
||||
// Retrieve the operation codes via IPC
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.');
|
||||
return;
|
||||
}
|
||||
|
||||
codeOk = operationCodes.OK;
|
||||
codeGetDepartments = operationCodes.GET_DEPARTMENTS;
|
||||
codeCreateDepartment = operationCodes.CREATE_DEPARTMENT;
|
||||
codeModifyDepartment = operationCodes.MODIFY_DEPARTMENT;
|
||||
codeDeleteDepartment = operationCodes.DELETE_DEPARTMENT;
|
||||
|
||||
// Send the login message to the server
|
||||
if (!await window.networkAPI.sendUcMessage(codeGetDepartments)) {
|
||||
await window.uiAPI.showAlert('Failed to send login request.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
|
||||
if(!response || response.operationCode !== codeOk){
|
||||
await window.uiAPI.showAlert('Could not fetch departments.');
|
||||
return false;
|
||||
}
|
||||
|
||||
const departments = response.metaInfo.departments;
|
||||
|
||||
const filteredDepartments = departments.filter(department => department.name.toLowerCase() !== 'ceo');
|
||||
|
||||
if (!filteredDepartments.length) {
|
||||
const noDepartmentsMessage = document.createElement('p');
|
||||
noDepartmentsMessage.textContent = 'No departments exist.';
|
||||
noDepartmentsMessage.classList.add('no-departments-message');
|
||||
departmentsContainer.appendChild(noDepartmentsMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
filteredDepartments.forEach(department => {
|
||||
const departmentDiv = document.createElement('div');
|
||||
departmentDiv.classList.add('department-card');
|
||||
|
||||
const nameDiv = document.createElement('div');
|
||||
nameDiv.classList.add('department-name');
|
||||
nameDiv.textContent = department.name;
|
||||
|
||||
// Create and add Modify button
|
||||
const modifyButton = document.createElement('button');
|
||||
modifyButton.textContent = 'Modify';
|
||||
modifyButton.classList.add('modify-button');
|
||||
modifyButton.dataset.departmentId = department.id;
|
||||
|
||||
modifyButton.addEventListener('click', () => {
|
||||
console.log(`Modify button clicked for department ID: ${department.id}`);
|
||||
openModifyModal(department.id, department.name); // Call the function to open the modify modal
|
||||
});
|
||||
|
||||
// Create and add Delete button
|
||||
const deleteButton = document.createElement('button');
|
||||
deleteButton.textContent = 'Delete';
|
||||
deleteButton.classList.add('delete-button');
|
||||
deleteButton.dataset.departmentId = department.id;
|
||||
|
||||
deleteButton.addEventListener('click', async () => {
|
||||
console.log(`Delete button clicked for department ID: ${department.id}`);
|
||||
await deleteDepartment(department.id); // Call the function to delete the department
|
||||
});
|
||||
|
||||
departmentDiv.appendChild(nameDiv);
|
||||
departmentDiv.appendChild(modifyButton);
|
||||
departmentDiv.appendChild(deleteButton);
|
||||
|
||||
departmentsContainer.appendChild(departmentDiv);
|
||||
});
|
||||
|
||||
toggleScroll('departments-container', 370);
|
||||
}
|
||||
|
||||
|
||||
async function createDepartment(departmentName) {
|
||||
if (!await window.networkAPI.sendUcMessage(codeCreateDepartment, { departmentName: departmentName })) {
|
||||
await window.uiAPI.showAlert('Failed to send login request.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
|
||||
if(!response || response.operationCode !== codeOk){
|
||||
await window.uiAPI.showAlert('Could not create new department.');
|
||||
return false;
|
||||
}
|
||||
|
||||
await fetchDepartments();
|
||||
}
|
||||
|
||||
async function modifyDepartment(departmentId, newDepartmentName) {
|
||||
if (!await window.networkAPI.sendUcMessage(codeModifyDepartment, { departmentId: departmentId, newDepartmentName: newDepartmentName })) {
|
||||
await window.uiAPI.showAlert('Failed to send modify request.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for the response using waitForResponse
|
||||
const response = await waitForResponse();
|
||||
|
||||
if(!response || response.operationCode !== codeOk){
|
||||
await window.uiAPI.showAlert('Could not modify department.');
|
||||
console.log(response.metaInfo.message);
|
||||
return false;
|
||||
}
|
||||
|
||||
await fetchDepartments();
|
||||
}
|
||||
|
||||
async function deleteDepartment(departmentId) {
|
||||
const confirmDelete = confirm("Are you sure you want to delete this department?");
|
||||
if (!confirmDelete) return;
|
||||
|
||||
if (!await window.networkAPI.sendUcMessage(codeDeleteDepartment, { departmentId: departmentId })) {
|
||||
await window.uiAPI.showAlert('Failed to send delete request.');
|
||||
return false;
|
||||
}
|
||||
|
||||
const response = await waitForResponse();
|
||||
|
||||
if(!response || response.operationCode !== codeOk){
|
||||
await window.uiAPI.showAlert('Could not delete department.');
|
||||
return false;
|
||||
}
|
||||
|
||||
await fetchDepartments();
|
||||
}
|
||||
|
||||
// Show the Modify Department Modal
|
||||
function openModifyModal(departmentId, departmentName) {
|
||||
document.getElementById('confirmModifyButton').value = departmentId;
|
||||
document.getElementById('modifiedDepartmentName').value = departmentName;
|
||||
document.getElementById('modifyDepartmentModal').style.display = 'block';
|
||||
|
||||
document.getElementById('closeModifyModal').addEventListener('click', () => {
|
||||
document.getElementById('modifyDepartmentModal').style.display = 'none';
|
||||
});
|
||||
|
||||
document.getElementById('confirmModifyButton').addEventListener('click', async () => {
|
||||
const newDepartmentName = document.getElementById('modifiedDepartmentName').value.trim();
|
||||
const departmentId = document.getElementById('confirmModifyButton').value;
|
||||
|
||||
console.log(newDepartmentName, departmentId);
|
||||
|
||||
if (!newDepartmentName || !departmentId) {
|
||||
window.uiAPI.showAlert("Please enter a new department name.");
|
||||
return;
|
||||
}
|
||||
await modifyDepartment(departmentId, newDepartmentName);
|
||||
document.getElementById('modifyDepartmentModal').style.display = 'none';
|
||||
});
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
let codeGetDepartments = '';
|
||||
let codeGetUsers = '';
|
||||
let codeDeleteUser = '';
|
||||
let codeOk = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const backButton = document.getElementById('backButton');
|
||||
backButton.addEventListener('click', async () => {
|
||||
await window.networkAPI.closeUcSocket();
|
||||
fadeOut('main_menu');
|
||||
});
|
||||
});
|
||||
|
||||
async function fetchData() {
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.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.networkAPI.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.networkAPI.sendUcMessage(codeGetDepartments)) {
|
||||
return null;
|
||||
}
|
||||
return await waitForResponse();
|
||||
}
|
||||
|
||||
async function fetchUsers() {
|
||||
// Send the request to get users
|
||||
if (!await window.networkAPI.sendUcMessage(codeGetUsers)) {
|
||||
return null;
|
||||
}
|
||||
return await waitForResponse();
|
||||
}
|
||||
|
||||
async function deleteUser(userId) {
|
||||
// Send the request to delete the user
|
||||
if (!await window.networkAPI.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.networkAPI.closeUcSocket();
|
||||
fadeOut('main_menu');
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
let id = '';
|
||||
let departmentId = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
const {id: userId, name, email, departmentId: userDepartmentId} = await window.databaseAPI.getUserInfo();
|
||||
|
||||
const emailInput = document.querySelector('input[name="email"]');
|
||||
const usernameInput = document.querySelector('input[name="username"]');
|
||||
const passwordInput = document.querySelector('input[name="password"]');
|
||||
|
||||
// Pre-fill form fields with existing data
|
||||
id = userId
|
||||
departmentId = userDepartmentId;
|
||||
usernameInput.value = name;
|
||||
emailInput.value = email;
|
||||
passwordInput.value = '';
|
||||
|
||||
const backButton = document.getElementById('back');
|
||||
const submitButton = document.getElementById('submit');
|
||||
|
||||
// Get operation codes from the backend
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.showAlert('Internal error of the application.');
|
||||
return;
|
||||
}
|
||||
|
||||
const codeModifyUser = operationCodes.MODIFY_USER;
|
||||
const codeOk = operationCodes.OK;
|
||||
|
||||
// Handle the back button click
|
||||
backButton.addEventListener('click', async function () {
|
||||
console.log('Back button clicked!');
|
||||
fadeOut('main_menu');
|
||||
});
|
||||
|
||||
// Handle the submit button click
|
||||
submitButton.addEventListener('click', async function (e) {
|
||||
e.preventDefault();
|
||||
console.log('Submit button clicked!');
|
||||
|
||||
// Fetch form values
|
||||
const email = emailInput.value;
|
||||
const name = usernameInput.value;
|
||||
const password = passwordInput.value;
|
||||
|
||||
const app_type = await window.databaseAPI.getAppType();
|
||||
|
||||
// Prepare the data to be sent via the UC socket
|
||||
const messageData = {
|
||||
id: id,
|
||||
name: name,
|
||||
email: email,
|
||||
password: password,
|
||||
departmentId: departmentId,
|
||||
app_type: app_type
|
||||
};
|
||||
|
||||
// Open UC socket
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Failed to open socket. Internal error of the application.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the message to the server using the UC socket
|
||||
if (!await window.networkAPI.sendUcMessage(codeModifyUser, messageData)) {
|
||||
await window.uiAPI.showAlert('Failed to send message.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for the response
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode === codeOk) {
|
||||
console.log('User update successful.');
|
||||
|
||||
// Save updated user info to the userConfig
|
||||
await window.databaseAPI.writeUserInfo({ id: id, email: email, name: name, departmentId: departmentId });
|
||||
|
||||
// Navigate back to the main menu
|
||||
fadeOut('main_menu');
|
||||
} else {
|
||||
console.log('Error updating user:', response?.metaInfo?.message || 'Unknown error');
|
||||
await window.electronAPI.showAlert(response?.metaInfo?.message || 'Unknown error occurred');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.');
|
||||
return;
|
||||
}
|
||||
|
||||
const codeResetDatabase = operationCodes.RESET_DATABASE;
|
||||
const codeOk = operationCodes.OK;
|
||||
|
||||
// Open a TCP socket to the stored IP
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Internal error of the application. Unable to open socket.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await window.networkAPI.sendUcMessage(codeResetDatabase)) {
|
||||
await window.uiAPI.showAlert('Failed to send login request.');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode !== codeOk) {
|
||||
await window.uiAPI.showAlert('Database reset failed.');
|
||||
return;
|
||||
}
|
||||
|
||||
await window.networkAPI.closeUcSocket();
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
fadeOut('login');
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
let codeResetPassword = '';
|
||||
let codeOk = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
const backToLoginButton = document.getElementById('backToLogin');
|
||||
const resetPasswordButton = document.getElementById('resetPassword');
|
||||
|
||||
// Retrieve the operation codes via IPC
|
||||
const operationCodes = await window.networkAPI.getOperationsCodes();
|
||||
if (!operationCodes) {
|
||||
await window.uiAPI.showAlert('Internal error of the application.');
|
||||
return;
|
||||
}
|
||||
|
||||
codeResetPassword = operationCodes.RESET_PASSWORD;
|
||||
codeOk = operationCodes.OK;
|
||||
|
||||
// Back to login
|
||||
backToLoginButton.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
window.uiAPI.changeContent('login');
|
||||
});
|
||||
|
||||
// Reset password logic
|
||||
resetPasswordButton.addEventListener('click', async function (e) {
|
||||
e.preventDefault();
|
||||
if (!await window.networkAPI.openUcSocket()) {
|
||||
await window.uiAPI.showAlert('Internal error of the application.');
|
||||
return;
|
||||
}
|
||||
|
||||
const form = document.getElementById('resetPasswordForm');
|
||||
const formData = new FormData(form);
|
||||
const email = formData.get('email');
|
||||
const newPassword = formData.get('newPassword');
|
||||
|
||||
// Ensure the email and new password are provided
|
||||
if (!email || !newPassword) {
|
||||
await window.uiAPI.showAlert('Please provide both email and new password.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await attemptResetPassword(email, newPassword)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await window.networkAPI.closeUcSocket();
|
||||
await window.uiAPI.showAlert('Password reset successfully.');
|
||||
await window.uiAPI.changeContent('login');
|
||||
});
|
||||
});
|
||||
|
||||
async function attemptResetPassword(email, newPassword) {
|
||||
const app_type = await window.databaseAPI.getAppType();
|
||||
const messageData = { email, newPassword, app_type };
|
||||
|
||||
if (!await window.networkAPI.sendUcMessage(codeResetPassword, messageData)) return false;
|
||||
|
||||
const response = await waitForResponse();
|
||||
if (response && response.operationCode === codeOk) {
|
||||
return true;
|
||||
} else {
|
||||
await window.uiAPI.showAlert(response?.metaInfo?.message || 'Error resetting password.');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
let pathToFile = '';
|
||||
let fetchUsersInterval = null;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
const selectFileButton = document.getElementById('selectFile');
|
||||
const submitButton = document.getElementById('submitButton');
|
||||
const backButton = document.getElementById('backButton');
|
||||
|
||||
selectFileButton.addEventListener('click', async function (event) {
|
||||
event.preventDefault();
|
||||
try {
|
||||
pathToFile = await window.uiAPI.selectFile();
|
||||
updateFileName();
|
||||
} catch (error) {
|
||||
console.error('Error opening file dialog:', error);
|
||||
}
|
||||
});
|
||||
submitButton.addEventListener('click', async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
// Check if a file was chosen
|
||||
if (!pathToFile.trim()) {
|
||||
await window.uiAPI.showAlert('File not chosen!');
|
||||
return;
|
||||
}
|
||||
|
||||
const user_info = await window.databaseAPI.getUserInfo();
|
||||
if (!user_info) {
|
||||
return;
|
||||
}
|
||||
|
||||
const form = document.getElementById('userDestForm');
|
||||
const checkboxes = form.querySelectorAll('input[name="users"]');
|
||||
const selectedUserIps = Array.from(checkboxes)
|
||||
.filter(checkbox => checkbox.checked)
|
||||
.map(checkbox => checkbox.value); // Get IP of the selected users
|
||||
|
||||
if (!selectedUserIps.length) {
|
||||
await window.uiAPI.showAlert('No user selected!');
|
||||
return;
|
||||
}
|
||||
|
||||
for (const selectedUserIp of selectedUserIps) {
|
||||
try {
|
||||
// Add task to send file to the queue
|
||||
const task = {
|
||||
ip: selectedUserIp, // Destination IP for the file
|
||||
path: pathToFile, // File path
|
||||
userName: user_info.name // Sender's username from userConfig
|
||||
};
|
||||
await window.databaseAPI.addTaskToSendFileQueue(task);
|
||||
await window.uiAPI.showAlert('File sent to the queue.');
|
||||
console.log(`Task added to send file to IP ${selectedUserIp}`);
|
||||
} catch (error) {
|
||||
console.error(`Error processing IP ${selectedUserIp}:`, error);
|
||||
}
|
||||
}
|
||||
});
|
||||
backButton.addEventListener('click', function (event) {
|
||||
event.preventDefault();
|
||||
fadeOut('main_menu');
|
||||
});
|
||||
|
||||
setInterval(fetchUsersAndCreateCheckboxes, 5000);
|
||||
});
|
||||
|
||||
// Function to update the file name display
|
||||
function updateFileName() {
|
||||
const fileNameElement = document.getElementById('selectFile');
|
||||
if (!fileNameElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathToFile.trim() === '') {
|
||||
fileNameElement.textContent = 'No file chosen';
|
||||
} else {
|
||||
fileNameElement.textContent = pathToFile.split('\\').pop().split('/').pop();
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUsersAndCreateCheckboxes() {
|
||||
// Step 1: Get the currently checked users before refreshing the list
|
||||
const selectedUserIps = new Set(
|
||||
Array.from(document.querySelectorAll('input[name="users"]:checked'))
|
||||
.map(checkbox => checkbox.value)
|
||||
);
|
||||
|
||||
const usersInfo = await window.databaseAPI.getActiveUsers();
|
||||
const filteredUsers = usersInfo.filter(user => user.id !== '');
|
||||
if (!filteredUsers.length === 0) {
|
||||
await window.uiAPI.showAlert('No active users found.');
|
||||
clearInterval(fetchUsersInterval);
|
||||
fetchUsersInterval = null;
|
||||
await window.uiAPI.changeContent('main_menu');
|
||||
return;
|
||||
}
|
||||
|
||||
const usersDiv = document.getElementById('choose_user_form_content');
|
||||
usersDiv.innerHTML = ''; // Clear the list before updating
|
||||
|
||||
usersInfo.forEach(user => {
|
||||
const checkbox = document.createElement('input');
|
||||
checkbox.type = 'checkbox';
|
||||
checkbox.name = 'users';
|
||||
checkbox.value = user.ip; // Store user's IP in the value
|
||||
|
||||
// Step 2: Check if this user was previously selected
|
||||
if (selectedUserIps.has(user.ip)) {
|
||||
checkbox.checked = true; // Restore checked state
|
||||
}
|
||||
|
||||
const label = document.createElement('label');
|
||||
label.innerHTML = `${user.name}`;
|
||||
label.insertBefore(checkbox, label.firstChild);
|
||||
|
||||
usersDiv.appendChild(label);
|
||||
});
|
||||
|
||||
toggleScroll('choose_user_form_content');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user