-
Notifications
You must be signed in to change notification settings - Fork 1
๐ fix: ์๋ฆผ ์ฝ์ ๊ธฐ๋ฅ ์์ ๋ฐ div ํ๊ทธ๋ฅผ link๋ก ์์ #148
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { createContext, useState, useEffect, type ReactNode } from 'react'; | ||
| import { getAlerts, type AlertViewResponse } from '@/api/alertApi'; | ||
| import { getAlerts, putAlerts, type AlertViewResponse } from '@/api/alertApi'; | ||
| type UserRole = 'employer' | 'employee' | null; | ||
|
|
||
| interface AuthContextType { | ||
|
|
@@ -8,6 +8,7 @@ interface AuthContextType { | |
| alarms: AlertViewResponse; // ์๋ฆผ ์ ๋ณด | ||
| login: (token: string, role: UserRole, userId: string) => void; // ๋ก๊ทธ์ธ ํจ์ | ||
| logout: () => void; // ๋ก๊ทธ์์ ํจ์ | ||
| markAlertAsRead: (alertId: string) => void; | ||
| } | ||
|
|
||
| const defaultAuthContext: AuthContextType = { | ||
|
|
@@ -23,6 +24,7 @@ const defaultAuthContext: AuthContextType = { | |
| }, | ||
| login: () => {}, | ||
| logout: () => {}, | ||
| markAlertAsRead: () => {}, | ||
| }; | ||
|
|
||
| export const AuthContext = createContext<AuthContextType>(defaultAuthContext); | ||
|
|
@@ -35,12 +37,24 @@ export function AuthProvider({ children }: { children: ReactNode }) { | |
| ); | ||
|
|
||
| useEffect(() => { | ||
| const token = localStorage.getItem('accessToken'); | ||
| const role = localStorage.getItem('userRole') as UserRole; | ||
| if (token && role) { | ||
| setIsLoggedIn(true); | ||
| setRole(role); | ||
| } | ||
| const fetchAuth = async () => { | ||
| const token = localStorage.getItem('accessToken'); | ||
| const userRole = localStorage.getItem('userRole') as UserRole; | ||
| const userId = localStorage.getItem('userId'); | ||
|
|
||
| if (token && userRole && userId) { | ||
| setIsLoggedIn(true); | ||
| setRole(userRole); | ||
| try { | ||
| const alertRes = await getAlerts(userId); | ||
| setAlarms(alertRes); | ||
| } catch (error) { | ||
| console.error('์ฑ ๋ก๋ฉ ์ค ์๋ฆผ ๊ฐ์ ธ์ค๊ธฐ ์คํจ:', error); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| fetchAuth(); | ||
| }, []); | ||
|
|
||
| const login = async (token: string, role: UserRole, userId: string) => { | ||
|
|
@@ -66,8 +80,29 @@ export function AuthProvider({ children }: { children: ReactNode }) { | |
| setAlarms(defaultAuthContext.alarms); | ||
| }; | ||
|
|
||
| const markAlertAsRead = async (alertId: string) => { | ||
| const userId = localStorage.getItem('userId'); | ||
| if (!userId) { | ||
| console.error('์ฝ์ ์ฒ๋ฆฌ ์คํจ: userId๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.'); | ||
| return; | ||
| } | ||
|
Comment on lines
+85
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ฌ ๋ก๊ทธ์ธ์ด ํ์ํ๋ค๊ณ ๋จ๊ฒจ๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์๊ณ ์์ ํ์ด์ผ๋๋๋ฐ ๋จธ์งํด๋ฒ๋ ธ๋ค์ ๋์ค์ ์์ ์ฌํญ ์์๋ ํจ๊ป ์์ ํ๊ฒ ์ต๋๋ค |
||
|
|
||
| setAlarms((prevAlarms) => ({ | ||
| ...prevAlarms, | ||
| items: prevAlarms.items.filter((alert) => alert.item.id !== alertId), | ||
| })); | ||
|
|
||
| try { | ||
| await putAlerts(userId, alertId); | ||
| } catch (error) { | ||
| console.error('API - ์๋ฆผ ์ฝ์ ์ฒ๋ฆฌ ์คํจ:', error); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <AuthContext.Provider value={{ isLoggedIn, role, alarms, login, logout }}> | ||
| <AuthContext.Provider | ||
| value={{ isLoggedIn, role, alarms, login, logout, markAlertAsRead }} | ||
| > | ||
| {children} | ||
| </AuthContext.Provider> | ||
| ); | ||
|
|
||
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.
๐ฌ ๋ค ํ์ด์ ๋ฐ๊ธฐ๋ณด๋จ AlertItem์ ๊ฐ์ฒด ํํ..? ๋ก prop์ ๋ฐ์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค