Skip to content

Commit

Permalink
feat: 이벤트 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jungenvDev committed Dec 18, 2024
1 parent 941fa52 commit d12c8fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createRouter } from "./router/router";
import { setupEventListeners } from "./managers/eventManager.js";

const root = document.getElementById("root");
createRouter(root);
const { navigate } = createRouter(root);
setupEventListeners(navigate);
44 changes: 44 additions & 0 deletions src/managers/eventManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const setupEventListeners = (navigate) => {
document.addEventListener("click", (e) => {
if (e.target.id === "logout") {
e.preventDefault();
localStorage.removeItem("user");
navigate("/login");
return;
}

if (e.target.matches("[data-link]")) {
e.preventDefault();
navigate(e.target.getAttribute("href"));
}
});

document.addEventListener("submit", (e) => {
if (e.target.id === "profile-form") {
e.preventDefault();
const formData = new FormData(e.target);
const user = JSON.parse(localStorage.getItem("user") || "{}");
const updatedUser = {
username: formData.get("username") || user.username,
email: formData.get("email") || user.email,
bio: formData.get("bio") || user.bio,
};
localStorage.setItem("user", JSON.stringify(updatedUser));
navigate("/profile");
}

if (e.target.id === "login-form") {
e.preventDefault();
const username = e.target.querySelector("#username").value;
localStorage.setItem(
"user",
JSON.stringify({
username,
email: "",
bio: "",
}),
);
navigate("/");
}
});
};
47 changes: 3 additions & 44 deletions src/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const createRouter = (root) => {
const handleRoute = () => {
const path = window.location.pathname;

//라우트 가드
if (path === "/profile" && !localStorage.getItem("user")) {
navigate("/login");
return;
Expand All @@ -29,54 +30,12 @@ export const createRouter = (root) => {
};

const navigate = (path) => {
// 브라우저 url 업데이트
window.history.pushState({}, "", path);
// 페이지 렌더링
handleRoute();
};

// 전역 이벤트 리스너 등록
document.addEventListener("click", (e) => {
if (e.target.id === "logout") {
e.preventDefault();
localStorage.removeItem("user");
navigate("/login");
return;
}

if (e.target.matches("[data-link]")) {
e.preventDefault();
navigate(e.target.getAttribute("href"));
}
});

document.addEventListener("submit", (e) => {
if (e.target.id === "profile-form") {
e.preventDefault();
const formData = new FormData(e.target);
const user = JSON.parse(localStorage.getItem("user") || "{}");
const updatedUser = {
username: formData.get("username") || user.username,
email: formData.get("email") || user.email,
bio: formData.get("bio") || user.bio,
};
localStorage.setItem("user", JSON.stringify(updatedUser));
navigate("/profile");
}

if (e.target.id === "login-form") {
e.preventDefault();
const username = e.target.querySelector("#username").value;
localStorage.setItem(
"user",
JSON.stringify({
username,
email: "",
bio: "",
}),
);
navigate("/");
}
});

window.addEventListener("popstate", handleRoute);
handleRoute();

Expand Down

0 comments on commit d12c8fb

Please sign in to comment.