Skip to content

Commit a288e11

Browse files
committed
Pass product as prop
1 parent 4b07f16 commit a288e11

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

components/Products/ProductsSingleProduct.vue

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
<template>
22
<div class="product-single-container">
3-
<template v-if="data?.product">
3+
<template v-if="product">
44
<section>
55
<div class="container flex flex-wrap items-center pt-4 pb-12 mx-auto">
66
<div
77
class="grid grid-cols-1 gap-4 mt-8 lg:grid-cols-2 xl:grid-cols-2 md:grid-cols-2 sm:grid-cols-2"
88
>
9-
<ProductsImage
10-
:alt="data.product.name"
11-
:src="data.product.image.sourceUrl"
12-
/>
9+
<ProductsImage :alt="product.name" :src="product.image.sourceUrl" />
1310
<div class="ml-8">
1411
<p class="text-3xl font-bold text-left">
15-
{{ data.product.name }}
12+
{{ product.name }}
1613
</p>
1714
<ProductsPrice
18-
:product="data.product"
15+
:product="product"
1916
:shouldCenterPrice="false"
2017
priceFontSize="big"
2118
/>
2219
<p class="pt-1 mt-6 text-2xl text-gray-900">
23-
{{ stripHTML(data.product.description) }}
20+
{{ stripHTML(product.description) }}
2421
</p>
2522
<p
26-
v-if="data.product.stockQuantity"
23+
v-if="product.stockQuantity"
2724
class="pt-1 mt-4 text-2xl text-gray-900"
2825
>
29-
{{ data.product.stockQuantity }} in stock
26+
{{ product.stockQuantity }} in stock
3027
</p>
3128
<p
32-
v-if="data.product.variations"
29+
v-if="product.variations"
3330
class="pt-1 mt-4 text-xl text-gray-900"
3431
>
3532
<span class="py-2">Varianter</span>
@@ -40,19 +37,19 @@
4037
v-model="selectedVariation"
4138
>
4239
<option
43-
v-for="(variation, index) in data.product.variations.nodes"
40+
v-for="(variation, index) in product.variations.nodes"
4441
:key="variation.databaseId"
4542
:value="variation.databaseId"
4643
:selected="index === 0"
4744
>
48-
{{ filteredVariantName(data.product.name, variation.name) }}
45+
{{ filteredVariantName(product.name, variation.name) }}
4946
({{ variation.stockQuantity }} in stock)
5047
</option>
5148
</select>
5249
</p>
5350
<div class="pt-1 mt-2">
5451
<CommonButton
55-
@common-button-click="addProductToCart(data.product)"
52+
@common-button-click="addProductToCart(product)"
5653
:is-loading="isLoading"
5754
>
5855
ADD TO CART
@@ -80,8 +77,6 @@
8077
8178
import { ref, watch, computed } from "vue";
8279
83-
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
84-
8580
import { stripHTML, filteredVariantName } from "@/utils/functions";
8681
8782
import { useCart } from "@/store/useCart";
@@ -90,23 +85,19 @@ const cart = useCart();
9085
9186
const isLoading = computed(() => cart.loading);
9287
93-
const selectedVariation = ref(); // TODO Pass this value to addProductToCart()
9488
const toast = ref(null);
9589
9690
const props = defineProps({
97-
id: { type: String, required: true },
98-
slug: { type: String, required: true },
91+
product: { type: Object, required: true },
9992
});
10093
101-
const variables = { id: props.id, slug: props.slug };
102-
const { data } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
94+
const selectedVariation = ref();
10395
10496
watch(
105-
() => data.value,
106-
(dataValue) => {
107-
if (dataValue && dataValue.product?.variations?.nodes?.length > 0) {
108-
selectedVariation.value =
109-
dataValue.product?.variations?.nodes[0].databaseId;
97+
() => props.product,
98+
(productValue) => {
99+
if (productValue && productValue.variations?.nodes?.length > 0) {
100+
selectedVariation.value = productValue.variations?.nodes[0].databaseId;
110101
}
111102
},
112103
{ immediate: true }

pages/product/[product].vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<template>
2-
<ProductsSingleProduct :id="route.query.id" :slug="route.params.product" />
2+
<ProductsSingleProduct :product="productData" v-if="productData" />
33
</template>
44

55
<script setup>
6+
import { useAsyncQuery } from "@vue/apollo-composable";
7+
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
68
const route = useRoute();
79
10+
const variables = { id: route.query.id };
11+
const { result } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
12+
const productData = result.value?.product;
13+
814
useHead({
915
title: route.params.product,
1016
titleTemplate: "%s - Nuxt 3 Woocommerce",

0 commit comments

Comments
 (0)