Skip to content

Commit

Permalink
refactor: 이벤트 함수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
장원정 committed Dec 17, 2024
1 parent d49115f commit 2a6505b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
import { createRouter } from "@/router/createRouter";
import UserStore from "@/store/userStore";
import { submitEventHandler, clickEventHandler } from "./utils";

const { router, navigator } = createRouter();

function submitEventHandler(e) {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const { id } = form;

if (id === "login-form") {
login(formData);
}

if (id === "profile-form") {
updateProfile(formData);
}
}

function login(formData) {
const username = formData.get("username");
if (username) {
new UserStore().setUser({ username, email: "", bio: "" });
navigator("/profile");
}
}

function updateProfile(formData) {
const username = formData.get("username");
const email = formData.get("email");
const bio = formData.get("bio");
new UserStore().setUser({ username, email, bio });
navigator("/profile");
}

function clickEventHandler(e) {
const { id, tagName } = e.target;

if (tagName === "A") {
e.preventDefault();
const { href } = e.target;
let path = new URL(href).pathname;
if (id === "logout") {
new UserStore().deleteUser();
path = "/login";
}
navigator(path);
}
}
const { router } = createRouter();

document.body.addEventListener("submit", submitEventHandler);
document.body.addEventListener("click", clickEventHandler);
Expand Down
19 changes: 19 additions & 0 deletions src/utils/clickEventHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createRouter } from "@/router/createRouter";
import UserStore from "@/store/userStore";

const { navigator } = createRouter();

export function clickEventHandler(e) {
const { id, tagName } = e.target;

if (tagName === "A") {
e.preventDefault();
const { href } = e.target;
let path = new URL(href).pathname;
if (id === "logout") {
new UserStore().deleteUser();
path = "/login";
}
navigator(path);
}
}
2 changes: 2 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./clickEventHandler";
export * from "./submitEventHandler";
35 changes: 35 additions & 0 deletions src/utils/submitEventHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createRouter } from "@/router/createRouter";
import UserStore from "@/store/userStore";

const { navigator } = createRouter();

export function submitEventHandler(e) {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const { id } = form;

if (id === "login-form") {
login(formData);
}

if (id === "profile-form") {
updateProfile(formData);
}
}

function login(formData) {
const username = formData.get("username");
if (username) {
new UserStore().setUser({ username, email: "", bio: "" });
navigator("/profile");
}
}

function updateProfile(formData) {
const username = formData.get("username");
const email = formData.get("email");
const bio = formData.get("bio");
new UserStore().setUser({ username, email, bio });
navigator("/profile");
}

0 comments on commit 2a6505b

Please sign in to comment.