Skip to content

Commit 085201b

Browse files
committed
Add product to Pinia state
1 parent 597aeba commit 085201b

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

components/Layout/LayoutCart.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ import GET_CART_QUERY from "@/apollo/queries/GET_CART_QUERY.gql";
4949
5050
import { getCookie } from "@/utils/functions";
5151
52+
import { useCart } from "@/store/useCart";
53+
5254
const cartLength = useState("cartLength", () => 0);
5355
const subTotal = useState("subTotal", "");
5456
const remoteError = useState("remoteError", () => false);
5557
58+
const cart = useCart();
59+
5660
const { data, error, pending, execute } = await useAsyncQuery(GET_CART_QUERY, {
5761
options: { fetchPolicy: "cache-and-network" },
5862
});
@@ -72,12 +76,14 @@ const updateCartDisplay = () => {
7276
remoteError.value = error;
7377
};
7478
75-
onMounted(() => updateCartDisplay());
79+
onBeforeMount(() => updateCartDisplay());
7680
7781
setInterval(() => {
7882
if (process.client && !pending.value && getCookie("woo-session")) {
7983
execute();
8084
updateCartDisplay();
85+
86+
//console.log("Cart content from useCart: ", cart.getCartContent);
8187
}
8288
}, 3000);
8389
</script>

components/Products/ProductsSingleProduct.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql";
8585
8686
import { stripHTML, filteredVariantPrice } from "@/utils/functions";
8787
88+
import { useCart } from "@/store/useCart";
89+
8890
const isLoading = useState("isLoading", () => false);
8991
9092
const config = useRuntimeConfig();
9193
94+
const cart = useCart();
95+
9296
const props = defineProps({
9397
id: { type: String, required: true },
9498
slug: { type: String, required: true },
@@ -107,6 +111,10 @@ const addProductToCart = async (product) => {
107111
108112
const addToCartvariables = { input: productQueryInput };
109113
114+
console.log("Should add cart to product:", product);
115+
cart.addToCart(product);
116+
117+
110118
const { mutate, onDone, onError } = useMutation(ADD_TO_CART_MUTATION, {
111119
addToCartvariables,
112120
});

store/useCart.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ const state = {
88
error: null,
99
};
1010

11-
export const useCart = defineStore("shopState", {
11+
export const useCart = defineStore("cartState", {
1212
state: () => state,
1313
actions: {
14-
addToCart({ item }) {
14+
addToCart(product) {
1515
const foundProductInCartIndex = this.cart.findIndex(
16-
(cartItem) => item.slug === cartItem.slug
16+
(cartproduct) => product.slug === cartproduct.slug
1717
);
1818

1919
if (foundProductInCartIndex > -1) {
2020
this.cart[foundProductInCartIndex].quantity += 1;
2121
} else {
22-
item.quantity = 1;
23-
this.cart.push(item);
22+
product.quantity = 1;
23+
this.cart.push(product);
2424
}
2525
},
26-
removeProductFromCart({ item }) {
27-
this.cart.splice(this.cart.indexOf(item), 1);
26+
removeProductFromCart({ product }) {
27+
this.cart.splice(this.cart.indexOf(product), 1);
2828
},
2929

3030
clearCart() {

0 commit comments

Comments
 (0)