-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrettings.js
More file actions
46 lines (38 loc) · 1 KB
/
grettings.js
File metadata and controls
46 lines (38 loc) · 1 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
const form = document.querySelector(".js-form"),
input = form.querySelector("input"),
greetings = document.querySelector(".js-greetings");
const USER_LS = "currentUser",
SHOWING_CN = "showing";
function saveName(text) {
localStorage.setItem(USER_LS, text);
console.log("Item's saved");
}
function handleSubmit(event) {
event.preventDefault();
const currentValue = input.value;
paintGreeting(currentValue);
saveName(currentValue);
form.classList.remove(SHOWING_CN);
}
function askForName() {
form.classList.add(SHOWING_CN);
form.addEventListener("submit", handleSubmit);
}
function paintGreeting(text) {
form.classList.remove(SHOWING_CN);
greetings.classList.add(SHOWING_CN);
greetings.innerText = `Hello ${text}`;
}
function loadName() {
const currentUser = localStorage.getItem(USER_LS);
if (currentUser === null) {
askForName();
} else {
document.querySelector(".form").style.display = "none";
paintGreeting(currentUser);
}
}
function init() {
loadName();
}
init();