Skip to content

Commit b56198b

Browse files
committed
.
1 parent f810b1d commit b56198b

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

components/Cart/CartContents.vue

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
2-
<div v-if="cartItems.length">
2+
<div v-if="isLoading">
3+
<h2 class="mt-64 text-3xl text-center">Loading cart...</h2>
4+
</div>
5+
<div v-else-if="error">
6+
<h2 class="mt-64 text-3xl text-center text-red-500">Error loading cart. Please try again.</h2>
7+
</div>
8+
<div v-else-if="cartItems.length">
39
<h1 class="h-10 p-6 text-3xl font-bold text-center">Cart</h1>
410
<section class="mt-10">
511
<CartItem
@@ -23,11 +29,13 @@
2329
* @module CartContents
2430
* @returns {Object} The Vue.js component object.
2531
*/
26-
import { computed } from 'vue';
32+
import { computed, ref, onMounted } from 'vue';
2733
import { useCart } from "@/store/useCart";
2834
import CommonButton from '@/components/common/CommonButton.vue';
2935
3036
const cart = useCart();
37+
const isLoading = ref(true);
38+
const error = ref(null);
3139
3240
const cartItems = computed(() => cart.cart);
3341
@@ -45,6 +53,17 @@ const handleRemoveProduct = async (key) => {
4553
// without exposing the error details
4654
}
4755
};
56+
57+
onMounted(async () => {
58+
try {
59+
await cart.refetch();
60+
} catch (err) {
61+
console.error('Error fetching cart:', err);
62+
error.value = err;
63+
} finally {
64+
isLoading.value = false;
65+
}
66+
});
4867
</script>
4968

5069
<style scoped>

components/Layout/LayoutCart.vue

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<template>
2-
<div v-if="cart.error">
3-
<span class="text-xl text-red-500">
4-
Error fetching cart. Please refresh the page.
5-
</span>
6-
</div>
7-
<div v-else class="mt-4 lg:mt-0">
8-
<NuxtLink to="/cart">
2+
<div class="mt-4 lg:mt-0">
3+
<div v-if="isLoading">
4+
<span class="text-xl text-gray-500">Loading cart...</span>
5+
</div>
6+
<div v-else-if="error">
7+
<span class="text-xl text-red-500">
8+
Error fetching cart. Please refresh the page.
9+
</span>
10+
</div>
11+
<NuxtLink v-else to="/cart">
912
<transition name="cart">
1013
<span
11-
v-if="cartLength && !cart.loading"
14+
v-if="cartLength > 0"
1215
class="text-xl text-white no-underline lg:text-black is-active"
1316
>
1417
<span class="hidden lg:block">
@@ -29,7 +32,7 @@
2932
</span>
3033
</transition>
3134
<transition name="cart">
32-
<div v-if="cartLength && !cart.loading">
35+
<div v-if="cartLength > 0">
3336
<span
3437
class="absolute w-6 h-6 pb-2 ml-16 -mt-12 text-center text-black bg-white lg:text-white lg:bg-black rounded-full lg:ml-14"
3538
>
@@ -45,12 +48,24 @@
4548
</template>
4649

4750
<script setup>
48-
import { computed } from "vue";
51+
import { ref, computed, onMounted } from "vue";
4952
import { useCart } from "@/store/useCart";
5053
import { formatPrice } from "@/utils/functions";
5154
5255
const cart = useCart();
56+
const isLoading = ref(true);
57+
const error = ref(null);
5358
5459
const cartLength = computed(() => cart.cartQuantity);
5560
const cartSubtotal = computed(() => cart.cartSubtotal);
61+
62+
onMounted(async () => {
63+
try {
64+
await cart.refetch();
65+
} catch (err) {
66+
error.value = err;
67+
} finally {
68+
isLoading.value = false;
69+
}
70+
});
5671
</script>

nuxt.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineNuxtConfig({
1313
"nuxt-icon",
1414
"@nuxt/image",
1515
],
16-
plugins: ["~/plugins/apollo"],
16+
plugins: ["~/plugins/apollo", "~/plugins/cartUpdater"],
1717
runtimeConfig: {
1818
public: {
1919
graphqlURL: process.env.PUBLIC_GRAPHQL_URL,

plugins/cartUpdater.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useCart } from '@/store/useCart';
2+
3+
export default defineNuxtPlugin((nuxtApp) => {
4+
nuxtApp.hook('app:created', () => {
5+
const cart = useCart();
6+
7+
// Refetch cart data on initial load
8+
cart.refetch();
9+
10+
// Refetch cart data on route change
11+
nuxtApp.$router.beforeEach((to, from, next) => {
12+
cart.refetch();
13+
next();
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)