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

87 lines
3.0 KiB
JavaScript

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