UI User relativ functional

This commit is contained in:
andrei-mihnea-cerbu
2024-04-01 22:44:09 +03:00
parent 79e508d3de
commit fb957a6f97
18 changed files with 381 additions and 83 deletions
+82
View File
@@ -0,0 +1,82 @@
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('.department-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('main_menu.html');
console.log('Content changed successfully');
} catch (error) {
console.error('Error changing content:', error);
}
});
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('loginData.json');
if (!result.success) {
throw new Error('Error reading the file. Please try again later.');
}
const data = JSON.parse(result.content);
const id = data.id;
await fetch(`http://localhost:5000/users/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
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));
return;
}
window.electronAPI.changeContent('main_menu.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));
}
});
});