un stadiu mai avansat cu overlay the confirmare a parolei CEO

This commit is contained in:
andrei-mihnea-cerbu
2024-04-01 17:15:35 +03:00
parent b7aad6cbe2
commit 79e508d3de
41 changed files with 1295 additions and 207 deletions
+11
View File
@@ -0,0 +1,11 @@
document.addEventListener('DOMContentLoaded', () => {
const closeButton = document.getElementById('closeButton');
closeButton.addEventListener('click', () => {
window.electronAPI.closeAlertWindow();
});
});
function showAlert(message) {
const modalMessage = document.getElementById('modal-message');
modalMessage.textContent = message;
}
+89
View File
@@ -0,0 +1,89 @@
document.addEventListener('DOMContentLoaded', async function () {
const signinButton = document.getElementById('signin');
const submitButton = document.getElementById('submit');
await window.electronAPI.readFile('loginData.json')
.then(async result => {
const loginData = JSON.parse(result.content);
const { email, password } = loginData;
const response = await fetch('http://localhost:5000/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
email: email,
password: password
})
});
if(response.ok) {
window.electronAPI.changeContent('main_menu.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
}
}).catch(async error => {
console.error('Can\'t read loginData');
await window.electronAPI.deleteFile('loginData.json')
});
signinButton.addEventListener('click', function (e) {
window.electronAPI.changeContent('sign_up_profile.html')
.then(() => console.log('Content changed successfully'))
.catch(error => console.error('Error changing content:', error));
});
submitButton.addEventListener('click', async function (e) {
try {
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');
if (!email || !password) {
throw new Error('Both email and password are required.');
}
const response = await fetch('http://localhost:5000/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
email: email,
password: password
})
});
switch (response.status) {
case 401:
throw new Error('Invalid credentials!');
case 500:
throw new Error('Internal server error. Try again later!');
}
const responseBody = await response.json();
const result = await window.electronAPI.writeFile('loginData.json', JSON.stringify(responseBody.message, null, 2));
if (!result.success) {
throw new Error('Error writing to file. Please try again later.');
}
window.electronAPI.changeContent('main_menu.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
} catch (error) {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error showing alert:', error));
}
});
});
+156
View File
@@ -0,0 +1,156 @@
document.addEventListener('DOMContentLoaded', async function () {
const backupButton = document.getElementById('backup');
const changeDepartmentButton = document.getElementById('change_department');
const changeInfoButton = document.getElementById('change_info');
const shareFileButton = document.getElementById('share_file');
const decryptButton = document.getElementById('decrypt');
const logoutButton = document.getElementById('logout');
const overlay = document.getElementById('overlay');
const ceoBackButton = document.getElementById('back_button');
const ceoSubmitButton = document.getElementById('submit_button');
let triggerSource = '';
function handleOverlayOpen(buttonId) {
overlay.style.display = 'block';
triggerSource = buttonId; // Remember the button that triggered the overlay
console.log(`${buttonId} button clicked!`);
}
changeDepartmentButton.addEventListener('click', function () {
handleOverlayOpen('change_department');
});
decryptButton.addEventListener('click', function () {
handleOverlayOpen('decrypt');
});
// Hide the form when the "Back" button is clicked
ceoBackButton.addEventListener('click', function() {
overlay.style.display = 'none';
});
// Validate and process the form when submitted
ceoSubmitButton.addEventListener('click', async function(event) {
event.preventDefault();
const password = document.getElementById('ceo_password').value;
const ceoPassword = getCeoPassword();
if (password === '') {
alert('Please enter a password.');
return;
}
if(password !== ceoPassword){
return;
}
if (triggerSource === 'change_department') {
} else if (triggerSource === 'decrypt') {
await decryptFiles()
}
overlay.style.display = 'none';
});
checkFileExists()
.then(() => console.log('verificare facuta'));
await insertUsername();
backupButton.addEventListener('click', function () {
console.log('Set backup directory button clicked!');
window.electronAPI.openBackupDirDialog()
.then(() => console.log('Back-up directory set'))
.catch(async error => await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error showing alert:', error)));
});
changeInfoButton.addEventListener('click', function () {
console.log('Change your info button clicked!');
window.electronAPI.changeContent('profile.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
});
shareFileButton.addEventListener('click', function () {
console.log('Share a file button clicked!');
window.electronAPI.changeContent('share_file.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
});
logoutButton.addEventListener('click', async function () {
console.log('Logout button clicked!');
await window.electronAPI.deleteFile('loginData.json');
window.electronAPI.changeContent('login.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
});
async function checkFileExists() {
try {
// Make an IPC call to check file existence
const fileExists = await window.electronAPI.checkFileExists('dirBackup.json');
if (!fileExists) {
const notificationsDiv = document.getElementById('notifications');
const button = document.createElement('button');
button.id = 'backup_alert';
button.name = 'alert';
button.textContent = 'Set your backup directory!';
button.addEventListener('click', handleButtonClick);
notificationsDiv.appendChild(button);
}
} catch (error) {
console.error('Error checking file existence:', error);
}
}
async function handleButtonClick() {
console.log('Button clicked!');
await window.electronAPI.openBackupDirDialog()
.then(() => console.log('Back-up directory set'))
.catch(async error => await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error showing alert:', error)));
const button = document.getElementById('backup_alert');
button.remove();
}
async function insertUsername() {
try {
const userData = await window.electronAPI.readFile('loginData.json');
const userJson = JSON.parse(userData.content);
const username = userJson.name;
console.log(userData);
console.log(username);
const usernameField = document.getElementById('username_field');
if (usernameField) {
usernameField.textContent = username + '!';
}
} catch (error) {
console.error('Error loading username:', error);
const usernameField = document.getElementById('username_field');
usernameField.textContent = 'User!';
}
}
async function decryptFiles(){
}
async function getCeoPassword(){
}
});
+87
View File
@@ -0,0 +1,87 @@
document.addEventListener('DOMContentLoaded', async function () {
try {
const result = await window.electronAPI.readFile('loginData.json');
const loginData = JSON.parse(result.content);
// Set values for the inputs
const emailInput = document.querySelector('input[name="email"]');
const usernameInput = document.querySelector('input[name="username"]');
const passwordInput = document.querySelector('input[name="password"]');
emailInput.value = loginData.email;
usernameInput.value = loginData.name;
passwordInput.value = loginData.password;
} catch (error) {
console.error('Error reading login data:', error);
}
const backButton = document.querySelector('button[name="login"]');
const submitButton = document.querySelector('button[name="submit"]');
// Add event listeners for the back and submit buttons
backButton.addEventListener('click', function () {
console.log('Back button clicked!');
window.electronAPI.changeContent('main_menu.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
});
submitButton.addEventListener('click', async function (e) {
try {
e.preventDefault();
console.log('Submit button clicked!');
const emailInput = document.querySelector('input[name="email"]');
const usernameInput = document.querySelector('input[name="username"]');
const passwordInput = document.querySelector('input[name="password"]');
const result = await window.electronAPI.readFile('loginData.json');
const loginData = JSON.parse(result.content);
const {id, department} = loginData;
const email = emailInput.value;
const name = usernameInput.value;
const password = passwordInput.value;
const response = await fetch(`http://localhost:5000/users/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
name: name,
email: email,
password: password
})
});
switch (response.status) {
case 400:
throw new Error('Email format invalid!')
case 409:
throw new Error('Email already in system!')
case 500:
throw new Error('Internal server error. Try again later!')
}
await window.electronAPI.writeFile('loginData.json', JSON.stringify({
id: id,
name: name,
email: email,
password: password,
department: department
}));
window.electronAPI.changeContent('main_menu.html')
.then(() => console.log('Navigated to dashboard'))
.catch(error => console.error('Error navigating:', error));
}catch(error) {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error showing alert:', error));
}
});
});
@@ -0,0 +1,7 @@
document.addEventListener('DOMContentLoaded', async function() {
await new Promise(resolve => setTimeout(resolve, 2000));
window.electronAPI.changeContent('login.html')
.then(() => console.log('Content changed successfully'))
.catch(error => console.error('Error changing content:', error));
});
@@ -0,0 +1,93 @@
document.addEventListener('DOMContentLoaded', async function() {
try {
// Make API call to fetch department data
const response = await fetch('http://localhost:5000/departments', {
method: 'GET',
headers: {
'x-api-key': 'uc_api'
}
});
const res = await response.json();
const data = res['data'];
const formContent = document.querySelector('.signup-form-content');
data.forEach(department => {
const label = document.createElement('label');
label.innerHTML = `<input type="radio" name="dept" value="${department.id}">${department.name}`;
formContent.appendChild(label);
});
} catch (error) {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error changing content:', error));
}
document.getElementById('back').addEventListener('click', async function() {
try {
await window.electronAPI.changeContent('sign_up_profile.html');
console.log('Content changed successfully');
} catch (error) {
console.error('Error changing content:', error);
}
});
// Handler for the continue button
document.getElementById('submit').addEventListener('click', async function(e) {
e.preventDefault();
const selectedDept = document.querySelector('input[name="dept"]:checked').value;
try {
if (!selectedDept) {
throw new Error('No department had been selected!.');
}
let result = await window.electronAPI.readFile('signupData.json');
if (!result.success) {
throw new Error('Error reading the file. Please try again later.');
}
const data = JSON.parse(result.content);
const email = data.email;
const name = data.name;
const password = data.password;
result = await window.electronAPI.deleteFile('signupData.json');
if (!result.success) {
throw new Error('Error deleting the file. Please try again later.');
}
await fetch('http://localhost:5000/users/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
name: name,
email: email,
password: password,
department: selectedDept,
})
}).then(async response => {
if(!response.ok){
await window.electronAPI.showAlert("Internal server error. Try again later!")
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error changing content:', error));
}
window.electronAPI.changeContent('sign_up_confirmation.html')
.then(() => console.log('Content changed successfully'))
.catch(error => console.error('Error changing content:', error));
}).catch(error => {
console.error(error);
});
} catch (error) {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error changing content:', error));
}
});
});
+81
View File
@@ -0,0 +1,81 @@
document.addEventListener('DOMContentLoaded', function () {
const cancelButton = document.getElementById('login');
const continueButton = document.getElementById('continue');
cancelButton.addEventListener('click', function () {
console.log(`'Login' button clicked!`);
window.electronAPI.changeContent('login.html')
.then(() => console.log('Content changed successfully'))
.catch(error => console.error('Error changing content:', error));
});
continueButton.addEventListener('click', async function (e) {
try {
e.preventDefault();
console.log('Continue button clicked');
const form = document.getElementById('signupForm');
const formData = new FormData(form);
const email = formData.get('email');
const name = formData.get('name');
const password = formData.get('password');
if (!email || !name || !password) {
throw new Error('All fields are required.');
}
if(password.length < 8){
throw new Error('Password must have minimum length 8!');
}
let statusFetch = 200;
await fetch('http://localhost:5000/users/validate_email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
email: email
})
}).then(async response => {
const responseData = await response.json();
statusFetch = response.status;
console.error(responseData.message);
})
.catch(error => {
console.log(error);
})
switch(statusFetch){
case 500:
throw new Error('Internal server error! Try again later');
case 400:
throw new Error('Not a valid email!');
case 409:
throw new Error('Email already exists in system!');
}
const formDataJSON = {};
formData.forEach((value, key) => {
formDataJSON[key] = value;
});
const result = await window.electronAPI.writeFile('signupData.json', JSON.stringify(formDataJSON));
if (!result.success) {
throw new Error('Error writing to file. Please try again later.');
}
window.electronAPI.changeContent('sign_up_departments.html')
.then(() => console.log('Content changed successfully'))
.catch(error => console.error('Error changing content:', error));
}
catch (error) {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error changing content:', error));;
}
});
});