From 072eca0e5038a7755ca9b2c94dfe1a0c3f110968 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 24 May 2018 15:19:21 -0400 Subject: colocate queries and components --- frontend/components/AddToCart.js | 52 +++++++++++++++++++++++------------ frontend/components/Cart.js | 43 +++++++++++++++-------------- frontend/components/CreateItem.js | 23 +++++++++++++++- frontend/components/DeleteItem.js | 19 +++++++++---- frontend/components/EditUser.js | 17 ++++++++++-- frontend/components/Items.js | 17 +++++++++++- frontend/components/Nav.js | 12 +++----- frontend/components/Order.js | 22 ++++++++++++++- frontend/components/OrderList.js | 22 ++++++++++++++- frontend/components/Pagination.js | 15 ++++++++-- frontend/components/Permissions.js | 25 ++++++++++++++++- frontend/components/PleaseSignIn.js | 2 +- frontend/components/RemoveFromCart.js | 21 ++++++++------ frontend/components/Reset.js | 14 +++++++++- frontend/components/ResetRequest.js | 11 +++++++- frontend/components/Search.js | 12 +++++++- frontend/components/Signin.js | 21 +++++++++++--- frontend/components/Signout.js | 14 ++++++++-- frontend/components/Signup.js | 20 +++++++++++--- frontend/components/SingleItem.js | 15 +++++++++- frontend/components/TakeMyMoney.js | 21 ++++++++++++-- frontend/components/UpdateItem.js | 13 +++++++-- frontend/components/User.js | 45 ++++++++++++++++++++++++++++++ 23 files changed, 386 insertions(+), 90 deletions(-) create mode 100644 frontend/components/User.js (limited to 'frontend/components') diff --git a/frontend/components/AddToCart.js b/frontend/components/AddToCart.js index 7cb0856..03242a4 100644 --- a/frontend/components/AddToCart.js +++ b/frontend/components/AddToCart.js @@ -1,10 +1,24 @@ import { Component } from 'react'; import { Mutation } from 'react-apollo'; import PropTypes from 'prop-types'; -import { - ADD_TO_CART_MUTATION, - CURRENT_USER_QUERY, -} from '../queries/queries.graphql'; +import gql from 'graphql-tag'; +import User, { CURRENT_USER_QUERY } from './User'; + +const ADD_TO_CART_MUTATION = gql` + mutation addToCart($id: ID!) { + addToCart(id: $id) { + id + quantity + item { + id + price + description + image + title + } + } + } +`; class AddToCart extends Component { static propTypes = { @@ -15,9 +29,7 @@ class AddToCart extends Component { const newCartItem = payload.data.addToCart; const data = cache.readQuery({ query: CURRENT_USER_QUERY }); - const existingIndex = data.me.cart.findIndex( - cartItem => cartItem.id === newCartItem.id, - ); + const existingIndex = data.me.cart.findIndex(cartItem => cartItem.id === newCartItem.id); if (existingIndex >= 0) { // already in cache, just replace it data.me.cart = [ @@ -34,19 +46,23 @@ class AddToCart extends Component { render() { const { id } = this.props; return ( - - {(addToCart, { loading }) => ( - - )} - + + {({ data: { me }, loading }) => { + if (!me || loading) return null; + return ( + + {(addToCart, { loading }) => ( + + )} + + ); + }} + ); } } export default AddToCart; +export { ADD_TO_CART_MUTATION }; diff --git a/frontend/components/Cart.js b/frontend/components/Cart.js index a815b78..7e8a9c5 100644 --- a/frontend/components/Cart.js +++ b/frontend/components/Cart.js @@ -1,14 +1,11 @@ import React from 'react'; import { Mutation, Query } from 'react-apollo'; import { adopt } from 'react-adopt'; +import gql from 'graphql-tag'; import TakeMyMoney from './TakeMyMoney'; import formatMoney from '../lib/formatMoney'; import CartItem from './CartItem'; -import { - CURRENT_USER_QUERY, - LOCAL_STATE_QUERY, - TOGGLE_CART_MUTATION, -} from '../queries/queries.graphql'; +import { CURRENT_USER_QUERY } from './User'; import calcTotalPrice from '../lib/calcTotalPrice'; import Error from './ErrorMessage'; import CartStyles from './styles/CartStyles'; @@ -16,46 +13,51 @@ import Supreme from './styles/Supreme'; import CloseButton from './styles/CloseButton'; import SickButton from './styles/SickButton'; +const LOCAL_STATE_QUERY = gql` + query { + cartOpen @client + } +`; + +const TOGGLE_CART_MUTATION = gql` + mutation { + toggleCart @client + } +`; + const Composed = adopt({ toggleCart: ({ render }) => ( {(mutate, result) => render({ mutate, result })} ), - localState: , - currentUser: , + localState: ({ render }) => , + currentUser: ({ render }) => ( + + ), }); const Cart = () => ( {({ toggleCart, localState, currentUser }) => { - const { - data: { me }, - error, - loading, - } = currentUser; + const { data: { me }, error, loading } = currentUser; if (loading) return

Loading...

; if (error) return ; if (!me) return null; return (
- + × {me.name}'s Cart.

- You have {me.cart.length} item{me.cart.length === 1 ? '' : 's'} in - your cart. + You have {me.cart.length} item{me.cart.length === 1 ? '' : 's'} in your cart.

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