|
@@ -3,16 +3,14 @@ import shop from '../../api/shop'
|
|
// initial state
|
|
// initial state
|
|
// shape: [{ id, quantity }]
|
|
// shape: [{ id, quantity }]
|
|
const state = {
|
|
const state = {
|
|
- added: [],
|
|
|
|
|
|
+ items: [],
|
|
checkoutStatus: null
|
|
checkoutStatus: null
|
|
}
|
|
}
|
|
|
|
|
|
// getters
|
|
// getters
|
|
const getters = {
|
|
const getters = {
|
|
- checkoutStatus: state => state.checkoutStatus,
|
|
|
|
-
|
|
|
|
cartProducts: (state, getters, rootState) => {
|
|
cartProducts: (state, getters, rootState) => {
|
|
- return state.added.map(({ id, quantity }) => {
|
|
|
|
|
|
+ return state.items.map(({ id, quantity }) => {
|
|
const product = rootState.products.all.find(product => product.id === id)
|
|
const product = rootState.products.all.find(product => product.id === id)
|
|
return {
|
|
return {
|
|
title: product.title,
|
|
title: product.title,
|
|
@@ -32,7 +30,7 @@ const getters = {
|
|
// actions
|
|
// actions
|
|
const actions = {
|
|
const actions = {
|
|
checkout ({ commit, state }, products) {
|
|
checkout ({ commit, state }, products) {
|
|
- const savedCartItems = [...state.added]
|
|
|
|
|
|
+ const savedCartItems = [...state.items]
|
|
commit('setCheckoutStatus', null)
|
|
commit('setCheckoutStatus', null)
|
|
// empty cart
|
|
// empty cart
|
|
commit('setCartItems', { items: [] })
|
|
commit('setCartItems', { items: [] })
|
|
@@ -50,14 +48,14 @@ const actions = {
|
|
addProductToCart ({ state, commit }, product) {
|
|
addProductToCart ({ state, commit }, product) {
|
|
commit('setCheckoutStatus', null)
|
|
commit('setCheckoutStatus', null)
|
|
if (product.inventory > 0) {
|
|
if (product.inventory > 0) {
|
|
- const cartItem = state.added.find(item => item.id === product.id)
|
|
|
|
|
|
+ const cartItem = state.items.find(item => item.id === product.id)
|
|
if (!cartItem) {
|
|
if (!cartItem) {
|
|
commit('pushProductToCart', { id: product.id })
|
|
commit('pushProductToCart', { id: product.id })
|
|
} else {
|
|
} else {
|
|
commit('incrementItemQuantity', cartItem)
|
|
commit('incrementItemQuantity', cartItem)
|
|
}
|
|
}
|
|
// remove 1 item from stock
|
|
// remove 1 item from stock
|
|
- commit('decrementProductInventory', { id: product.id })
|
|
|
|
|
|
+ commit('products/decrementProductInventory', { id: product.id }, { root: true })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -65,19 +63,19 @@ const actions = {
|
|
// mutations
|
|
// mutations
|
|
const mutations = {
|
|
const mutations = {
|
|
pushProductToCart (state, { id }) {
|
|
pushProductToCart (state, { id }) {
|
|
- state.added.push({
|
|
|
|
|
|
+ state.items.push({
|
|
id,
|
|
id,
|
|
quantity: 1
|
|
quantity: 1
|
|
})
|
|
})
|
|
},
|
|
},
|
|
|
|
|
|
incrementItemQuantity (state, { id }) {
|
|
incrementItemQuantity (state, { id }) {
|
|
- const cartItem = state.added.find(item => item.id === id)
|
|
|
|
|
|
+ const cartItem = state.items.find(item => item.id === id)
|
|
cartItem.quantity++
|
|
cartItem.quantity++
|
|
},
|
|
},
|
|
|
|
|
|
setCartItems (state, { items }) {
|
|
setCartItems (state, { items }) {
|
|
- state.added = items
|
|
|
|
|
|
+ state.items = items
|
|
},
|
|
},
|
|
|
|
|
|
setCheckoutStatus (state, status) {
|
|
setCheckoutStatus (state, status) {
|
|
@@ -86,6 +84,7 @@ const mutations = {
|
|
}
|
|
}
|
|
|
|
|
|
export default {
|
|
export default {
|
|
|
|
+ namespaced: true,
|
|
state,
|
|
state,
|
|
getters,
|
|
getters,
|
|
actions,
|
|
actions,
|