Skip to content

Commit e15d7ad

Browse files
committed
Problem with list products in cart adjusted
1 parent 1f8ac29 commit e15d7ad

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

__tests__/reducers/cart.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ describe('Cart Reducer', () => {
3131
const state = reducer(previousState, CartActions.changeItemQuantity(5, 12));
3232
expect(state.items[1].quantity).toEqual(12);
3333
});
34+
35+
it('Should increase product quantity when add the same product to cart', () => {
36+
const newItem = { id: 4, title: 'T-shirt test' };
37+
const state = reducer(previousState, CartActions.addItem(newItem));
38+
39+
expect(state.items[0].quantity).toEqual(2);
40+
});
3441
});

src/components/ErrorMessage/styles.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { colors, metrics } from '~/styles';
77
export const Container = styled.View`
88
width: ${metrics.screenWidth - metrics.basePadding}px;
99
position: absolute;
10-
top: ${Platform.OS === 'android'
11-
? metrics.baseMargin
12-
: metrics.baseMargin + 20}px;
10+
top: ${Platform.OS === 'android' ? metrics.baseMargin : metrics.baseMargin + 85}px;
1311
left: ${metrics.baseMargin}px;
1412
padding: ${metrics.baseMargin}px ${metrics.basePadding}px;
1513
background-color: ${colors.danger};

src/pages/Cart/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Cart extends Component {
6565
<Fragment>
6666
<CartList
6767
data={items}
68-
keyExtractor={product => String(product.id)}
68+
keyExtractor={item => String(item.id)}
6969
showsVerticalScrollIndicator={false}
7070
renderItem={({ item: product }) => (
7171
<CartItem key={product.id}>
@@ -108,7 +108,7 @@ class Cart extends Component {
108108

109109
const mapStateToProps = state => ({
110110
items: state.cart.items,
111-
amount: state.cart.items.reduce((n1, n2) => n1 + n2.quantity * n2.price, 0), // Calculate amount of price * quantity
111+
amount: state.cart.items.reduce((total, item) => total + item.quantity * item.price, 0), // Calculate amount of price * quantity
112112
});
113113

114114
const mapDispatchToProps = dispatch => bindActionCreators(CartActions, dispatch);

src/pages/Cart/styles.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export const CartList = styled.FlatList`
1414
`;
1515

1616
export const CartItem = styled.View`
17-
flex: 1;
1817
flex-direction: row;
1918
align-items: center;
2019
justify-content: space-between;

src/store/ducks/cart.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ const INITIAL_STATE = Immutable({
1515
});
1616

1717
export const reducer = createReducer(INITIAL_STATE, {
18-
[Types.ADD_ITEM]: (state, { item }) => ({ items: [...state.items, { ...item, quantity: 1 }] }),
18+
[Types.ADD_ITEM]: (state, { item }) => {
19+
const foundItem = state.items.find(product => product.id === item.id);
20+
if (foundItem) {
21+
return {
22+
items: state.items.map(product => (product.id === item.id ? { ...product, quantity: product.quantity + 1 } : product)),
23+
};
24+
}
25+
26+
return { items: [...state.items, { ...item, quantity: 1 }] };
27+
},
1928
[Types.REMOVE_ITEM]: (state, { itemId }) => ({
2029
items: [...state.items.filter(item => item.id !== itemId)],
2130
}),

0 commit comments

Comments
 (0)