1
0

actions.js 644 B

1234567891011121314151617181920212223242526
  1. import shop from '../api/shop'
  2. import * as types from './mutation-types'
  3. export const addToCart = ({ commit }, product) => {
  4. if (product.inventory > 0) {
  5. commit(types.ADD_TO_CART, {
  6. id: product.id
  7. })
  8. }
  9. }
  10. export const checkout = ({ commit, state }, products) => {
  11. const savedCartItems = [...state.cart.added]
  12. commit(types.CHECKOUT_REQUEST)
  13. shop.buyProducts(
  14. products,
  15. () => commit(types.CHECKOUT_SUCCESS),
  16. () => commit(types.CHECKOUT_FAILURE, { savedCartItems })
  17. )
  18. }
  19. export const getAllProducts = ({ commit }) => {
  20. shop.getProducts(products => {
  21. commit(types.RECEIVE_PRODUCTS, { products })
  22. })
  23. }