Refactor Dashboard component to implement a structured routing system with dedicated pages for workplace input, login, chain selection, task management, and completion. Introduce centralized localStorage management for user data and navigation logic, enhancing user experience and streamlining the application flow. Remove the deprecated LoginForm component and update the MainPage to redirect users based on their authentication and task status.
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit
This commit is contained in:
113
src/utils/storage.ts
Normal file
113
src/utils/storage.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Централизованная работа с localStorage
|
||||
* Все ключи и операции в одном месте
|
||||
*/
|
||||
|
||||
const isBrowser = () => typeof window !== 'undefined'
|
||||
|
||||
// Ключи localStorage
|
||||
export const STORAGE_KEYS = {
|
||||
USER_ID: 'challengeUserId',
|
||||
NICKNAME: 'challengeNickname',
|
||||
WORKPLACE_NUMBER: 'challengeWorkplaceNumber',
|
||||
SELECTED_CHAIN_ID: 'challengeSelectedChainId',
|
||||
SELECTED_TASK_ID: 'challengeSelectedTaskId',
|
||||
} as const
|
||||
|
||||
// Получение значений
|
||||
export const storage = {
|
||||
getUserId: (): string | null => {
|
||||
if (!isBrowser()) return null
|
||||
return localStorage.getItem(STORAGE_KEYS.USER_ID)
|
||||
},
|
||||
|
||||
getNickname: (): string | null => {
|
||||
if (!isBrowser()) return null
|
||||
return localStorage.getItem(STORAGE_KEYS.NICKNAME)
|
||||
},
|
||||
|
||||
getWorkplaceNumber: (): string | null => {
|
||||
if (!isBrowser()) return null
|
||||
return localStorage.getItem(STORAGE_KEYS.WORKPLACE_NUMBER)
|
||||
},
|
||||
|
||||
getSelectedChainId: (): string | null => {
|
||||
if (!isBrowser()) return null
|
||||
return localStorage.getItem(STORAGE_KEYS.SELECTED_CHAIN_ID)
|
||||
},
|
||||
|
||||
getSelectedTaskId: (): string | null => {
|
||||
if (!isBrowser()) return null
|
||||
return localStorage.getItem(STORAGE_KEYS.SELECTED_TASK_ID)
|
||||
},
|
||||
|
||||
// Установка значений
|
||||
setUserId: (value: string): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.setItem(STORAGE_KEYS.USER_ID, value)
|
||||
},
|
||||
|
||||
setNickname: (value: string): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.setItem(STORAGE_KEYS.NICKNAME, value)
|
||||
},
|
||||
|
||||
setWorkplaceNumber: (value: string): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.setItem(STORAGE_KEYS.WORKPLACE_NUMBER, value)
|
||||
},
|
||||
|
||||
setSelectedChainId: (value: string): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.setItem(STORAGE_KEYS.SELECTED_CHAIN_ID, value)
|
||||
},
|
||||
|
||||
setSelectedTaskId: (value: string): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.setItem(STORAGE_KEYS.SELECTED_TASK_ID, value)
|
||||
},
|
||||
|
||||
// Удаление значений
|
||||
removeUserId: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_ID)
|
||||
},
|
||||
|
||||
removeNickname: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.NICKNAME)
|
||||
},
|
||||
|
||||
removeWorkplaceNumber: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.WORKPLACE_NUMBER)
|
||||
},
|
||||
|
||||
removeSelectedChainId: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_CHAIN_ID)
|
||||
},
|
||||
|
||||
removeSelectedTaskId: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_TASK_ID)
|
||||
},
|
||||
|
||||
// Полная очистка при выходе
|
||||
clearAll: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.USER_ID)
|
||||
localStorage.removeItem(STORAGE_KEYS.NICKNAME)
|
||||
localStorage.removeItem(STORAGE_KEYS.WORKPLACE_NUMBER)
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_CHAIN_ID)
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_TASK_ID)
|
||||
},
|
||||
|
||||
// Очистка данных сессии (цепочка, задание) без выхода
|
||||
clearSessionData: (): void => {
|
||||
if (!isBrowser()) return
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_CHAIN_ID)
|
||||
localStorage.removeItem(STORAGE_KEYS.SELECTED_TASK_ID)
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user