Icontem

File: app/components/test-shopping-cart/store/actions.js

Recommend this page to a friend!
  Classes of Sergey Beskorovayniy  >  Vuex Examples  >  app/components/test-shopping-cart/store/actions.js  >  Download  
File: app/components/test-shopping-cart/store/actions.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Vuex Examples
Example apps using Vuex state management pattern
Author: By
Last change:
Date: 2 years ago
Size: 1,048 bytes
 

Contents

Class file image Download
define([
    'app/components/test-shopping-cart/api/shop',
    'app/components/test-shopping-cart/store/mutation-types'
], function (shop, types) {

    var addToCart = function (store, product) {
        if (product.inventory > 0) {
            store.commit(types.ADD_TO_CART, product.id);
        };
    };
    var checkout = function (store, products) {
        var savedCartItems = store.state.cart.added;
        store.commit(types.CHECKOUT_REQUEST);
        shop.buyProducts(
                products,
                function () {
                    store.commit(types.CHECKOUT_SUCCESS);
                },
                function () {
                    store.commit(types.CHECKOUT_FAILURE, savedCartItems);
                }
        );
    };
    var getAllProducts = function (store) {
        shop.getProducts(function (products) {
            store.commit(types.RECEIVE_PRODUCTS, products);
        });
    };
    return {
        addToCart: addToCart,
        checkout: checkout,
        getAllProducts: getAllProducts
    };
});