-
Notifications
You must be signed in to change notification settings - Fork 429
[How to add NgRx] - NgRx Store & App State handling #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b08d7a7
added ngrx to package.json
hakonamatata 5b96a9d
added router state
hakonamatata 1e62eef
added a counter reducer
hakonamatata b566186
counter component updates counter reducer
hakonamatata 62fa6fa
added decrease action
hakonamatata 4c78af0
removed some comments
hakonamatata a7312de
added to readme
hakonamatata 586b3ea
readme image
hakonamatata cba8747
moved import to app.module
hakonamatata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
24 changes: 22 additions & 2 deletions
Client/app/containers/counter/counter.component.ts
This file contains hidden or 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,13 +1,33 @@ | ||
// tslint:disable:no-null-keyword | ||
// tslint:disable:semicolon | ||
|
||
import { Component } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import * as fromRoot from '../../reducers'; | ||
|
||
import { COUNTER_INCREASE, COUNTER_DECREASE } from '../../reducers/counter.reducer'; | ||
import { CounterState } from '../../reducers/counter.reducer'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
@Component({ | ||
selector: 'counter', | ||
templateUrl: './counter.component.html' | ||
}) | ||
export class CounterComponent { | ||
public currentCount = 0; | ||
// public currentCount = 0; | ||
public counter: Observable<CounterState>; | ||
|
||
constructor(private _store: Store<fromRoot.State>) { | ||
this.counter = _store.select<CounterState>('counter') | ||
} | ||
|
||
public incrementCounter() { | ||
this.currentCount++; | ||
// this.currentCount++; | ||
this._store.dispatch({ type: COUNTER_INCREASE, payload: null }) | ||
} | ||
|
||
public decreaseCounter() { | ||
// this.currentCount++; | ||
this._store.dispatch({ type: COUNTER_DECREASE, payload: null }) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
Client/app/reducers/counter.reducer.ts
This file contains hidden or 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,43 @@ | ||
// tslint:disable:semicolon | ||
|
||
import { Action } from '@ngrx/store'; | ||
|
||
// user actions | ||
export const COUNTER_INCREASE = '[COUNTER] increase' | ||
export const COUNTER_DECREASE = '[COUNTER] decrease' | ||
|
||
export class CounterState { | ||
value: number | ||
} | ||
|
||
const initialState: CounterState = { | ||
value: 0 | ||
} | ||
|
||
export function reducer(state = initialState, action: Action): CounterState { | ||
|
||
switch (action.type) { | ||
case COUNTER_INCREASE: { | ||
|
||
let newState = { | ||
value: state.value + 1 | ||
} | ||
|
||
return newState | ||
|
||
} | ||
case COUNTER_DECREASE: | ||
|
||
let newState = { | ||
value: state.value - 1 | ||
} | ||
|
||
return newState | ||
|
||
default: | ||
return state | ||
} | ||
} | ||
|
||
|
||
|
27 changes: 27 additions & 0 deletions
Client/app/reducers/index.ts
This file contains hidden or 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,27 @@ | ||
import { compose } from "@ngrx/core"; | ||
import { combineReducers, ActionReducer } from "@ngrx/store"; | ||
import { storeFreeze } from 'ngrx-store-freeze'; | ||
|
||
import * as fromRouter from '@ngrx/router-store'; | ||
import * as fromCounter from '../reducers/counter.reducer'; | ||
|
||
export interface State { | ||
router: fromRouter.RouterState; | ||
counter: fromCounter.CounterState; | ||
} | ||
|
||
const reducers = { | ||
router: fromRouter.routerReducer, | ||
counter: fromCounter.reducer | ||
}; | ||
|
||
const developmentReducer: ActionReducer<State> = compose(storeFreeze, combineReducers)(reducers); | ||
// const productionReducer: ActionReducer<State> = combineReducers(reducers); | ||
|
||
export function reducer(state: any, action: any) { | ||
// if (environment.production) { | ||
// return productionReducer(state, action); | ||
// } else { | ||
return developmentReducer(state, action); | ||
// } | ||
} |
This file contains hidden or 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
Binary file added
docs/ngrx-store.PNG
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.