2018-02-28添加到购物车

The data structure is like :

{cart: [
  {listingId: 1, listing:{id: 1, }, quantity: 10},
  {listingId: 2, listing:{id: 2, }, quantity: 20},
···
]

Thus the actions look like :

export const addToCart = listing => ({
  type: 'ADD_TO_CART',
  listing
})

export const addOne = listingId => ({
  type: 'ADD_ONE',
  listingId
})

export const minusOne = listingId => ({
  type: 'MINUS_ONE',
  listingId
})

When you want to increment one of the listing, first find it in the array, then return the whole state.

import { ADD_TO_CART, ADD_ONE, MINUS_ONE } from './actions'

const initialState = {
  cart: []
}

const cart = (state = initialState.cart, action) => {
  switch (action.type) {
    case ADD_TO_CART:
      return Object.assign({}, state,
        {listing: action.listing, listingId: action.listing.id, quantity: 1})
    case ADD_ONE:
      const plus = state.find(d=>d.listingId===action.listingId)
      plus.quantity++
      return Object.assign({}, state, {cart: plus})
    case MINUS_ONE:
      const minus = state.find(d => d.listingId === action.listingId)
      minus.quantity--
      return Object.assign({}, state, {cart: minus})
    default:
      return state
  }
}

export default cart  

There is sth difficulty in writing addToCart method, currently the Main stateless function :

import React from "react"
import { Query } from 'react-apollo'
import { gql } from 'apollo-boost'
import Product from './Product'
import './Main.css'

const GET_DOG = gql`
  query {
    store(id: 4751) {
      title
      listings(search:{offset:0,limit:10,order_by:"id",order_asc:"asc",per_page:10,page:1,include_deleted:true}) {
      id
      price
      quantity
      created_at
      updated_at
      product_id
      blid
      store_id
      web_price
      department_id
      product {
        id
        name
        image_url
        description
        barcode
        }
      }
    }
  }
`

const Main = () => (
  <Query query={GET_DOG}>
    {({loading, error, data}) => {
      if (loading) return <div>Loading</div>
      if (error) return <div>Error :</div>

      return (
        <div className="App">
          <header className="App-header">
            <h1 className="App-title">菜单</h1>
          </header>
          <div className="products">
            {data.store.listings.map(listing => (
              <Product key={listing.id} productData={listing} />
            ))}
          </div>
        </div>
      )
    }}

  </Query>
)

export default Main

The Product method:

import React from 'react'
import './Product.css'

const Product = (props) => (
  <div className='product-wrap'>
    <div className='product-img-wrap'>
      <img src={props.productData.product.image_url} alt="" className='product-img'/>
    </div>
    <div className='product-info'>
      <span className='product-name'>{props.productData.product.name}</span>
      <span className='product-price'>{props.productData.price}</span>
    </div>
    <button className='add-to-cart'>加入购物车</button>
  </div>
)

export default Product
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,211评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,631评论 0 23
  • 爱表扬六大原则之六:由内而外、推己及人 要表扬孩子,首先要学会表扬自己。表扬是一种能力和修为,只有能量调对的基础上...
    小可以之动阅读 3,882评论 0 52
  • 关于logo中午跟小葡萄讨论了一会,有了最初形态,她说你回去画出来吧,这个过程用语言表达过于繁琐,以图表达。
    一个意外的结尾阅读 3,418评论 1 1
  • 《纪晓岚面子经》 1.不露声色,靠脑子、靠才学征服对手 虎走山还在,山在虎还来。 2.让人转怒为喜,你就是人上人 ...
    在装翅膀的猪阅读 2,454评论 0 0

友情链接更多精彩内容