From c3bbebfcfeed7a9b8c7470d7b48e12046d834e59 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 17 Aug 2018 15:51:02 -0400 Subject: suuuup everyone --- .../42/frontend/components/AddToCart.js | 35 ++++++++++++ stepped-solutions/42/frontend/components/Cart.js | 63 ++++++++++++++++++++++ .../42/frontend/components/CartItem.js | 41 ++++++++++++++ stepped-solutions/42/frontend/components/User.js | 38 +++++++++++++ 4 files changed, 177 insertions(+) create mode 100755 stepped-solutions/42/frontend/components/AddToCart.js create mode 100755 stepped-solutions/42/frontend/components/Cart.js create mode 100755 stepped-solutions/42/frontend/components/CartItem.js create mode 100755 stepped-solutions/42/frontend/components/User.js (limited to 'stepped-solutions/42/frontend') diff --git a/stepped-solutions/42/frontend/components/AddToCart.js b/stepped-solutions/42/frontend/components/AddToCart.js new file mode 100755 index 0000000..3e2842b --- /dev/null +++ b/stepped-solutions/42/frontend/components/AddToCart.js @@ -0,0 +1,35 @@ +import React from 'react'; +import { Mutation } from 'react-apollo'; +import gql from 'graphql-tag'; +import { CURRENT_USER_QUERY } from './User'; + +const ADD_TO_CART_MUTATION = gql` + mutation addToCart($id: ID!) { + addToCart(id: $id) { + id + quantity + } + } +`; + +class AddToCart extends React.Component { + render() { + const { id } = this.props; + return ( + + {(addToCart, { loading }) => ( + + )} + + ); + } +} +export default AddToCart; diff --git a/stepped-solutions/42/frontend/components/Cart.js b/stepped-solutions/42/frontend/components/Cart.js new file mode 100755 index 0000000..1895235 --- /dev/null +++ b/stepped-solutions/42/frontend/components/Cart.js @@ -0,0 +1,63 @@ +import React from 'react'; +import { Query, Mutation } from 'react-apollo'; +import gql from 'graphql-tag'; +import User from './User'; +import CartStyles from './styles/CartStyles'; +import Supreme from './styles/Supreme'; +import CloseButton from './styles/CloseButton'; +import SickButton from './styles/SickButton'; +import CartItem from './CartItem'; +import calcTotalPrice from '../lib/calcTotalPrice'; +import formatMoney from '../lib/formatMoney'; + +const LOCAL_STATE_QUERY = gql` + query { + cartOpen @client + } +`; + +const TOGGLE_CART_MUTATION = gql` + mutation { + toggleCart @client + } +`; + +const Cart = () => ( + + {({ data: { me } }) => { + if (!me) return null; + console.log(me); + return ( + + {toggleCart => ( + + {({ data }) => ( + +
+ + × + + {me.name}'s Cart +

+ You Have {me.cart.length} Item{me.cart.length === 1 ? '' : 's'} in your cart. +

+
+
    + {me.cart.map(cartItem => )} +
+
+

{formatMoney(calcTotalPrice(me.cart))}

+ Checkout +
+
+ )} +
+ )} +
+ ); + }} +
+); + +export default Cart; +export { LOCAL_STATE_QUERY, TOGGLE_CART_MUTATION }; diff --git a/stepped-solutions/42/frontend/components/CartItem.js b/stepped-solutions/42/frontend/components/CartItem.js new file mode 100755 index 0000000..8f6a099 --- /dev/null +++ b/stepped-solutions/42/frontend/components/CartItem.js @@ -0,0 +1,41 @@ +import React from 'react'; +import styled from 'styled-components'; +import PropTypes from 'prop-types'; +import formatMoney from '../lib/formatMoney'; + +const CartItemStyles = styled.li` + padding: 1rem 0; + border-bottom: 1px solid ${props => props.theme.lightgrey}; + display: grid; + align-items: center; + grid-template-columns: auto 1fr auto; + img { + margin-right: 10px; + } + h3, + p { + margin: 0; + } +`; + +const CartItem = ({ cartItem }) => ( + + {cartItem.item.title} +
+

{cartItem.item.title}

+

+ {formatMoney(cartItem.item.price * cartItem.quantity)} + {' - '} + + {cartItem.quantity} × {formatMoney(cartItem.item.price)} each + +

+
+
+); + +CartItem.propTypes = { + cartItem: PropTypes.object.isRequired, +}; + +export default CartItem; diff --git a/stepped-solutions/42/frontend/components/User.js b/stepped-solutions/42/frontend/components/User.js new file mode 100755 index 0000000..429bb25 --- /dev/null +++ b/stepped-solutions/42/frontend/components/User.js @@ -0,0 +1,38 @@ +import { Query } from 'react-apollo'; +import gql from 'graphql-tag'; +import PropTypes from 'prop-types'; + +const CURRENT_USER_QUERY = gql` + query { + me { + id + email + name + permissions + cart { + id + quantity + item { + id + price + image + title + description + } + } + } + } +`; + +const User = props => ( + + {payload => props.children(payload)} + +); + +User.propTypes = { + children: PropTypes.func.isRequired, +}; + +export default User; +export { CURRENT_USER_QUERY }; -- cgit v1.3