-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreeting.js
66 lines (56 loc) · 1.6 KB
/
greeting.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
const clockContainer = document.querySelector(".js-clock"),
clockTitle = clockContainer.querySelector(".currentTime");
const form = document.querySelector(".js-form"),
input = form.querySelector("input"),
greeting = document.querySelector(".js-greetings");
const USER_LS = "currentUser",
SHOWING_CN = "showing";
function getTime() {
const date = new Date();
const minutes = date.getMinutes();
const hours = date.getHours();
const seconds = date.getSeconds();
clockTitle.innerText = `${hours < 10 ? `0${hours}` : hours}:${
minutes < 10 ? `0${minutes}` : minutes
}:${seconds < 10 ? `0${seconds}` : seconds}`;
}
function saveName(text) {
localStorage.setItem(USER_LS, text);
}
function handleSubmit(event) {
event.preventDefault();
const currentValue = input.value;
paintGreeting(currentValue);
saveName(currentValue);
}
function askForName() {
form.classList.add(SHOWING_CN);
form.addEventListener("submit", handleSubmit);
}
function paintGreeting(text) {
form.classList.remove(SHOWING_CN);
greeting.classList.add(SHOWING_CN);
if (21 <= getTime.hours || getTime.hours < 6) {
greeting.innerText = `Good moring, ${text}`;
} else if (6 <= getTime.hours || getTime.hours < 12) {
greeting.innerText = `Good evening, ${text}`;
} else {
greeting.innerText = `Good afternoon, ${text}`;
}
}
function loadName() {
const currentUser = localStorage.getItem(USER_LS);
if (currentUser === null) {
askForName();
// he is not
} else {
// he is
paintGreeting(currentUser);
}
}
function init() {
loadName();
getTime();
setInterval(getTime, 1000);
}
init();