Skip to content

Commit 6b27571

Browse files
committed
Refactor single product
1 parent e48e651 commit 6b27571

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PUBLIC_GRAPHQL_URL = "change me"
2-
PLACEHOLDER_SMALL_IMAGE_URL = "change me"
2+
PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL = "https://via.placeholder.com/500"
33
ALGOLIA_APPLICATION_ID = "change me"
44
ALGOLIA_API_KEY = "change me"
55
ALGOLIA_INDEX_NAME = "change me"
Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
<template>
2-
<div v-if="product">
2+
<div v-if="data.product">
33
<section>
44
<div class="container flex flex-wrap items-center pt-4 pb-12 mx-auto">
55
<div
66
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"
77
>
88
<img
9-
v-if="product.image !== undefined"
9+
v-if="data.product.image !== undefined"
1010
id="product-image"
1111
class="h-auto p-8 transition duration-500 ease-in-out transform xl:p-2 md:p-2 lg:p-2 hover:shadow-lg hover:scale-95"
12-
:alt="product.name"
13-
:src="product.image.sourceUrl"
12+
:alt="data.product.name"
13+
:src="data.product.image.sourceUrl"
1414
/>
1515
<img
1616
v-else
1717
id="product-image"
1818
class="h-auto p-8 transition duration-500 ease-in-out transform xl:p-2 md:p-2 lg:p-2 hover:shadow-lg hover:scale-95"
19-
:alt="product.name"
20-
:src="process.env.placeholderSmallImage"
19+
:alt="data.product.name"
20+
:src="config.placeholderImage"
2121
/>
2222
<div class="ml-8">
2323
<p class="text-3xl font-bold text-left">
24-
{{ product.name }}
24+
{{ data.product.name }}
2525
</p>
26-
<div v-if="product.onSale" class="flex">
26+
<div v-if="data.product.onSale" class="flex">
2727
<p class="pt-1 mt-4 text-3xl text-gray-900">
28-
<span v-if="product.variations">
29-
{{ filteredVariantPrice(product.price) }}</span
28+
<span v-if="data.productvariations">
29+
{{ filteredVariantPrice(data.product.price) }}</span
3030
>
31-
<span v-else>{{ product.salePrice }}</span>
31+
<span v-else>{{ data.product.salePrice }}</span>
3232
</p>
3333
<p class="pt-1 pl-8 mt-4 text-2xl text-gray-900 line-through">
34-
<span v-if="product.variations">
35-
{{ filteredVariantPrice(product.price, "right") }}</span
34+
<span v-if="data.productvariations">
35+
{{ filteredVariantPrice(data.product.price, "right") }}</span
3636
>
37-
<span v-else>{{ product.regularPrice }}</span>
37+
<span v-else>{{ data.product.regularPrice }}</span>
3838
</p>
3939
</div>
4040
<p v-else class="pt-1 mt-4 text-2xl text-gray-900">
41-
{{ product.price }}
41+
{{ data.product.price }}
4242
</p>
4343
<br />
4444
<p class="pt-1 mt-4 text-2xl text-gray-900">
45-
{{ stripHTML(product.description) }}
45+
{{ stripHTML(data.product.description) }}
4646
</p>
4747
<p
48-
v-if="product.variations"
48+
v-if="data.product.variations"
4949
class="pt-1 mt-4 text-xl text-gray-900"
5050
>
5151
<span class="py-2">Varianter</span>
@@ -55,7 +55,7 @@
5555
class="block w-64 px-6 py-2 bg-white border border-gray-500 rounded-lg focus:outline-none focus:shadow-outline"
5656
>
5757
<option
58-
v-for="(variation, index) in product.variations.nodes"
58+
v-for="(variation, index) in data.product.variations.nodes"
5959
:key="variation.databaseId"
6060
:value="variation.databaseId"
6161
:selected="index === 0"
@@ -65,7 +65,7 @@
6565
</select>
6666
</p>
6767
<div class="pt-1 mt-2">
68-
<CartAddToCartButton :product="product" />
68+
<CartAddToCartButton :product="data.product" />
6969
</div>
7070
</div>
7171
</div>
@@ -75,12 +75,17 @@
7575
</template>
7676

7777
<script setup>
78+
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
79+
7880
import { stripHTML, filteredVariantPrice } from "@/utils/functions";
7981
80-
defineProps({
81-
product: {
82-
type: [Object],
83-
required: true,
84-
},
82+
const config = useRuntimeConfig();
83+
84+
const props = defineProps({
85+
id: { type: String, required: true },
86+
slug: { type: String, required: true },
8587
});
88+
89+
const variables = { id: props.id, slug: props.slug };
90+
const { data } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
8691
</script>

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineNuxtConfig({
1313
"@nuxtjs/algolia",
1414
],
1515
plugins: ["~/plugins/apollo"],
16-
runtimeConfig: { public: { graphqlURL: process.env.PUBLIC_GRAPHQL_URL } },
16+
runtimeConfig: { public: { graphqlURL: process.env.PUBLIC_GRAPHQL_URL, placeholderImage: process.env.PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL } },
1717
postcss: {
1818
plugins: {
1919
tailwindcss: {},

pages/product/[product].vue

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
<template>
2-
<div v-if="data?.product">
3-
<ProductsSingleProduct :product="data.product" />
4-
</div>
5-
<div v-else>
6-
<SpinnerLoading />
7-
</div>
2+
<ProductsSingleProduct :id="route.query.id" :slug="route.params.product" />
83
</template>
94

105
<script setup>
11-
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
12-
136
const route = useRoute();
147
15-
const variables = { id: route.query.id, slug: route.params.product };
16-
const { data } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
17-
188
useHead({
199
title: route.params.product,
2010
titleTemplate: "%s - Nuxt 3 Woocommerce",

0 commit comments

Comments
 (0)