Last active
January 12, 2019 14:13
-
-
Save c0depanda/3362b502418a71cfb272d36cada97ef1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import Vue | |
import Vue from 'vue'; | |
// import Vuex | |
import Vuex from 'vuex'; | |
// Install the Vuex plugin on vue | |
Vue.use(Vuex); | |
// create a Vuex store instance | |
export const store = new Vuex.Store({ | |
state: { | |
cart: ["bread", "rice", "beans", "turkey"] | |
}, | |
getters: { | |
// Fetch the total number of items in the cart | |
totalNumberOfCartItems: state => { | |
return state.cart.length; | |
}, | |
}, | |
mutations: { | |
// Add item to cart | |
addItemToCart (state, payload) { | |
state.cart.push(payload); | |
}, | |
// Clear items in the cart | |
emtpyCart (state) { | |
state.cart = []; | |
} | |
}, | |
actions: { | |
checkout({commit}, requestObject) { | |
// API Call to submit the items in the cart | |
Vue.http.post('submit', requestObject).then((response) => { | |
// log success | |
console.log(response); | |
// Clear Cart by mutating the state | |
commit('emptyCart'); | |
}).catch((error) => { | |
// log error | |
console.log(error); | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment