diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-01-30 12:41:01 -0500 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-01-30 12:41:01 -0500 |
| commit | cd0b07b3adb36fb5ec098f9e511ab45d774fee7b (patch) | |
| tree | a203e5010219ccea1acc6bbd2c0a4bcfa0a78615 /frontend/components/Cart.js | |
| parent | 0a1648bf47490b312169c531aeb51ef4725e8d2e (diff) | |
Move to Prisma Backend
Diffstat (limited to 'frontend/components/Cart.js')
| -rw-r--r-- | frontend/components/Cart.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/frontend/components/Cart.js b/frontend/components/Cart.js new file mode 100644 index 0000000..88b072c --- /dev/null +++ b/frontend/components/Cart.js @@ -0,0 +1,46 @@ +import { Component } from 'react'; +import { graphql, compose } from 'react-apollo'; +import styled from 'styled-components'; +import { CURRENT_USER_QUERY } from '../queries'; +import formatMoney from '../lib/formatMoney.js'; +import ChaChing from './ChaChing'; + +const CartStyles = styled.div` + background: white; + border-radius: 10px; + padding: 20px; + overflow: hidden; + display: flex; + align-items: center; +`; + +class Cart extends Component { + componentDidMount() { + // This fetches the new data, but doesn't populate the user via props + // this.props.currentUserQuery.refetch(); + // This fetches the new data, and populates the user via props + setTimeout(this.props.currentUserQuery.refetch, 1); + } + + render() { + // Check for loading state.. + const { loading, error } = this.props.currentUserQuery; + const { user } = this.props.currentUserQuery; + if (loading || error || !user) return <p>Cart Loading...</p>; + const total = user.cart.reduce((a, b) => a + b.price, 0); + + return ( + <CartStyles className="cart"> + There + {user.cart.length === 1 ? ' is ' : 'are '} + <ChaChing amount={user.cart.length} /> + {user.cart.length === 1 ? ' item ' : ' items '} + in your cart totaling + <ChaChing amount={formatMoney(total)} /> + </CartStyles> + ); + } +} + +const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' }); +export default compose(userEnhancer)(Cart); |
