product page and cart finished
This commit is contained in:
+3
-29
@@ -1,7 +1,8 @@
|
||||
export const state = () => ({
|
||||
products: [],
|
||||
cart: [],
|
||||
checkout: {}
|
||||
checkout: {
|
||||
lineItems: []
|
||||
}
|
||||
})
|
||||
|
||||
export const mutations = {
|
||||
@@ -10,32 +11,5 @@ export const mutations = {
|
||||
},
|
||||
setCheckout (state, data) {
|
||||
state.checkout = data
|
||||
},
|
||||
incrementCart (state, index) {
|
||||
state.cart[index].count++
|
||||
},
|
||||
addCart (state, item) {
|
||||
item.count = 1
|
||||
state.cart.push(item)
|
||||
}
|
||||
}
|
||||
|
||||
export const getters = {
|
||||
getCartItem: state => (id) => {
|
||||
return state.cart.filter(item => (item.id === id))
|
||||
}
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
addItemToCart ({ commit, getters, state }, payload) {
|
||||
return new Promise((resolve) => {
|
||||
const element = getters.getCartItem(payload.id)
|
||||
if (element.length > 0) {
|
||||
commit('incrementCart', state.cart.indexOf(element[0]))
|
||||
} else {
|
||||
commit('addCart', payload)
|
||||
}
|
||||
resolve(getters.getCartItem(payload.id)[0])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user