Skip to content

Commit 8529bf0

Browse files
authored
Merge pull request #1011 from w3bdesign/1009-pinia-cart
Add Pinia store
2 parents 47b7e0e + 5f2e53b commit 8529bf0

File tree

9 files changed

+203
-79
lines changed

9 files changed

+203
-79
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PUBLIC_GRAPHQL_URL = "change me"
22
PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL = "https://via.placeholder.com/500"
3+
PUBLIC_CURRENCY_SYMBOL = "kr"
34
ALGOLIA_APPLICATION_ID = "change me"
45
ALGOLIA_API_KEY = "change me"
56
ALGOLIA_INDEX_NAME = "change me"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
## Features
1818

19-
- Nuxt 3
19+
- Nuxt 3
20+
- Vue 3 and Composition API
2021
- Tailwind CSS
2122
- Nuxt Apollo
23+
- Pinia with pinia-plugin-persistedstate for cart and state management
2224
- Responsive design
2325
- Support for simple and variable products
2426
- CSS animations and transitions

components/Cart/CartContents.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,23 @@
5858
import GET_CART_QUERY from "@/apollo/queries/GET_CART_QUERY.gql";
5959
import UPDATE_CART_MUTATION from "@/apollo/mutations/UPDATE_CART_MUTATION.gql";
6060
61+
import { useCart } from "@/store/useCart";
62+
6163
const isRemoving = useState("isRemoving", () => false);
6264
65+
const cart = useCart();
66+
6367
defineProps({
6468
showCheckoutButton: { type: Boolean, required: false, default: false },
6569
});
6670
6771
const { data } = await useAsyncQuery(GET_CART_QUERY);
6872
69-
const handleRemoveProduct = ({ key }) => {
73+
const handleRemoveProduct = (product) => {
7074
const updatedItems = [];
7175
76+
const { key } = product;
77+
7278
updatedItems.push({
7379
key,
7480
quantity: 0,
@@ -82,6 +88,8 @@ const handleRemoveProduct = ({ key }) => {
8288
},
8389
};
8490
91+
cart.removeProductFromCart(product);
92+
8593
const { mutate, onDone, onError } = useMutation(UPDATE_CART_MUTATION, {
8694
variables,
8795
});

components/Layout/LayoutCart.vue

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
>
3737
{{ cartLength }}
3838
</span>
39-
<span class="text-white lg:text-black">Total: {{ subTotal }}</span>
39+
<span class="text-white lg:text-black"
40+
>Total: {{ config.currencySymbol }} {{ subTotal }}</span
41+
>
4042
</div>
4143
</transition>
4244
</NuxtLink>
@@ -49,10 +51,16 @@ import GET_CART_QUERY from "@/apollo/queries/GET_CART_QUERY.gql";
4951
5052
import { getCookie } from "@/utils/functions";
5153
54+
import { useCart } from "@/store/useCart";
55+
5256
const cartLength = useState("cartLength", () => 0);
5357
const subTotal = useState("subTotal", "");
5458
const remoteError = useState("remoteError", () => false);
5559
60+
const config = useRuntimeConfig();
61+
62+
const cart = useCart();
63+
5664
const { data, error, pending, execute } = await useAsyncQuery(GET_CART_QUERY, {
5765
options: { fetchPolicy: "cache-and-network" },
5866
});
@@ -62,22 +70,21 @@ const updateCartDisplay = () => {
6270
return;
6371
}
6472
65-
cartLength.value = data.value.cart.contents.nodes.reduce(
66-
(accumulator, argument) => accumulator + argument.quantity,
67-
0
68-
);
73+
cartLength.value = cart.getCartQuantity;
6974
70-
subTotal.value = data.value.cart.total;
75+
subTotal.value = cart.getCartTotal;
7176
7277
remoteError.value = error;
7378
};
7479
75-
onMounted(() => updateCartDisplay());
80+
onBeforeMount(() => {
81+
execute();
82+
updateCartDisplay();
83+
});
7684
7785
setInterval(() => {
7886
if (process.client && !pending.value && getCookie("woo-session")) {
79-
execute();
8087
updateCartDisplay();
8188
}
82-
}, 3000);
89+
}, 1000);
8390
</script>

components/Products/ProductsSingleProduct.vue

Lines changed: 6 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,8 @@ const addProductToCart = async (product) => {
107111
108112
const addToCartvariables = { input: productQueryInput };
109113
114+
cart.addToCart(product);
115+
110116
const { mutate, onDone, onError } = useMutation(ADD_TO_CART_MUTATION, {
111117
addToCartvariables,
112118
});

nuxt.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineNuxtConfig({
88
css: ["~/assets/css/main.css", "~/assets/css/animate.min.css"],
99
modules: [
1010
"@pinia/nuxt",
11+
"@pinia-plugin-persistedstate/nuxt",
1112
"@nuxtjs/apollo",
1213
"@formkit/nuxt",
1314
"@nuxtjs/algolia",
@@ -19,6 +20,7 @@ export default defineNuxtConfig({
1920
graphqlURL: process.env.PUBLIC_GRAPHQL_URL,
2021
indexName: process.env.PUBLIC_ALGOLIA_INDEX_NAME,
2122
placeholderImage: process.env.PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL,
23+
currencySymbol: process.env.PUBLIC_CURRENCY_SYMBOL,
2224
},
2325
},
2426
postcss: {

0 commit comments

Comments
 (0)