feat: SW-693 Moved state changes to reducer
This commit is contained in:
@@ -115,14 +115,21 @@ export default function Search({ locations }: SearchProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
state.searchData =
|
const searchData =
|
||||||
typeof window !== "undefined"
|
typeof window !== "undefined"
|
||||||
? JSON.parse(sessionStorage.getItem(sessionStorageKey) || "")
|
? JSON.parse(sessionStorage.getItem(sessionStorageKey) || "")
|
||||||
: undefined
|
: undefined
|
||||||
state.searchHistory =
|
const searchHistory =
|
||||||
typeof window !== "undefined"
|
typeof window !== "undefined"
|
||||||
? JSON.parse(localStorage.getItem(localStorageKey) || "")
|
? JSON.parse(localStorage.getItem(localStorageKey) || "")
|
||||||
: null
|
: null
|
||||||
|
dispatch({
|
||||||
|
payload: {
|
||||||
|
searchData,
|
||||||
|
searchHistory,
|
||||||
|
},
|
||||||
|
type: ActionType.SET_STORAGE_DATA,
|
||||||
|
})
|
||||||
}, [state])
|
}, [state])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -68,6 +68,13 @@ export function reducer(state: State, action: Action) {
|
|||||||
searchHistory: action.payload.searchHistory,
|
searchHistory: action.payload.searchHistory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case ActionType.SET_STORAGE_DATA: {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
searchData: action.payload.searchData,
|
||||||
|
searchHistory: action.payload.searchHistory,
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
const unhandledActionType: never = type
|
const unhandledActionType: never = type
|
||||||
console.info(`Unhandled type: ${unhandledActionType}`)
|
console.info(`Unhandled type: ${unhandledActionType}`)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export enum ActionType {
|
|||||||
CLEAR_SEARCH_LOCATIONS = "CLEAR_SEARCH_LOCATIONS",
|
CLEAR_SEARCH_LOCATIONS = "CLEAR_SEARCH_LOCATIONS",
|
||||||
SEARCH_LOCATIONS = "SEARCH_LOCATIONS",
|
SEARCH_LOCATIONS = "SEARCH_LOCATIONS",
|
||||||
SELECT_ITEM = "SELECT_ITEM",
|
SELECT_ITEM = "SELECT_ITEM",
|
||||||
|
SET_STORAGE_DATA = "SET_STORAGE_DATA",
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ClearHistoryLocationsAction {
|
interface ClearHistoryLocationsAction {
|
||||||
@@ -46,11 +47,20 @@ interface SetItemAction {
|
|||||||
type: ActionType.SELECT_ITEM
|
type: ActionType.SELECT_ITEM
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SetStorageData {
|
||||||
|
payload: {
|
||||||
|
searchData: Location
|
||||||
|
searchHistory: Locations
|
||||||
|
}
|
||||||
|
type: ActionType.SET_STORAGE_DATA
|
||||||
|
}
|
||||||
|
|
||||||
export type Action =
|
export type Action =
|
||||||
| ClearHistoryLocationsAction
|
| ClearHistoryLocationsAction
|
||||||
| ClearSearchLocationsAction
|
| ClearSearchLocationsAction
|
||||||
| SearchLocationsAction
|
| SearchLocationsAction
|
||||||
| SetItemAction
|
| SetItemAction
|
||||||
|
| SetStorageData
|
||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
defaultLocations: Locations
|
defaultLocations: Locations
|
||||||
|
|||||||
Reference in New Issue
Block a user