<!DOCTYPE html>
<html>
<head>
<title>Something</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="js-clock">
<div class="js-title">
<h1>00:00</h1>
</div>
</div>
<form class="js-form form">
<input type="text" placeholder="What is your name?" />
</form>
<h4 class="js-greetings greetings"></h4>
<script src="clock.js"></script>
<script src="greeting.js"></script>
</body>
</html>
body {
color: #34495e;
}
.form,
.greetings {
display: none;
}
.showing {
display: block;
}
const form = document.querySelector(".js-form"),
input = form.querySelector("input"),
greeting = document.querySelector(".js-greetings");
const USER_LS = "currentUser", //LocalStorage
SHOWING_CN = "showing"; //ClassName
function saveName(text){
localStorage.setItem(USER_LS, text);
}
function handleSubmit(event){
event.preventDefault(); //event의 기본값 삭제
const currentValue = input.value; //value 값 가져오기
paintGreeting(currentValue);
saveName(currentValue);
}
function askForName(){
form.classList.add(SHOWING_CN);
form.addEventListener("submit", handleSubmit);
}
function paintGreeting(text){
form.classList.remove(SHOWING_CN); //유저 등록 되어있는 상태로 form을 숨겨줘야 함
greeting.classList.add(SHOWING_CN);
greeting.innerText = `Hello ${text}`;
}
function loadName(){
const currentUser = localStorage.getItem(USER_LS); //LS에서 데이터가져오기
if(currentUser === null){
askForName();
} else {
paintGreeting(currentUser);
}
}
function init(){
loadName();
}
init();