-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
97 lines (77 loc) · 2.28 KB
/
script.js
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
89
90
91
92
93
94
95
96
97
const firebaseConfig = {
apiKey: "AIzaSyB7i1nha2fRAiuXpnw4aYc35NfzHWuWWo0",
authDomain: "react-brasil-newsletter.firebaseapp.com",
projectId: "react-brasil-newsletter"
};
const $email = document.getElementById("email");
const $name = document.getElementById("name");
const $wantReceive = document.getElementById("want-receive");
const $submit = document.getElementById("submit");
const $messageSucssesContent = document.getElementById(
"message-sucsses-content"
);
const $messageErrorContent = document.getElementById("message-error-content");
const $additionalSection = document.getElementById("additional-section");
window.onload = function() {
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
$name.focus();
const toggleAdditionalSection = () => {
return $additionalSection.classList.toggle("main__additional--opened");
};
const resetFields = () => {
$name.value = "";
$email.value = "";
};
const success = () => {
resetFields();
const name = $name.value;
$messageErrorContent.textContent = "";
$messageSucssesContent.textContent =
"Que legal, recebemos sua assinatura com sucesso :)";
$name.focus();
};
const error = email => {};
const addEmailToCollection = () => {
const doc = {
email: $email.value,
name: $name.value
};
db
.collection("emails")
.doc(doc.email)
.set(doc)
.then(success)
.catch(error => console.error(error));
};
const emailIsValid = email => {
if (email === "") {
return false;
}
return /^[\w\.\-_]{1,}@[\w\.\-]{6,}/.test(email);
};
const handleClickSubmit = () => {
const email = $email.value;
if ($name.value === "") {
$messageErrorContent.textContent = "Por favor, infome o seu nome!";
$name.focus();
return false;
}
if (emailIsValid(email)) {
addEmailToCollection();
} else {
$messageErrorContent.textContent = "Tivemos problemas com seu e-mail :(";
$email.focus();
return false;
}
};
const handleKeyupEmail = e => {
const ENTER = 13;
if (e.keyCode === ENTER) {
return handleClickToReceive();
}
return e;
};
$submit.addEventListener("click", handleClickSubmit);
$email.addEventListener("keyup", handleKeyupEmail);
};