Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- increament(e, item){
- let qty = e.target.previousElementSibling.textContent;
- qty++;
- e.target.previousElementSibling.textContent = qty;
- this.props.changePrice(item);
- }
- <div>
- <input onClick={(e) =>this.decreament(e)} type="submit" value="-"/>
- <span>1</span>
- <input onClick={(e) => this.increament(e, item)} type="submit" value="+"/>
- </div>
- function mapStateToProps(state){
- return({
- itemlist: state.rootReducer
- })
- }
- function mapDispatchToProps(dispatch) {
- return({
- removeItem: (item)=>{
- dispatch({type: 'removeCart', payload: item})
- },
- changePrice: (item)=>{
- dispatch({type: 'changePrice', payload: item})
- }
- })
- }
- export default connect(mapStateToProps, mapDispatchToProps)(Cart);
- const changePrice = (itemArray, item)=>{
- let newObject = {};
- let filteredObject = itemArray.filter(reduxItem => reduxItem.id === item.id);
- let newprice = filteredObject[0].price + filteredObject[0].price;
- filteredObject[0].price = newprice;
- newObject = filteredObject;
- const something = ([...itemArray, newObject]);
- return something;
- }
- const reducer = (state = [], action) =>{
- switch (action.type) {
- case 'Add':
- return [...state, action.payload]
- case 'removeCart':
- const targetItemIndex = state.indexOf(action.payload);
- return state.filter((item, index) => index !== targetItemIndex)
- case 'changePrice':
- return changePrice(state, action.payload)
- default:
- return state;
- }
- }
- export default reducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement