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 = (