From 2dc8d03de020cc96967db8e396d0cd39b3543559 Mon Sep 17 00:00:00 2001 From: Geunbaek Date: Wed, 18 Dec 2024 04:59:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=9C=84?= =?UTF-8?q?=EC=9E=84=20=EC=B2=98=EB=A6=AC=20&=20isHash=20=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EB=B6=84=EA=B8=B0=20&=20unmount=20?= =?UTF-8?q?=EC=8B=9C=20unsubscribe=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/login-form-component.jsx | 23 +++++-- src/components/nav-component.jsx | 76 ++++++++++++++--------- src/components/profile-form-component.jsx | 27 +++++++- src/pages/login-page.jsx | 14 +++++ src/pages/profile-page.jsx | 20 +----- 5 files changed, 105 insertions(+), 55 deletions(-) diff --git a/src/components/login-form-component.jsx b/src/components/login-form-component.jsx index 3b964998..5be5f20e 100644 --- a/src/components/login-form-component.jsx +++ b/src/components/login-form-component.jsx @@ -6,19 +6,30 @@ class LoginFormComponent extends HTMLElement { super(); } + addEvent() { + const loginForm = this.querySelector("#login-form"); + + this.addEventListener("submit", (event) => { + event.preventDefault(); + + if (event.target === loginForm) { + this.handleLogin(); + } + }); + } + connectedCallback() { this.render(); } - handleLogin(event) { - event.preventDefault(); + handleLogin() { const username = this.querySelector("#username").value; if (!username) { return; } - navigateTo("/"); + navigateTo("/", { hash: window.isHash }); const user = { username, @@ -31,13 +42,13 @@ class LoginFormComponent extends HTMLElement { render() { const element = ( -
+
@@ -50,6 +61,7 @@ class LoginFormComponent extends HTMLElement { /> + ) : (
  • - + 로그인
  • @@ -89,6 +105,8 @@ class NavComponent extends HTMLElement { } else { this.appendChild(element); } + + this.addEvent(); } } diff --git a/src/components/profile-form-component.jsx b/src/components/profile-form-component.jsx index 50bdcbf1..6e9951ab 100644 --- a/src/components/profile-form-component.jsx +++ b/src/components/profile-form-component.jsx @@ -1,17 +1,36 @@ import { userStore, userStoreActions } from "../stores/userStore"; +import { navigateTo } from "../utils/router"; class ProfileFormComponent extends HTMLElement { constructor() { super(); + userStore.subscribe(this.render.bind(this)); + } + + addEvent() { + const profileForm = this.querySelector("#profile-form"); + + const handleSubmit = (event) => { + if (event.target === profileForm) { + event.preventDefault(); + + this.handleUpdateProfile(); + } + }; + + this.removeEventListener("submit", handleSubmit); + this.addEventListener("submit", handleSubmit); } connectedCallback() { this.render(); - userStore.subscribe(this.render.bind(this)); } - handleUpdateProfile(event) { - event.preventDefault(); + disconnectedCallback() { + userStore.unsubscribe(this.render.bind(this)); + } + + handleUpdateProfile() { const username = this.querySelector("#username").value; const email = this.querySelector("#email").value; const bio = this.querySelector("#bio").value; @@ -23,6 +42,7 @@ class ProfileFormComponent extends HTMLElement { }; userStoreActions.updateUser(user); + navigateTo("/profile"); } render() { @@ -85,6 +105,7 @@ class ProfileFormComponent extends HTMLElement { } else { this.appendChild(element); } + this.addEvent(); } } diff --git a/src/pages/login-page.jsx b/src/pages/login-page.jsx index 41e7d469..cef6681b 100644 --- a/src/pages/login-page.jsx +++ b/src/pages/login-page.jsx @@ -1,13 +1,27 @@ +import { userStore } from "../stores/userStore"; +import { navigateTo } from "../utils/router"; + class LoginPage extends HTMLElement { constructor() { super(); + userStore.subscribe(this.render.bind(this)); } connectedCallback() { this.render(); } + disconnectedCallback() { + userStore.unsubscribe(this.render.bind(this)); + } + render() { + const user = userStore.getState(); + + if (user.username) { + navigateTo(window.isHash ? "#/" : "/", { hash: window.isHash }); + } + const element = (
    diff --git a/src/pages/profile-page.jsx b/src/pages/profile-page.jsx index da5bca62..36ddad87 100644 --- a/src/pages/profile-page.jsx +++ b/src/pages/profile-page.jsx @@ -1,38 +1,22 @@ -import { userStore, userStoreActions } from "../stores/userStore"; +import { userStore } from "../stores/userStore"; import { navigateTo } from "../utils/router"; class ProfilePage extends HTMLElement { constructor() { super(); - userStore.subscribe(this.render.bind(this)); } connectedCallback() { const user = userStore.getState(); if (!user?.username) { - navigateTo("/login"); + navigateTo("/login", { hash: window.isHash }); return; } this.render(); } - handleUpdateProfile(event) { - event.preventDefault(); - const username = this.querySelector("#username").value; - const email = this.querySelector("#email").value; - const bio = this.querySelector("#bio").value; - - const user = { - username, - email, - bio, - }; - - userStoreActions.updateUser(user); - } - render() { const element = (