Skip to content

Commit

Permalink
console cleaning - footer
Browse files Browse the repository at this point in the history
  • Loading branch information
TetiZ committed Feb 1, 2024
1 parent 8110ced commit 48130ff
Showing 1 changed file with 51 additions and 44 deletions.
95 changes: 51 additions & 44 deletions src/js/submit-email-footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,61 @@ import 'izitoast/dist/css/iziToast.min.css';
const form = document.querySelector('.subs-form');
const localStorageKey = 'user-email';

// const savedEmail = localStorage.getItem(localStorageKey);
if (form) {
const savedEmail = localStorage.getItem(localStorageKey);

// function saveToLocalStorage(evt) {
// localStorage.setItem(localStorageKey, evt.target.value);
// }
// form.addEventListener('input', saveToLocalStorage);
function saveToLocalStorage(evt) {
localStorage.setItem(localStorageKey, evt.target.value);
}
form.addEventListener('input', saveToLocalStorage);

// form.elements.email.value = savedEmail ? savedEmail : '';
form.elements.email.value = savedEmail ? savedEmail : '';

async function postEmail(userEmail) {
try {
const response = await fetch(
'https://energyflow.b.goit.study/api/subscription',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: userEmail }),
async function postEmail(userEmail) {
try {
const response = await fetch(
'https://energyflow.b.goit.study/api/subscription',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: userEmail }),
}
);
if (!response.ok) {
console.error('Код помилки:', response.status);
throw new Error('Мережевий запит не був успішним');
}
);
if (!response.ok) {
console.error('Код помилки:', response.status);
throw new Error('Мережевий запит не був успішним');
}
const data = await response.json();
console.log('Дані успішно відправлені', data);
const data = await response.json();
console.log('Дані успішно відправлені', data);

iziToast.success({
title: 'We are excited to have you on board!',
message:
'Thank you for subscribing to new exercises on Energy Flow. You have just taken a significant step towards improving your fitness and well-being.',
position: 'center',
});
} catch (error) {
console.error('Помилка при відправленні даних', error);
iziToast.success({
title: 'We are excited to have you on board!',
message:
'Thank you for subscribing to new exercises on Energy Flow. You have just taken a significant step towards improving your fitness and well-being.',
position: 'center',
});
} catch (error) {
console.error('Помилка при відправленні даних', error);
}
}
}

form.addEventListener('submit', async e => {
e.preventDefault();
// const userEmail = savedEmail;
// if (userEmail) {
try {
await postEmail(userEmail);
// localStorage.removeItem(localStorageKey);
form.reset();
} catch (error) {
console.error('Помилка при відправленні електронної пошти.', error);
}
});
form.addEventListener('submit', async e => {
e.preventDefault();
const userEmail = savedEmail;
if (userEmail) {
try {
await postEmail(userEmail);
localStorage.removeItem(localStorageKey);
form.reset();
} catch (error) {
console.error('Помилка при відправленні електронної пошти.', error);
}
} else {
console.log('Електронна пошта не введена.');
}
});
} else {
console.error('Форма не найдена.');
}

0 comments on commit 48130ff

Please sign in to comment.