-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreeting.js
More file actions
59 lines (50 loc) · 1.55 KB
/
greeting.js
File metadata and controls
59 lines (50 loc) · 1.55 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
const greetingForm = document.querySelector(".js-form");
const greetingInput = greetingForm.querySelector("input");
const greetings = document.querySelector(".js-greetings");
const USER_LS = "currentUser";
const SHOWING_CN = "showing";
init();
function init() {
loadName();
}
function loadName() {
const currentUser = localStorage.getItem(USER_LS);
if(currentUser === null) {
askForName();
} else {
paintGreeting(currentUser);
}
}
function askForName() {
greetingForm.classList.add(SHOWING_CN);
greetingForm.addEventListener("submit", formSubmit);
}
function formSubmit(e) {
e.preventDefault();
const inputValue = greetingInput.value;
paintGreeting(inputValue);
saveNameAtLocalStorage(inputValue);
}
function paintGreeting(inputValue) {
greetingForm.classList.remove(SHOWING_CN);
greetings.innerHTML = `안녕하세요. ${inputValue}님 `;
greetings.classList.add(SHOWING_CN);
const tagI = document.createElement("i");
tagI.classList.add("fa");
tagI.classList.add("fa-refresh");
tagI.classList.add("fa-spin");
tagI.classList.add("fa-3x");
tagI.addEventListener("click", refreshName);
const delBtn = document.createElement("button");
delBtn.appendChild(tagI);
greetings.appendChild(delBtn);
}
function saveNameAtLocalStorage(inputValue) {
localStorage.setItem(USER_LS, inputValue);
}
function refreshName(e) {
localStorage.removeItem(USER_LS);
greetingInput.value = "";
greetingForm.classList.add(SHOWING_CN);
greetings.classList.remove(SHOWING_CN);
}