Skip to content

Instantly share code, notes, and snippets.

@c0depanda
Created February 19, 2019 21:22
Show Gist options
  • Save c0depanda/78beb62963f885a02defa4142e50aa93 to your computer and use it in GitHub Desktop.
Save c0depanda/78beb62963f885a02defa4142e50aa93 to your computer and use it in GitHub Desktop.
// import Vue
import Vue from 'vue';
export default {
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) {
return new Promise((resolve, reject) => {
// 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');
// return success
resolve(response);
}).catch((error) => {
// log error
console.log(error);
// return error
reject(error);
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment