- Notifications
You must be signed in to change notification settings - Fork429
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
hakonamatata wants to merge9 commits intoTrilonIO:masterChoose a base branch fromhakonamatata:ngrx_store
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
b08d7a7 added ngrx to package.json
hakonamatata5b96a9d added router state
hakonamatata1e62eef added a counter reducer
hakonamatatab566186 counter component updates counter reducer
hakonamatata62fa6fa added decrease action
hakonamatata4c78af0 removed some comments
hakonamatataa7312de added to readme
hakonamatata586b3ea readme image
hakonamatatacba8747 moved import to app.module
hakonamatataFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
13 changes: 12 additions & 1 deletionClient/app/app.module.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
6 changes: 3 additions & 3 deletionsClient/app/browser-app.module.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
5 changes: 4 additions & 1 deletionClient/app/containers/counter/counter.component.html
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 deletionsClient/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 counter: Observable<CounterState>; | ||
| constructor(private _store: Store<fromRoot.State>) { | ||
| this.counter = _store.select<CounterState>('counter') | ||
| } | ||
| public incrementCounter() { | ||
| // 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 deletionsClient/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 deletionsClient/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); | ||
| // } | ||
| } |
8 changes: 8 additions & 0 deletionsREADME.md
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 addeddocs/ngrx-store.PNG
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletionspackage.json
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.