text: import shop from '../../api/shop'
  import * as types from '../mutation-types'
  
  // initial state
  // shape: [{ id, quantity }]
  const state = {
    added: [],
    checkoutStatus: null
  }
  
  // getters
  const getters = {
    checkoutStatus: state => state.checkoutStatus
  }
  
  // actions
  const actions = {
    checkout ({ commit, state }, products) {
      //当前的购物车物品暂存一下
      const savedCartItems = [...state.added]
      commit(types.CHECKOUT_REQUEST)
  
      shop.buyProducts(
        products,
        //成功操作
        () => commit(types.CHECKOUT_SUCCESS),
        //失败操作
        () => commit(types.CHECKOUT_FAILURE, { savedCartItems })
      )
    }
  }
  
  // mutations
  const mutations = {
    [types.ADD_TO_CART] (state, { id }) {
      state.lastCheckout = null
      const record = state.added.find(p => p.id === id)
      if (!record) {
        state.added.push({
          id,
          quantity: 1
        })
      } else {
        record.quantity++
      }
    },
  
    [types.CHECKOUT_REQUEST] (state) {
      // clear cart
      state.added = []
      state.checkoutStatus = null
    },
  
    [types.CHECKOUT_SUCCESS] (state) {
      state.checkoutStatus = 'successful'
    },
  
    [types.CHECKOUT_FAILURE] (state, { savedCartItems }) {
      // rollback to the cart saved before sending the request
      state.added = savedCartItems
      state.checkoutStatus = 'failed'
    }
  }
  
  export default {
    state,
    getters,
    actions,
    mutations
  }
  