-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
50 lines (41 loc) · 1.17 KB
/
user.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
const userDiv = document.querySelector(".js-user"),
userForm = userDiv.querySelector(".js-form"),
userInput = userForm.querySelector("input"),
greetingsDiv = document.querySelector(".js-greetings"),
greetingsText = greetingsDiv.querySelector("h3"),
jsToDo = document.querySelector(".js-toDo");
const USER_LS = "user",
GREETINGS_LS = "greetings",
USGR_LS = "usgr";
function askForName(){
userDiv.classList.remove(USER_LS);
userForm.addEventListener("submit", handleUserForm);
}
function loadUser(){
loadedUser = localStorage.getItem(USER_LS);
if (loadedUser === null){
askForName();
}else{
paintGreetings(loadedUser);
}
}
function saveName(text){
localStorage.setItem(USER_LS, text);
text = '';
}
function paintGreetings(text){
greetingsDiv.classList.remove(GREETINGS_LS);
jsToDo.classList.remove(USGR_LS);
userDiv.classList.add(USER_LS);
greetingsText.innerHTML = `안녕하세요. ${text}님!`
}
function handleUserForm(e){
e.preventDefault();
const inputValue = userInput.value;
paintGreetings(inputValue);
saveName(inputValue);
}
function init(){
loadUser();
}
init();