Created
June 14, 2016 14:35
-
-
Save Jessica7/98f7829f2fa348f0bdc68febf7e67517 to your computer and use it in GitHub Desktop.
reducer
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
| import * as ACTIONS from '../constants/products'; | |
| const initialState = { | |
| items: [], | |
| allCheked : [] | |
| }; | |
| const sortByPrice = (a, b) => { | |
| var productA = parseInt(a.priceCash); | |
| var productB = parseInt(b.priceCash); | |
| return (productA >= productB) ? -1 : 1; | |
| }; | |
| function products(state = initialState, action) { | |
| switch(action.type) { | |
| case ACTIONS.INJECT_PRODUCTS: { | |
| return { | |
| ...state, | |
| items: action.items | |
| }; | |
| }; | |
| case ACTIONS.FILTER_PRICE: { | |
| const sorted = state.items.sort(sortByPrice); | |
| if (action.key == 'lowest price') { | |
| sorted.reverse(); | |
| } | |
| return { | |
| ...state, | |
| items: sorted | |
| }; | |
| }; | |
| case ACTIONS.FITLER_CATEGORY: { | |
| const newList = state.allChecked; | |
| newList.push("palavra"); | |
| return { | |
| ...state, | |
| allChecked: newList | |
| } | |
| } | |
| default: { | |
| return state; | |
| }; | |
| } | |
| } | |
| export default products; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment