|
1 |
| -import React, { Component } from 'react'; |
| 1 | +import React, { Component, Fragment } from 'react'; |
2 | 2 | import { bindActionCreators } from 'redux';
|
3 | 3 | import { connect } from 'react-redux';
|
| 4 | +import { Alert } from 'react-native'; |
4 | 5 | import PropTypes from 'prop-types';
|
5 | 6 |
|
6 | 7 | import CartActions from '~/store/ducks/cart';
|
7 | 8 |
|
8 |
| -import { Container, Product, Amount } from './styles'; |
| 9 | +import { colors } from '~./styles'; |
| 10 | +import { |
| 11 | + Container, |
| 12 | + CartList, |
| 13 | + CartItem, |
| 14 | + Image, |
| 15 | + Info, |
| 16 | + Name, |
| 17 | + Brand, |
| 18 | + Price, |
| 19 | + Form, |
| 20 | + AmountInput, |
| 21 | + DeleteButton, |
| 22 | + DeleteIcon, |
| 23 | + EmptyMessage, |
| 24 | + SubTotal, |
| 25 | + SubTotalText, |
| 26 | + SubTotalPrice, |
| 27 | +} from './styles'; |
9 | 28 |
|
10 | 29 | class Cart extends Component {
|
11 | 30 | static navigationOptions = {
|
12 | 31 | title: 'Cart',
|
13 |
| - headerTitleStyle: { color: '#f19d9d' }, |
| 32 | + headerTitleStyle: { color: colors.secondary }, |
| 33 | + }; |
| 34 | + |
| 35 | + static propTypes = { |
| 36 | + removeItem: PropTypes.func.isRequired, |
| 37 | + changeItemQuantity: PropTypes.func.isRequired, |
| 38 | + amount: PropTypes.number.isRequired, |
| 39 | + items: PropTypes.arrayOf( |
| 40 | + PropTypes.shape({ |
| 41 | + id: PropTypes.number.isRequired, |
| 42 | + image: PropTypes.string.isRequired, |
| 43 | + name: PropTypes.string.isRequired, |
| 44 | + brand: PropTypes.string.isRequired, |
| 45 | + price: PropTypes.number.isRequired, |
| 46 | + }), |
| 47 | + ).isRequired, |
| 48 | + }; |
| 49 | + |
| 50 | + handleConfirmDelete = (product) => { |
| 51 | + const { removeItem } = this.props; |
| 52 | + |
| 53 | + Alert.alert('Remove item', 'Are you sure you want to delete this item?', [ |
| 54 | + { text: 'Cancel' }, |
| 55 | + { text: 'Yes', onPress: () => removeItem(product.id) }, |
| 56 | + ]); |
14 | 57 | };
|
15 | 58 |
|
16 | 59 | render() {
|
17 |
| - const { items } = this.props; |
| 60 | + const { items, amount, changeItemQuantity } = this.props; |
18 | 61 |
|
19 | 62 | return (
|
20 | 63 | <Container>
|
21 |
| - {items.map(item => ( |
22 |
| - <Product /> |
23 |
| - ))} |
24 |
| - |
25 |
| - <Amount /> |
| 64 | + {items.length > 0 ? ( |
| 65 | + <Fragment> |
| 66 | + <CartList |
| 67 | + data={items} |
| 68 | + keyExtractor={product => String(product.id)} |
| 69 | + showsVerticalScrollIndicator={false} |
| 70 | + renderItem={({ item: product }) => ( |
| 71 | + <CartItem key={product.id}> |
| 72 | + <Image source={{ uri: product.image }} /> |
| 73 | + <Info> |
| 74 | + <Name>{product.name}</Name> |
| 75 | + <Brand>{product.brand}</Brand> |
| 76 | + <Price>{`$ ${product.price}`}</Price> |
| 77 | + </Info> |
| 78 | + <Form> |
| 79 | + <AmountInput |
| 80 | + autoCorrect={false} |
| 81 | + autoCapitalize="none" |
| 82 | + defaultValue={String(product.quantity)} |
| 83 | + maxLength={2} |
| 84 | + keyboardType="numeric" |
| 85 | + onChangeText={text => changeItemQuantity(product.id, Number(text))} |
| 86 | + > |
| 87 | + {product.amount} |
| 88 | + </AmountInput> |
| 89 | + <DeleteButton onPress={() => this.handleConfirmDelete(product)}> |
| 90 | + <DeleteIcon /> |
| 91 | + </DeleteButton> |
| 92 | + </Form> |
| 93 | + </CartItem> |
| 94 | + )} |
| 95 | + /> |
| 96 | + <SubTotal> |
| 97 | + <SubTotalText>Total</SubTotalText> |
| 98 | + <SubTotalPrice>{`R$ ${amount.toFixed(2)}`}</SubTotalPrice> |
| 99 | + </SubTotal> |
| 100 | + </Fragment> |
| 101 | + ) : ( |
| 102 | + <EmptyMessage>There are no products in the cart.</EmptyMessage> |
| 103 | + )} |
26 | 104 | </Container>
|
27 | 105 | );
|
28 | 106 | }
|
|
0 commit comments