-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stock-market): added new ngrx functions
- Loading branch information
1 parent
c47dd9e
commit 54272f8
Showing
7 changed files
with
89 additions
and
105 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
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
65 changes: 31 additions & 34 deletions
65
...ular-ngrx-material-starter/src/app/features/examples/stock-market/stock-market.reducer.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 |
---|---|---|
@@ -1,45 +1,42 @@ | ||
import { StockMarketState } from './stock-market.model'; | ||
import { | ||
StockMarketActions, | ||
StockMarketActionTypes | ||
actionStockMarketRetrieve, | ||
actionStockMarketRetrieveError, | ||
actionStockMarketRetrieveSuccess | ||
} from './stock-market.actions'; | ||
import { Action, createReducer, on } from '@ngrx/store'; | ||
|
||
export const initialState: StockMarketState = { | ||
symbol: 'GOOGL', | ||
loading: false | ||
}; | ||
|
||
export function stockMarketReducer( | ||
state: StockMarketState = initialState, | ||
action: StockMarketActions | ||
): StockMarketState { | ||
switch (action.type) { | ||
case StockMarketActionTypes.RETRIEVE: | ||
return { | ||
...state, | ||
loading: true, | ||
stock: null, | ||
error: null, | ||
symbol: action.payload.symbol | ||
}; | ||
|
||
case StockMarketActionTypes.RETRIEVE_SUCCESS: | ||
return { | ||
...state, | ||
loading: false, | ||
stock: action.payload.stock, | ||
error: null | ||
}; | ||
const reducer = createReducer( | ||
initialState, | ||
on(actionStockMarketRetrieve, (state, { symbol }) => ({ | ||
...state, | ||
loading: true, | ||
stock: null, | ||
error: null, | ||
symbol | ||
})), | ||
on(actionStockMarketRetrieveSuccess, (state, { stock }) => ({ | ||
...state, | ||
loading: false, | ||
stock, | ||
error: null | ||
})), | ||
on(actionStockMarketRetrieveError, (state, { error }) => ({ | ||
...state, | ||
loading: false, | ||
stock: null, | ||
error | ||
})) | ||
); | ||
|
||
case StockMarketActionTypes.RETRIEVE_ERROR: | ||
return { | ||
...state, | ||
loading: false, | ||
stock: null, | ||
error: action.payload.error | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
export function stockMarketReducer( | ||
state: StockMarketState | undefined, | ||
action: Action | ||
) { | ||
return reducer(state, action); | ||
} |