-
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.
redux types, first slice, hook useLocalstorage, some adaptive fix
- Loading branch information
1 parent
c3a2027
commit a3ae283
Showing
30 changed files
with
290 additions
and
197 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 |
---|---|---|
@@ -1 +1,5 @@ | ||
export interface StateSchema {} | ||
import { SpaceState } from '@/shared/ui/SpaceCanvas'; | ||
|
||
export interface StateSchema { | ||
spaceCanvas: SpaceState; | ||
} |
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,17 +1,15 @@ | ||
import { spaceSlice } from '@/shared/ui/SpaceCanvas'; | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { StateSchema } from './StateSchema'; | ||
|
||
export const createReduxStore = (initialState?: StateSchema) => { | ||
return configureStore<StateSchema>({ | ||
reducer: {}, | ||
reducer: { | ||
spaceCanvas: spaceSlice.reducer, | ||
}, | ||
// devTools: IS_DEV, | ||
preloadedState: initialState, | ||
}); | ||
}; | ||
|
||
// Infer the `RootState` and `AppDispatch` types from the store itself | ||
// export type RootState = ReturnType<typeof store.getState>; | ||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} | ||
// export type AppDispatch = typeof store.dispatch; | ||
|
||
export type AppDispatch = ReturnType<typeof createReduxStore>['dispatch']; |
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,4 +1,5 @@ | ||
export enum LocalstorageKeys { | ||
LIKED = 'liked-products-id', | ||
HISTORY = 'history-products-id', | ||
SPACE = 'space-background', | ||
} |
2 changes: 1 addition & 1 deletion
2
...b/hooks/useStorageData/getStorageData.tsx → ...b/hooks/getStorageData/getStorageData.tsx
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,4 +1,5 @@ | ||
export { getStorageData } from './getStorageData/getStorageData'; | ||
export { useCurrency } from './useCurrency/useCurrency'; | ||
export type { Currency } from './useCurrency/useCurrency'; | ||
export { getStorageData } from './useStorageData/getStorageData'; | ||
export { useStorageData } from './useStorageData/useStorageData'; | ||
export { useLocalstorage } from './useLocalstorage/useLocalstorage'; | ||
export { useLocalstorageArray } from './useLocalstorageArray/useLocalstorageArray'; |
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,30 @@ | ||
'use client'; | ||
|
||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import { LocalstorageKeys } from '@/shared/const/localstorage'; | ||
import { useState } from 'react'; | ||
|
||
export const useLocalstorage = <T>( | ||
key: LocalstorageKeys, | ||
initialValue: T, | ||
): [T, (value: T) => void] => { | ||
const isBrowser = typeof window !== 'undefined'; | ||
if (!isBrowser) { | ||
return [initialValue, () => {}]; | ||
} | ||
|
||
// Попытка получить сохраненное значение из localStorage | ||
const storedValue = localStorage.getItem(key); | ||
const initial = storedValue ? JSON.parse(storedValue) : initialValue; | ||
|
||
// Создание состояния для значения и функции для его обновления | ||
const [value, updateValue] = useState<T>(initial); | ||
|
||
// Функция для обновления значения в состоянии и сохранения его в localStorage | ||
const setValue = (newValue: T) => { | ||
updateValue(newValue); | ||
localStorage.setItem(key, JSON.stringify(newValue)); | ||
}; | ||
|
||
return [value, setValue]; | ||
}; |
60 changes: 60 additions & 0 deletions
60
src/shared/lib/hooks/useLocalstorageArray/useLocalstorageArray.ts
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,60 @@ | ||
'use client'; | ||
|
||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import { LocalstorageKeys } from '@/shared/const/localstorage'; | ||
import { MouseEvent, useState } from 'react'; | ||
|
||
export const useLocalstorageArray = <T>( | ||
key: LocalstorageKeys, | ||
initialValue: T, | ||
) => { | ||
const isBrowser = typeof window !== 'undefined'; | ||
if (!isBrowser) { | ||
return [initialValue, () => {}]; | ||
} | ||
|
||
const [isAdded, setIsAdded] = useState<boolean>(() => { | ||
const storedItems: T[] = JSON.parse(localStorage.getItem(key) || '[]'); | ||
|
||
return storedItems.includes(initialValue); | ||
}); | ||
|
||
const updateLocalStorage = (products: T[]) => { | ||
localStorage.setItem(key, JSON.stringify(products)); | ||
setIsAdded(!isAdded); | ||
}; | ||
|
||
const toggle = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
|
||
const storedItems: T[] = JSON.parse(localStorage.getItem(key) || '[]'); | ||
|
||
const updatedItems = isAdded | ||
? storedItems.filter((item) => item !== initialValue) | ||
: [...storedItems, initialValue]; | ||
|
||
updateLocalStorage(updatedItems); | ||
}; | ||
|
||
const add = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
|
||
const storedItems: T[] = JSON.parse(localStorage.getItem(key) || '[]'); | ||
|
||
if (!isAdded) { | ||
const updatedItems = [...storedItems, initialValue]; | ||
updateLocalStorage(updatedItems); | ||
} | ||
}; | ||
|
||
const remove = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
|
||
const storedItems: T[] = JSON.parse(localStorage.getItem(key) || '[]'); | ||
|
||
const updatedItems = storedItems.filter((item) => item !== initialValue); | ||
updateLocalStorage(updatedItems); | ||
}; | ||
|
||
return { isAdded, toggle, add, remove }; | ||
}; |
This file was deleted.
Oops, something went wrong.
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 +1 @@ | ||
export { Blackhole } from './Blackhole'; | ||
export { Blackhole } from './ui/Blackhole'; |
File renamed without changes.
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 |
---|---|---|
|
@@ -34,6 +34,6 @@ | |
@apply font-bold; | ||
|
||
@media (max-width: theme('screens.sm')) { | ||
@apply text-6xl; | ||
@apply text-7xl; | ||
} | ||
} |
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 +1,3 @@ | ||
export { SpaceCanvas } from './SpaceCanvas'; | ||
export { spaceSlice } from './model/slice/SpaceSlice'; | ||
export type { SpaceState } from './model/slice/SpaceSlice'; | ||
export { SpaceCanvas } from './ui/SpaceCanvas'; |
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,32 @@ | ||
'use client'; | ||
|
||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
export interface SpaceState { | ||
value: boolean; | ||
} | ||
|
||
const initialState: SpaceState = { | ||
value: false, | ||
}; | ||
|
||
export const spaceSlice = createSlice({ | ||
name: 'space', | ||
initialState, | ||
reducers: { | ||
enable: (state) => { | ||
state.value = true; | ||
}, | ||
disable: (state) => { | ||
state.value = false; | ||
}, | ||
toggle: (state) => { | ||
state.value = !state.value; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { enable, disable, toggle } = spaceSlice.actions; | ||
|
||
export default spaceSlice.reducer; |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.