-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ngrx store functionality to display a value from the store a…
…nd save it in local storage after pushing a button
- Loading branch information
Tommy Heyding
committed
Apr 10, 2022
1 parent
3686372
commit fa62c2a
Showing
11 changed files
with
98 additions
and
73 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
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,10 +1,25 @@ | ||
import {ActionReducerMap} from '@ngrx/store'; | ||
import * as fromHome from './home/pages/home/store/home.reducer'; | ||
import {ActionReducer, ActionReducerMap, combineReducers, MetaReducer} from '@ngrx/store'; | ||
import {AppStore} from './app.store'; | ||
import {localStorageSync} from 'ngrx-store-localstorage'; | ||
import {homeFeatureKey} from './home/pages/home/store/home.store'; | ||
import {homeReducer} from './home/pages/home/store/home.reducer'; | ||
|
||
export interface AppState { | ||
home: fromHome.HomeState; | ||
// There seems to be a bug with redux. This declaration leads to compile errors. The option to use combineReducers works. | ||
/* | ||
export const reducers: ActionReducerMap<AppStore> = { | ||
home: homeReducer, | ||
};*/ | ||
|
||
export const reducers = combineReducers({homeReducer} as any); | ||
|
||
export function localStorageSyncReducer(reducer: ActionReducer<AppStore>): ActionReducer<AppStore> { | ||
return localStorageSync({ | ||
keys: [ | ||
homeFeatureKey | ||
], | ||
rehydrate: true, | ||
storage: window.sessionStorage | ||
})(reducer); | ||
} | ||
|
||
export const appReducer: ActionReducerMap<AppState, any> = { | ||
home: fromHome.homeReducer | ||
}; | ||
export const metaReducers: Array<MetaReducer<any, any>> = [localStorageSyncReducer]; |
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,5 @@ | ||
import {HomeState} from './home/pages/home/store/home.store'; | ||
|
||
export interface AppStore { | ||
home?: HomeState; | ||
} |
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,18 +1,12 @@ | ||
import {Action} from '@ngrx/store'; | ||
import {createAction, props} from '@ngrx/store'; | ||
|
||
// Load Data is just a dummy action and is not used | ||
export const LOAD_DATA = '[HOME] LOAD_DATA'; | ||
export const SET_USER_INPUT = '[HOME] SET_USER_INPUT'; | ||
|
||
export class LoadData implements Action { | ||
readonly type = LOAD_DATA; | ||
const loadData = createAction('[HOME] LOAD DATA'); | ||
const getUserInput = createAction('[HOME] GET USER INPUT'); | ||
const setUserInput = createAction('[HOME] SET USER INPUT', props<{ userInput: string }>()); | ||
|
||
export const HomeActions = { | ||
loadData, | ||
getUserInput, | ||
setUserInput | ||
} | ||
|
||
export class SetUserInput implements Action { | ||
readonly type = SET_USER_INPUT; | ||
|
||
constructor(public payload: string) { | ||
} | ||
} | ||
|
||
export type HomeActions = LoadData | SetUserInput |
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,10 @@ | ||
import * as HomeActions from './home.actions'; | ||
import {Action, createReducer, on} from '@ngrx/store'; | ||
import {defaultHomeStore, HomeState} from './home.store'; | ||
import {HomeActions} from './home.actions'; | ||
|
||
export const homeFeatureKey = 'home'; | ||
const homeRedux = createReducer(defaultHomeStore, | ||
on(HomeActions.setUserInput, (state, {userInput}) => ({...state, userInput}))); | ||
|
||
export interface HomeState { | ||
userInput: string; | ||
} | ||
|
||
export const initialHomeState: HomeState = { | ||
userInput: 'Hello world!' | ||
}; | ||
|
||
export function homeReducer(state: HomeState = initialHomeState, action: HomeActions.HomeActions): HomeState { | ||
switch (action.type) { | ||
case HomeActions.SET_USER_INPUT: | ||
return { | ||
...state, | ||
userInput: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
export function homeReducer(state: HomeState, action: Action): HomeState { | ||
return homeRedux(state, action); | ||
} |
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,10 +1,11 @@ | ||
import {createFeatureSelector, createSelector} from '@ngrx/store'; | ||
import * as fromHome from './home.reducer'; | ||
import {AppStore} from '../../../../app.store'; | ||
import {HomeState} from './home.store'; | ||
|
||
export const selectHomeState = createFeatureSelector<fromHome.HomeState>( | ||
fromHome.homeFeatureKey | ||
); | ||
const selectFeature = createFeatureSelector<AppStore, HomeState>('home'); | ||
|
||
export const selectUserInput = createSelector( | ||
selectHomeState, state => state.userInput | ||
); | ||
const selectUserInput = createSelector(selectFeature, homeState => homeState.userInput); | ||
|
||
export const HomeSelectors = { | ||
selectUserInput: selectUserInput, | ||
}; |
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,9 @@ | ||
export const homeFeatureKey = 'home'; | ||
|
||
export interface HomeState { | ||
userInput: string; | ||
} | ||
|
||
export const defaultHomeStore: HomeState = { | ||
userInput: 'Hello world!' | ||
} |