Skip to content

Commit 1d542d1

Browse files
authored
Merge pull request #986 from w3bdesign/975-refactor-and-improve-code
Refactor code
2 parents 233927f + fc63914 commit 1d542d1

File tree

10 files changed

+101
-80
lines changed

10 files changed

+101
-80
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"

components/Category/CategoryShowAll.vue

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
2-
<div v-if="categories">
2+
<div v-if="data.productCategories">
33
<section class="mx-auto bg-white">
44
<div
55
class="grid gap-2 px-2 pt-2 pb-2 lg:px-0 xl:px-0 md:px-0 lg:grid-cols-4 sm:grid-cols-2 md:grid-cols-3 xs:grid-cols-3"
66
>
7-
<template v-for="nodes in categories">
7+
<template v-for="nodes in data.productCategories">
88
<template v-for="category in nodes">
99
<div v-if="category.slug !== undefined" :key="category.id">
1010
<NuxtLink
@@ -33,10 +33,8 @@
3333
</template>
3434

3535
<script setup>
36-
defineProps({
37-
categories: {
38-
type: [Object],
39-
required: true,
40-
},
41-
});
36+
import FETCH_ALL_CATEGORIES_QUERY from "@/apollo/queries/FETCH_ALL_CATEGORIES_QUERY.gql";
37+
38+
const variables = { limit: 99 };
39+
const { data } = await useAsyncQuery(FETCH_ALL_CATEGORIES_QUERY, variables);
4240
</script>

components/Products/ProductsShowAll.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
2-
<div v-if="products">
2+
<div v-if="data?.products?.nodes">
33
<section>
44
<div id="product-container" class="flex flex-wrap items-center">
5-
<template v-for="product in products">
5+
<template v-for="product in data.products.nodes">
66
<div
77
v-if="product.slug !== undefined"
88
:key="product.id"
@@ -54,14 +54,14 @@
5454
</template>
5555

5656
<script setup>
57+
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
5758
import { filteredVariantPrice } from "@/utils/functions";
5859
59-
defineProps({
60-
products: { type: Array, required: true },
61-
});
62-
6360
const productImage = (product) =>
6461
product.image ? product.image.sourceUrl : process.env.placeholderSmallImage;
62+
63+
const variables = { limit: 99 };
64+
const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY, variables);
6565
</script>
6666

6767
<style scoped>
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>

components/common/CommonButton.vue

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<template>
2+
<div>
3+
<button
4+
class="relative w-48 h-12 px-4 py-2 mt-4 transition ease-in-out delay-75 duration-300 font-bold text-white bg-blue-500 rounded hover:bg-blue-800"
5+
:class="{ disabled: isLoading }"
6+
@click="$emit('CommonButtonClick')"
7+
>
8+
<slot />
9+
<svg
10+
v-if="isLoading"
11+
class="absolute -mt-6 -ml-2 animate-spin"
12+
width="25"
13+
height="25"
14+
viewBox="0 0 24 24"
15+
fill="none"
16+
xmlns="http://www.w3.org/2000/svg"
17+
>
18+
<path
19+
fill-rule="evenodd"
20+
clip-rule="evenodd"
21+
d="M12 0C5.37258 0 0 5.37258 0 12C0 18.6274 5.37258 24 12 24C18.6274 24 24 18.6274 24 12C24 5.37258 18.6274 0 12 0ZM4.14355 13.5165C4.85219 17.2096 8.10023 20 12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C8.0886 4 4.83283 6.80704 4.13728 10.5165L6.72824 10.5071C7.37819 8.20738 9.49236 6.52222 12.0001 6.52222C15.0254 6.52222 17.4779 8.9747 17.4779 12C17.4779 15.0253 15.0254 17.4778 12.0001 17.4778C9.49752 17.4778 7.3869 15.7995 6.73228 13.5071L4.14355 13.5165ZM9.52234 12C9.52234 13.3684 10.6317 14.4778 12.0001 14.4778C13.3685 14.4778 14.4779 13.3684 14.4779 12C14.4779 10.6316 13.3685 9.52222 12.0001 9.52222C10.6317 9.52222 9.52234 10.6316 9.52234 12Z"
22+
fill="white"
23+
/>
24+
</svg>
25+
</button>
26+
</div>
27+
</template>
28+
29+
<script setup>
30+
/* Usage:
31+
* <CommonButton @common-button-click="function name" isLoading="true">Common button</CommonButton>
32+
*/
33+
34+
defineEmits(["CommonButtonClick"]);
35+
36+
defineProps({
37+
isLoading: {
38+
type: Boolean,
39+
required: false,
40+
default: false,
41+
},
42+
});
43+
</script>
44+
45+
<style scoped>
46+
.disabled {
47+
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded opacity-50 cursor-not-allowed;
48+
}
49+
</style>

nuxt.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export default defineNuxtConfig({
1313
"@nuxtjs/algolia",
1414
],
1515
plugins: ["~/plugins/apollo"],
16-
runtimeConfig: { public: { graphqlURL: process.env.PUBLIC_GRAPHQL_URL } },
16+
runtimeConfig: {
17+
public: {
18+
graphqlURL: process.env.PUBLIC_GRAPHQL_URL,
19+
placeholderImage: process.env.PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL,
20+
},
21+
},
1722
postcss: {
1823
plugins: {
1924
tailwindcss: {},

pages/categories.vue

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
<template>
2-
<div v-if="data.productCategories">
3-
<CategoryShowAll :categories="data.productCategories" />
4-
</div>
5-
<div v-else>
6-
<SpinnerLoading />
7-
</div>
2+
<CategoryShowAll />
83
</template>
94

105
<script setup>
11-
import FETCH_ALL_CATEGORIES_QUERY from "@/apollo/queries/FETCH_ALL_CATEGORIES_QUERY.gql";
12-
13-
const variables = { limit: 5 };
14-
const { data } = await useAsyncQuery(FETCH_ALL_CATEGORIES_QUERY, variables);
15-
166
useHead({
177
title: "Categories",
188
titleTemplate: "%s - Nuxt 3 Woocommerce",

pages/index.vue

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
<template>
22
<div>
33
<IndexHero />
4-
<div v-if="data.products">
5-
<ProductsShowAll :products="data.products.nodes" />
6-
</div>
4+
<ProductsShowAll />
75
</div>
86
</template>
9-
10-
<script setup>
11-
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
12-
13-
const variables = { limit: 5 };
14-
const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY, variables);
15-
</script>

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",

pages/products.vue

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
<template>
2-
<div v-if="data.products">
3-
<ProductsShowAll :products="data.products.nodes" />
4-
</div>
2+
<ProductsShowAll />
53
</template>
64

75
<script setup>
8-
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
9-
10-
const variables = { limit: 5 };
11-
const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY, variables);
12-
136
useHead({
147
title: "Products",
158
titleTemplate: "%s - Nuxt 3 Woocommerce",

0 commit comments

Comments
 (0)