Incepere creare procese separate

This commit is contained in:
andrei-mihnea-cerbu
2024-04-03 23:22:38 +03:00
parent 038b809dd8
commit ffeebf1177
58 changed files with 913 additions and 479 deletions
+50 -60
View File
@@ -1,81 +1,71 @@
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('DOMContentLoaded', async function () {
const cancelButton = document.getElementById('login');
const continueButton = document.getElementById('continue');
const signupDataExists = await window.electronAPI.checkFileExists('signupData.json');
if(signupDataExists){
await window.electronAPI.readFile('signupData.json')
.then(result => {
const signupData = JSON.parse(result.content);
// Assign the values to the form inputs
if (signupData.email) document.querySelector('input[name="email"]').value = signupData.email;
if (signupData.name) document.querySelector('input[name="name"]').value = signupData.name;
if (signupData.password) document.querySelector('input[name="password"]').value = signupData.password;
})
.catch(error => {
console.error('Error reading signup data:', error.message);
});
}
let ip = '';
await window.electronAPI.readFile('ipConfig.json')
.then(result => {
const jsonData = JSON.parse(result.content);
ip = jsonData.ip;
})
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));
fadeOut('login.html');
});
continueButton.addEventListener('click', async function (e) {
try {
e.preventDefault();
console.log('Continue button clicked');
e.preventDefault();
console.log('Continue button clicked');
const form = document.getElementById('signupForm');
const formData = new FormData(form);
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');
const email = formData.get('email');
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);
await fetch(`http://${ip}/users/validate_email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'uc_api'
},
body: JSON.stringify({
email: email
})
.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!');
}).then(async response => {
if (!response.ok) {
const data = await response.json();
throw new Error(data.message);
}
}).then(async () => {
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));;
}
await window.electronAPI.writeFile('signupData.json', JSON.stringify(formDataJSON));
fadeOut('sign_up_departments.html');
}).catch(async error => {
await window.electronAPI.showAlert(error.message)
.then(() => console.log('Alert window opened'))
.catch(error => console.error('Error changing content:', error));
})
});
});