diff --git a/examples/shopping-cart/store/actions.js b/examples/shopping-cart/store/actions.js index 8623fc81e..91efe9743 100644 --- a/examples/shopping-cart/store/actions.js +++ b/examples/shopping-cart/store/actions.js @@ -1,4 +1,3 @@ -import shop from '../api/shop' import * as types from './mutation-types' export const addToCart = ({ commit }, product) => { @@ -8,19 +7,3 @@ export const addToCart = ({ commit }, product) => { }) } } - -export const checkout = ({ commit, state }, products) => { - const savedCartItems = [...state.cart.added] - commit(types.CHECKOUT_REQUEST) - shop.buyProducts( - products, - () => commit(types.CHECKOUT_SUCCESS), - () => commit(types.CHECKOUT_FAILURE, { savedCartItems }) - ) -} - -export const getAllProducts = ({ commit }) => { - shop.getProducts(products => { - commit(types.RECEIVE_PRODUCTS, { products }) - }) -} diff --git a/examples/shopping-cart/store/getters.js b/examples/shopping-cart/store/getters.js index aa8da0e12..eb2c092d0 100644 --- a/examples/shopping-cart/store/getters.js +++ b/examples/shopping-cart/store/getters.js @@ -1,7 +1,3 @@ -export const checkoutStatus = state => state.cart.checkoutStatus - -export const allProducts = state => state.products.all - export const cartProducts = state => { return state.cart.added.map(({ id, quantity }) => { const product = state.products.all.find(p => p.id === id) diff --git a/examples/shopping-cart/store/modules/cart.js b/examples/shopping-cart/store/modules/cart.js index 9a746c280..28dbc0106 100644 --- a/examples/shopping-cart/store/modules/cart.js +++ b/examples/shopping-cart/store/modules/cart.js @@ -1,3 +1,4 @@ +import shop from '../../api/shop' import * as types from '../mutation-types' // initial state @@ -7,6 +8,24 @@ const state = { 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 }) { @@ -41,5 +60,7 @@ const mutations = { export default { state, + getters, + actions, mutations } diff --git a/examples/shopping-cart/store/modules/products.js b/examples/shopping-cart/store/modules/products.js index a1db7d560..18c712d45 100644 --- a/examples/shopping-cart/store/modules/products.js +++ b/examples/shopping-cart/store/modules/products.js @@ -1,3 +1,4 @@ +import shop from '../../api/shop' import * as types from '../mutation-types' // initial state @@ -5,6 +6,20 @@ const state = { all: [] } +// getters +const getters = { + allProducts: state => state.all +} + +// actions +const actions = { + getAllProducts ({ commit }) { + shop.getProducts(products => { + commit(types.RECEIVE_PRODUCTS, { products }) + }) + } +} + // mutations const mutations = { [types.RECEIVE_PRODUCTS] (state, { products }) { @@ -18,5 +33,7 @@ const mutations = { export default { state, + getters, + actions, mutations }