Skip to content

Commit a4527fe

Browse files
committed
Feat Increment Decrement Delete
1 parent cd773f5 commit a4527fe

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

src/components/App.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useReducer } from 'react';
22
import styled from 'styled-components';
3+
import { makeId } from 'simple-util-js';
34

45
import CartInput from './CartInput';
56
import CartShow from './CartShow';
@@ -10,7 +11,7 @@ const reducer = (state, { type, payload }) => {
1011
switch (type) {
1112
case 'INCREMENT':
1213
return state.map(item => {
13-
if (item.id === payload.id) {
14+
if (item.id === payload) {
1415
item.quantity = item.quantity + 1;
1516
return item;
1617
}
@@ -19,8 +20,8 @@ const reducer = (state, { type, payload }) => {
1920
});
2021
case 'DECREMENT':
2122
return state.map(item => {
22-
if (item.id === payload.id) {
23-
item.quantity = item.quantity - 1;
23+
if (item.id === payload) {
24+
item.quantity = item.quantity <= 0 ? 0 : item.quantity - 1;
2425
return item;
2526
}
2627

@@ -30,9 +31,11 @@ const reducer = (state, { type, payload }) => {
3031
return [
3132
state,
3233
{
33-
payload
34+
...payload
3435
}
3536
];
37+
case 'DELETE':
38+
return state.filter(item => item.id !== payload);
3639
default:
3740
throw new Error();
3841
}
@@ -41,13 +44,13 @@ const reducer = (state, { type, payload }) => {
4144
const App = () => {
4245
const [carts, dispatch] = useReducer(reducer, [
4346
{
44-
id: 'test',
47+
id: makeId(),
4548
name: 'test',
4649
price: 10,
4750
quantity: 10
4851
},
4952
{
50-
id: 'test2',
53+
id: makeId(),
5154
name: 'test2',
5255
price: 100,
5356
quantity: 4
@@ -56,7 +59,7 @@ const App = () => {
5659
return (
5760
<Wrapper>
5861
<CartInput dispatch={dispatch}></CartInput>
59-
<CartShow carts={carts}></CartShow>
62+
<CartShow carts={carts} dispatch={dispatch}></CartShow>
6063
</Wrapper>
6164
);
6265
};

src/components/Cart.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import styled from 'styled-components';
33

44
const Item = styled.div``;
55

6-
const Cart = ({ id, name, price, quantity }) => {
6+
const Cart = ({ id, name, price, quantity, onIncrement, onDecrement, onDelete }) => {
77
return (
88
<Item>
99
{name} ({price}) : {quantity}
10-
<button>+1</button>
11-
<button>-1</button>
12-
<button>삭제</button>
10+
<button onClick={() => onIncrement(id)}>+1</button>
11+
<button onClick={() => onDecrement(id)}>-1</button>
12+
<button onClick={() => onDelete(id)}>삭제</button>
1313
</Item>
1414
);
1515
};

src/components/CartList.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import React from 'react';
22
import Cart from './Cart';
33

4-
const getCartComp = carts => carts.map(cart => <Cart {...cart}></Cart>);
4+
const CartList = ({ carts, dispatch }) => {
5+
const onIncrement = id => dispatch({ type: 'INCREMENT', payload: id });
6+
const onDecrement = id => dispatch({ type: 'DECREMENT', payload: id });
7+
const onDelete = id => dispatch({ type: 'DELETE', payload: id });
8+
9+
const getCartComp = carts =>
10+
carts.map(cart => (
11+
<Cart
12+
key={cart.id}
13+
{...cart}
14+
onIncrement={onIncrement}
15+
onDecrement={onDecrement}
16+
onDelete={onDelete}
17+
/>
18+
));
519

6-
const CartList = ({ carts }) => {
720
return <div>{getCartComp(carts)}</div>;
821
};
922

src/components/CartShow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import TotalCart from './TotalCart';
44

55
const getTotal = carts => carts.reduce((acc, cur) => (acc += cur.price * cur.quantity), 0);
66

7-
const CartShow = ({ carts }) => {
7+
const CartShow = ({ carts, dispatch }) => {
88
return (
99
<>
10-
<CartList carts={carts}></CartList>
10+
<CartList carts={carts} dispatch={dispatch}></CartList>
1111
<hr />
1212
<TotalCart total={getTotal(carts)}></TotalCart>
1313
</>

0 commit comments

Comments
 (0)