forked from kakao-tech-campus-2nd-step3/Team18_FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- LanguageProvider context를 통해 동적 언어 변경 - LanguageFilter 컴포넌트에서 선택된 언어에 따라 언어가 변경되도록 구현
- Loading branch information
Showing
12 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
import { userLocalStorage } from '@/utils/storage'; | ||
import { createContext, ReactNode, useContext, useEffect, useState } from 'react'; | ||
|
||
type LanguageContextType = { | ||
language: string | null; | ||
setLanguage: (language: string | null) => void; | ||
}; | ||
|
||
const LanguageContext = createContext<LanguageContextType | undefined>(undefined); | ||
|
||
export const LanguageProvider = ({ children }: { children: ReactNode }) => { | ||
const [language, setLanguage] = useState<string | null>(() => userLocalStorage.getLanguage()); | ||
|
||
useEffect(() => { | ||
const changeLanguage = () => { | ||
const updatedLanguage = userLocalStorage.getLanguage(); | ||
setLanguage(updatedLanguage); | ||
}; | ||
|
||
window.addEventListener('storage', changeLanguage); | ||
return () => window.removeEventListener('storage', changeLanguage); | ||
}, []); | ||
|
||
return <LanguageContext.Provider value={{ language, setLanguage }}>{children}</LanguageContext.Provider>; | ||
}; | ||
|
||
export const useLanguage = () => { | ||
const context = useContext(LanguageContext); | ||
if (!context) { | ||
throw new Error('useLanguage must be used within a LanguageProvider'); | ||
} | ||
return context; | ||
}; |
This file contains 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,23 +1,29 @@ | ||
import { UserData } from '@/types'; | ||
|
||
const USER_KEY = 'user'; | ||
const TOKEN_KEY = 'token'; | ||
const STORAGE_KEYS = { | ||
USER: 'user', | ||
TOKEN: 'token', | ||
LANGUAGE: 'language', | ||
}; | ||
|
||
export const userLocalStorage = { | ||
getUser: (): UserData | undefined => { | ||
const user = localStorage.getItem(USER_KEY); | ||
const user = localStorage.getItem(STORAGE_KEYS.USER); | ||
return user ? JSON.parse(user) : undefined; | ||
}, | ||
setUser: (user: UserData) => { | ||
localStorage.setItem(USER_KEY, JSON.stringify(user)); | ||
localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(user)); | ||
}, | ||
removeUser: () => { | ||
localStorage.removeItem(USER_KEY); | ||
}, | ||
getToken: (): string | null => { | ||
return localStorage.getItem(TOKEN_KEY); | ||
localStorage.removeItem(STORAGE_KEYS.USER); | ||
}, | ||
|
||
removeToken: () => { | ||
localStorage.removeItem(TOKEN_KEY); | ||
localStorage.removeItem(STORAGE_KEYS.TOKEN); | ||
}, | ||
|
||
getLanguage: (): string | null => { | ||
const language = localStorage.getItem(STORAGE_KEYS.LANGUAGE); | ||
return language ? JSON.parse(language).state.selectedOption.value : null; | ||
}, | ||
}; |