Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. increament(e, item){
  2. let qty = e.target.previousElementSibling.textContent;
  3. qty++;
  4. e.target.previousElementSibling.textContent = qty;
  5. this.props.changePrice(item);
  6. }
  7.  
  8. <div>
  9. <input onClick={(e) =>this.decreament(e)} type="submit" value="-"/>
  10. <span>1</span>
  11. <input onClick={(e) => this.increament(e, item)} type="submit" value="+"/>
  12. </div>
  13.  
  14. function mapStateToProps(state){
  15. return({
  16. itemlist: state.rootReducer
  17. })
  18. }
  19. function mapDispatchToProps(dispatch) {
  20. return({
  21. removeItem: (item)=>{
  22. dispatch({type: 'removeCart', payload: item})
  23. },
  24. changePrice: (item)=>{
  25. dispatch({type: 'changePrice', payload: item})
  26. }
  27. })
  28. }
  29. export default connect(mapStateToProps, mapDispatchToProps)(Cart);
  30.  
  31. const changePrice = (itemArray, item)=>{
  32. let newObject = {};
  33. let filteredObject = itemArray.filter(reduxItem => reduxItem.id === item.id);
  34. let newprice = filteredObject[0].price + filteredObject[0].price;
  35. filteredObject[0].price = newprice;
  36. newObject = filteredObject;
  37. const something = ([...itemArray, newObject]);
  38. return something;
  39. }
  40. const reducer = (state = [], action) =>{
  41. switch (action.type) {
  42. case 'Add':
  43. return [...state, action.payload]
  44. case 'removeCart':
  45. const targetItemIndex = state.indexOf(action.payload);
  46. return state.filter((item, index) => index !== targetItemIndex)
  47. case 'changePrice':
  48. return changePrice(state, action.payload)
  49. default:
  50. return state;
  51. }
  52. }
  53. export default reducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement