Skip to content

Commit

Permalink
refactor: userStore 클래스 문법 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
장원정 committed Dec 19, 2024
1 parent 99fd013 commit 45ff34a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/components/layout/Header.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import userStore from "@/store/userStore";

const Header = () => {
const isLogin = !!userStore.getUser();
const isLogin = userStore.isLogin();
const path = window.location.pathname;
// 석호님 코드 따라하기
const activeStyle = "text-blue-600 font-bold";
Expand Down
12 changes: 6 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ addRoutes(
);

function validateRouteUser(path) {
const user = userStore.getUser();
if (!user && path === "/profile") path = "/login";
if (user && path === "/login") path = "/";
const isLogin = userStore.isLogin();
if (!isLogin && path === "/profile") path = "/login";
if (isLogin && path === "/login") path = "/";
return path;
}

function validateHashRouteUser(hash) {
const user = userStore.getUser();
if (!user && hash === "#/profile") hash = "#/login";
if (user && hash === "#/login") hash = "#/";
const isLogin = userStore.isLogin();
if (!isLogin && hash === "#/profile") hash = "#/login";
if (isLogin && hash === "#/login") hash = "#/";
return hash;
}

Expand Down
47 changes: 22 additions & 25 deletions src/store/userStore.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
class UserStore {
constructor() {
if (UserStore.instance) {
return UserStore.instance;
}
UserStore.instance = this;
this.user = localStorage.getItem("user")
? JSON.parse(localStorage.getItem("user"))
: null;
}
let user = JSON.parse(localStorage.getItem("user"));

setUser(user) {
localStorage.setItem("user", JSON.stringify(user));
this.user = user;
}
function setUser(newUser) {
localStorage.setItem("user", JSON.stringify(newUser));
user = newUser;
}

deleteUser() {
localStorage.removeItem("user");
this.user = null;
}
function deleteUser() {
localStorage.removeItem("user");
user = null;
}

getUser() {
return this.user;
}
function getUser() {
return user;
}

isLogin() {
return !!this.user;
}
function isLogin() {
return !!user;
}

export default new UserStore();
const userStore = {
setUser,
deleteUser,
getUser,
isLogin,
};

export default userStore;

0 comments on commit 45ff34a

Please sign in to comment.