Skip to content

[example] Move module specific getters and actions into each module #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions examples/shopping-cart/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import shop from '../api/shop'
import * as types from './mutation-types'

export const addToCart = ({ commit }, product) => {
Expand All @@ -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 })
})
}
4 changes: 0 additions & 4 deletions examples/shopping-cart/store/getters.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
21 changes: 21 additions & 0 deletions examples/shopping-cart/store/modules/cart.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shop from '../../api/shop'
import * as types from '../mutation-types'

// initial state
Expand All @@ -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 }) {
Expand Down Expand Up @@ -41,5 +60,7 @@ const mutations = {

export default {
state,
getters,
actions,
mutations
}
17 changes: 17 additions & 0 deletions examples/shopping-cart/store/modules/products.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import shop from '../../api/shop'
import * as types from '../mutation-types'

// initial state
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 }) {
Expand All @@ -18,5 +33,7 @@ const mutations = {

export default {
state,
getters,
actions,
mutations
}