diff options
author | Mateja <mail@matejamaric.com> | 2021-07-25 00:55:53 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-07-25 00:55:53 +0200 |
commit | 2e4678c40a7c92a981bf67845a2888893ea9b758 (patch) | |
tree | 70f14941890823db362e8af543e7d6c84618f3ea /client/src/store | |
parent | 54e471b5741a7569001067e476467c4ff6f6a7de (diff) | |
download | mevn-ecommerce-2e4678c40a7c92a981bf67845a2888893ea9b758.tar.gz mevn-ecommerce-2e4678c40a7c92a981bf67845a2888893ea9b758.zip |
Boilerplate checkout cart.
Diffstat (limited to 'client/src/store')
-rw-r--r-- | client/src/store/index.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/client/src/store/index.js b/client/src/store/index.js index d2fe9b7..2454977 100644 --- a/client/src/store/index.js +++ b/client/src/store/index.js @@ -3,16 +3,42 @@ import axios from 'axios'; export default createStore({ state: { - products: [] + products: [], + cart: [] }, getters: { getProducts(state) { return state.products; + }, + getCart(state) { + return state.cart; + }, + getCartSize(state) { + let sum = 0; + state.cart.forEach(x => sum += x.quantity); + return sum; + }, + getCartPrice(state) { + let sum = 0; + state.cart.forEach(x => sum += x.price * x.quantity); + return sum; } }, mutations: { setProducts(state, products) { state.products = products; + }, + addToCart(state, product) { + let foundProduct = state.cart.find(x => x.id == product._id); + if (foundProduct) + foundProduct.quantity++; + else + state.cart.push({ + id: product._id, + name: product.name, + price: product.price, + quantity: 1 + }); } }, actions: { |