-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
169 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { navigateTo } from "../router/router.js"; | ||
import { saveLocalStorage } from "../storage/storage.js"; | ||
|
||
export const loginEvents = () => { | ||
const form = document.getElementById("login-form"); | ||
const emailInput = document.getElementById("username"); | ||
const passwordInput = document.getElementById("passwordInput"); | ||
|
||
// 로그인 | ||
form.addEventListener("submit", (e) => { | ||
e.preventDefault(); | ||
const email = emailInput.value.trim(); | ||
const password = passwordInput.value.trim(); | ||
|
||
if (validateForm(email, password)) { | ||
saveLocalStorage("user", { username: email, email: "", bio: "" }); | ||
console.log(localStorage.getItem("user")); | ||
navigateTo("/profile"); | ||
} | ||
}); | ||
|
||
// 유효성 검사 | ||
const validateForm = (email) => { | ||
if (!email) { | ||
alert("이메일을 입력해주세요."); | ||
emailInput.focus(); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { navigateTo } from "../router/router.js"; | ||
import { clearLocalStorage, saveLocalStorage } from "../storage/storage.js"; | ||
|
||
export const mainEvents = () => { | ||
// 헤더 이동 | ||
document.addEventListener("click", (e) => { | ||
const target = e.target.closest("a"); | ||
if (target) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
const href = target.getAttribute("href"); | ||
if (href) { | ||
navigateTo(href); | ||
} | ||
} | ||
}); | ||
|
||
// 프로필 수정 | ||
document.addEventListener("submit", (e) => { | ||
e.preventDefault(); | ||
if (e.target && e.target.id === "profile-form") { | ||
const updatedUser = { | ||
username: document.getElementById("username").value, | ||
email: document.getElementById("email").value, | ||
bio: document.getElementById("bio").value, | ||
}; | ||
|
||
saveLocalStorage("user", updatedUser); | ||
alert("프로필이 업데이트 되었습니다."); | ||
} | ||
}); | ||
|
||
// 로그아웃 | ||
document.addEventListener("click", (e) => { | ||
if (e.target && e.target.id === "logout") { | ||
e.preventDefault(); | ||
clearLocalStorage(); | ||
navigateTo("/login"); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,8 @@ | ||
import { navigateTo, resolveRoute } from "./router.js"; | ||
import { resolveRoute } from "./router/router.js"; | ||
|
||
document.addEventListener("click", (e) => { | ||
if (e.target && e.target.id === "logout") { | ||
e.preventDefault(); | ||
localStorage.clear(); | ||
navigateTo("/login"); | ||
} | ||
}); | ||
|
||
// 이벤트 초기화 | ||
const initializeRouter = () => { | ||
window.addEventListener("hashchange", () => resolveRoute(true)); | ||
window.addEventListener("load", () => resolveRoute(true)); | ||
}; | ||
|
||
// 라우터 초기화 호출 | ||
initializeRouter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,8 @@ | ||
import { navigateTo, resolveRoute } from "./router.js"; | ||
import { resolveRoute } from "./router/router.js"; | ||
|
||
document.addEventListener("click", (e) => { | ||
if (e.target && e.target.id === "logout") { | ||
e.preventDefault(); | ||
localStorage.clear(); | ||
navigateTo("/login"); | ||
} | ||
}); | ||
|
||
// 이벤트 초기화 | ||
const initializeRouter = () => { | ||
window.addEventListener("popstate", () => resolveRoute(false)); | ||
window.addEventListener("load", () => resolveRoute(false)); | ||
}; | ||
|
||
// 라우터 초기화 호출 | ||
initializeRouter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ErrorPage } from "../pages/errorPage.js"; | ||
import { getLocalStorage } from "../storage/storage.js"; | ||
import { eventRegister, Routes } from "./routes.js"; | ||
|
||
// 초기 URL 모드 | ||
let isHashMode = false; | ||
|
||
const getCurrentPath = () => { | ||
if (isHashMode) { | ||
return window.location.hash.slice(1) || "/"; | ||
} else { | ||
return window.location.pathname; | ||
} | ||
}; | ||
|
||
export const resolveRoute = (isHash = false) => { | ||
isHashMode = isHash; | ||
const path = getCurrentPath(); | ||
const user = getLocalStorage("user"); | ||
const root = document.getElementById("root"); | ||
|
||
if (path === "/profile" && !user) { | ||
navigateTo("/login"); | ||
return; | ||
} | ||
if (path === "/login" && user) { | ||
navigateTo("/"); | ||
return; | ||
} | ||
|
||
const route = Routes[path]; | ||
if (route) { | ||
root.innerHTML = route(); | ||
eventRegister(path); | ||
} else { | ||
root.innerHTML = ErrorPage(); | ||
} | ||
}; | ||
|
||
export const navigateTo = (path) => { | ||
if (isHashMode) { | ||
window.location.hash = `#${path}`; | ||
} else { | ||
window.history.pushState(null, null, path); | ||
} | ||
resolveRoute(isHashMode); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { MainPage } from "../pages/mainPage.js"; | ||
import { LoginPage } from "../pages/loginPage.js"; | ||
import { ProfilePage } from "../pages/profilePage.js"; | ||
import { mainEvents } from "../events/mainEvents.js"; | ||
import { loginEvents } from "../events/loginEvents.js"; | ||
|
||
export const PATHS = { | ||
MAIN: "/", | ||
LOGIN: "/login", | ||
PROFILE: "/profile", | ||
}; | ||
|
||
export const Routes = { | ||
[PATHS.MAIN]: () => MainPage(), | ||
[PATHS.LOGIN]: () => LoginPage(), | ||
[PATHS.PROFILE]: () => ProfilePage(), | ||
}; | ||
|
||
// 경로별 이벤트 등록 | ||
export const eventRegister = (path) => { | ||
switch (path) { | ||
case PATHS.MAIN: | ||
case PATHS.PROFILE: | ||
mainEvents(); | ||
break; | ||
case PATHS.LOGIN: | ||
loginEvents(); | ||
break; | ||
default: | ||
console.warn(`No events to register for path: ${path}`); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const saveLocalStorage = (key, value) => { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}; | ||
|
||
export const getLocalStorage = (key) => { | ||
return JSON.parse(localStorage.getItem(key)); | ||
}; | ||
|
||
export const clearLocalStorage = () => { | ||
localStorage.clear(); | ||
}; |