-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (73 loc) · 2.58 KB
/
script.js
File metadata and controls
88 lines (73 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const scrollItems = document.querySelectorAll(".scroll-item");
const containerAll = document.getElementById("container-all");
const containerEmail = document.getElementById("container-email--first");
const input = document.getElementById("display");
const button = document.getElementById("btn");
const form = document.getElementById("brevo-form");
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
let usuariosSalvos = JSON.parse(localStorage.getItem("usuarios")) || [];
function salvarUsuario(email) {
if (!usuariosSalvos.includes(email)) {
usuariosSalvos.push(email);
localStorage.setItem("usuarios", JSON.stringify(usuariosSalvos));
return true; // novo usuário salvo
} else {
console.log("Email já cadastrado.");
return false; // já existia
}
}
document.addEventListener("DOMContentLoaded", () => {
const usuariosSalvos = JSON.parse(localStorage.getItem("usuarios") || "[]");
if (usuariosSalvos.length > 0) {
// Usuário já se inscreveu → mostra conteúdo, esconde form
containerEmail.style.display = "none";
containerAll.style.display = "flex";
} else {
// Ainda não se inscreveu → mostra form, esconde conteúdo
containerEmail.style.display = "flex";
containerAll.style.display = "none";
}
});
form.addEventListener("submit", async (e) => {
e.preventDefault();
const value = input.value.trim();
if (!value || !emailRegex.test(value)) {
alert("Por favor, insira um email válido.");
input.focus();
return;
}
const novoUsuario = salvarUsuario(value);
if (!novoUsuario) {
alert("Você já se inscreveu antes!");
containerEmail.style.display = "none";
containerAll.style.display = "block";
form.reset();
return;
}
try {
const formData = new FormData(form);
await fetch(form.action, {
method: form.method,
body: formData,
mode: "no-cors", // obrigatório no Brevo
});
alert("Email cadastrado com sucesso! Obrigado por se inscrever.");
containerEmail.style.display = "none";
containerAll.style.display = "block";
form.reset();
} catch (error) {
console.error("Erro ao enviar o formulário:", error);
alert("Houve um erro ao cadastrar o email. Por favor, tente novamente mais tarde.");
}
});
window.addEventListener("scroll", () => {
const triggerBottom = (window.innerHeight / 5) * 4;
scrollItems.forEach((item) => {
const itemTop = item.getBoundingClientRect().top;
if (itemTop < triggerBottom) {
item.classList.add("show");
} else {
item.classList.remove("show"); // opcional, remove se rolar para cima
}
});
});