-
Notifications
You must be signed in to change notification settings - Fork 5
Feature/ user store - zustand 기반 전역 유저 상태 관리 #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b114256
feat: zustand userStore 생성
youdaeng2 4909fb0
feat: 처음 진입 시 로그인 돼있으면 유저 정보 받아오는 hook 생성 및 _app.tsx에 배치
youdaeng2 7285d8a
feat: 전역 유저 상태에 접근할 수 있는 커스텀 hook 생성
youdaeng2 b994881
Merge branch 'dev' into feature/user-store
youdaeng2 349aff2
chore: 불필요 icon 파일 삭제
youdaeng2 5800fd1
fix: star.svg 에러 처리(새 파일로 변경 과정)
youdaeng2 45efac1
fix: star.svg 에러 처리(새 파일로 변경)
youdaeng2 eb478ea
fix: star.svg 에러 처리(새 파일로 변경 과정)
youdaeng2 4bf114a
fix: star.svg 에러 처리(새 파일로 변경 과정)
youdaeng2 d1ab3d5
fix: star.svg 에러 처리(svg 캐시 삭제)
youdaeng2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { useEffect } from 'react'; | ||
| import { getUser } from '@/api/user'; | ||
| import { useUserStore } from '@/stores/userStore'; | ||
|
|
||
| /** | ||
| * 앱 진입 시 유저 정보를 패치하고 Zustand에 저장하는 훅 */ | ||
| export const useInitUser = () => { | ||
| const setUser = useUserStore((state) => state.setUser); | ||
|
|
||
| /* 처음에만 실행 */ | ||
| useEffect(() => { | ||
| const fetchUser = async () => { | ||
| try { | ||
| const user = await getUser(); | ||
| setUser(user); | ||
| } catch (error) { | ||
| // 로그인 안 돼있는 경우 무시 | ||
| } | ||
| }; | ||
|
|
||
| fetchUser(); | ||
| }, [setUser]); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { useUserStore } from '@/stores/userStore'; | ||
|
|
||
| /** | ||
| * 전역 유저 상태를 쉽게 접근할 수 있는 커스텀 훅 */ | ||
| export const useUser = () => { | ||
| const user = useUserStore((state) => state.user); | ||
| const isLoggedIn = useUserStore((state) => state.isLoggedIn); | ||
| const setUser = useUserStore((state) => state.setUser); | ||
| const clearUser = useUserStore((state) => state.clearUser); | ||
|
|
||
| return { user, isLoggedIn, setUser, clearUser }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { create } from 'zustand'; | ||
|
|
||
| /* 유저 정보 타입 */ | ||
| interface User { | ||
| id: number; | ||
| nickname: string; | ||
| image: string | null; | ||
| teamId: string; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| } | ||
|
|
||
| /* Zustand 유저 상태 저장소 타입 */ | ||
| interface UserStore { | ||
| user: User | null; | ||
| isLoggedIn: boolean; | ||
|
|
||
| /* 유저 정보 설정 (로그인 등) */ | ||
| setUser: (user: User) => void; | ||
|
|
||
| /* 유저 정보 초기화 (로그아웃 등) */ | ||
| clearUser: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * 유저 상태 전역 스토어 */ | ||
| export const useUserStore = create<UserStore>((set) => ({ | ||
| user: null, | ||
| isLoggedIn: false, | ||
| setUser: (user) => set({ user, isLoggedIn: true }), | ||
| clearUser: () => set({ user: null, isLoggedIn: false }), | ||
| })); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
머지 후 로그인 시 추가하도록 하겠습니다!