-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.js
57 lines (49 loc) · 1.34 KB
/
Cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { View, Button, Text } from 'react-native';
import React from 'react';
export const Cart = ({ cart }) => (
<Container>
<User user={ cart.user } />
<Items items={ cart.items } />
</Container>
);
export const Container = (props) => <View { ...props } />
export const User = ({ user }) => (
<Title>{ user.name + ' - ' + user.email }</Title>
);
export const Title = (props) => <Text { ...props } />
export class Items extends React.Component {
constructor(props) {
super(props);
this.state = this.calculateState(props.items);
}
calculateState = (items) => ({
items,
total: items.map(item => item.price)
.reduce((a, b) => a + b, 0),
})
deleteItem = ({ id }) => {
const items = this.state.items.filter(item => item.id !== id);
this.setState(this.calculateState(items));
}
render = () => (
<Container>
{ this.state.items.map(item => (
<Item
key={ item.id }
item={ item }
onDelete={ () => this.deleteItem(item) }
/>
))}
<Total total={ this.state.total }/>
</Container>
)
}
const Total = ({ total }) => <Text>Total { total }</Text>
const Item = ({ item, onDelete }) => (
<Container>
<Text>{ item.name }</Text>
<Text>{ item.price }</Text>
<Button title='Delete' onPress={ onDelete }/>
</Container>
);
export default Cart;