summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/components/Cart/Cart.js
blob: d477a51535da193d9efcbd90b58d75cd902a0699 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import DeleteIcon from '@material-ui/icons/Delete';
import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
import Type from 'ba-styles/Typography.scss';
import {
  Menu,
  Typography,
  Button,
  ListSubheader,
  List,
  ListItem,
  ListItemText,
  ListItemSecondaryAction,
  IconButton,
  Divider,
} from '@material-ui/core';
import styles from './cart-jss';


class Cart extends React.Component {
  render() {
    const {
      classes,
      anchorEl,
      close,
      dataCart,
      removeItem,
      totalPrice,
      checkout
    } = this.props;

    const getCartItem = dataArray => dataArray.map((item, index) => (
      <Fragment key={index.toString()}>
        <ListItem>
          <figure>
            <img src={item.get('thumbnail')} alt="thumb" />
          </figure>
          <ListItemText
            primary={item.get('name')}
            secondary={`Quantity: ${item.get('quantity')} Item - USD ${item.get('price') * item.get('quantity')}`}
            className={classes.itemText}
          />
          <ListItemSecondaryAction>
            <IconButton aria-label="Comments" onClick={() => removeItem(item)}>
              <DeleteIcon />
            </IconButton>
          </ListItemSecondaryAction>
        </ListItem>
        <li>
          <Divider />
        </li>
      </Fragment>
    ));
    return (
      <Menu
        id="cart-menu"
        anchorEl={anchorEl}
        anchorOrigin={{
          vertical: 'top',
          horizontal: 'right',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'right',
        }}
        open={Boolean(anchorEl)}
        onClose={close}
        className={classes.cartPanel}
      >
        <List
          component="ul"
          subheader={(
            <ListSubheader component="div">
              <ShoppingCartIcon />
              {' '}
Total:
              {' '}
              {dataCart.size}
              {' '}
Unique items in Cart
            </ListSubheader>
          )}
          className={classes.cartWrap}
        >
          {
            dataCart.size < 1 ? (
              <div className={classes.empty}>
                <Typography variant="subtitle1">Empty Cart</Typography>
                <Typography variant="caption">Your shopping items will be listed here</Typography>
              </div>
            ) : (
              <Fragment>
                {getCartItem(dataCart)}
                <ListItem className={classes.totalPrice}>
                  <Typography variant="subtitle1">
                    Total :
                    {' '}
                    <span className={Type.bold}>
$
                      {totalPrice}
                    </span>
                  </Typography>
                </ListItem>
                <li>
                  <Divider />
                </li>
                <ListItem>
                  <Button fullWidth className={classes.button} variant="contained" onClick={() => checkout()} color="secondary">
                    Checkout
                  </Button>
                </ListItem>
              </Fragment>
            )
          }
        </List>
      </Menu>
    );
  }
}

Cart.propTypes = {
  classes: PropTypes.object.isRequired,
  dataCart: PropTypes.object.isRequired,
  anchorEl: PropTypes.object,
  close: PropTypes.func.isRequired,
  removeItem: PropTypes.func.isRequired,
  checkout: PropTypes.func.isRequired,
  totalPrice: PropTypes.number.isRequired,
};

Cart.defaultProps = {
  anchorEl: null,
};

export default withStyles(styles)(Cart);