Files
FACULTATE-LICENTA/Desktop App/render/ceo/js/share_file.js
T

122 lines
4.2 KiB
JavaScript

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');
}