Created
March 13, 2017 23:44
-
-
Save alexlafroscia/af0f65a39e9add45ff19acb0988a9df7 to your computer and use it in GitHub Desktop.
redux cart
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 Ember from 'ember'; | |
import connect from 'ember-redux/components/connect'; | |
const stateToComputed = (state) => { | |
return { | |
numberOfTypes: state.cart.length, | |
totalCartCount: state.cart.reduce((totalCount, product) => totalCount += product.count, 0), | |
cartItems: state.cart | |
}; | |
}; | |
const dispatchToActions = (dispatch) => { | |
return { | |
add: (product) => dispatch({ type: 'ADD_PRODUCT', product }) | |
}; | |
}; | |
const ApplicationController = Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}) | |
export default connect(stateToComputed, dispatchToActions)(ApplicationController); |
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
function addProduct(cart, product) { | |
const { type } = product; | |
const existingProductType = cart.find((product) => product.type === type); | |
if (existingProductType) { | |
existingProductType.count += 1; | |
} else { | |
cart.push({ type, count: 1 }); | |
} | |
return cart; | |
} | |
export default function cart(state, { type, product }) { | |
switch (type) { | |
case 'ADD_PRODUCT': | |
return addProduct(state, product); | |
default: | |
return []; | |
}; | |
}; |
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 cart from './cart'; | |
export default { | |
cart | |
}; |
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
{ | |
"version": "0.11.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.11.0", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.11.0", | |
"ember-testing": "2.11.0" | |
}, | |
"addons": { | |
"ember-redux": "2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment