Skip to content

Commit fa7f1c5

Browse files
committed
Refactor to follow best practices
1 parent 757ae3e commit fa7f1c5

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

components/Products/ProductsShowAll.vue

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,16 @@
2424
</template>
2525

2626
<script setup>
27-
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
28-
import GET_PRODUCTS_FROM_CATEGORY_QUERY from "@/apollo/queries/GET_PRODUCTS_FROM_CATEGORY_QUERY.gql";
29-
3027
import ProductImage from "@/components/Products/ProductImage.vue";
3128
import ProductPrice from "@/components/Products/ProductPrice.vue";
3229
33-
const props = defineProps({
34-
categoryId: { type: String, required: false },
35-
categorySlug: { type: String, required: false },
36-
});
37-
3830
const config = useRuntimeConfig();
3931
40-
const products = computed(() => {
41-
return (
42-
allCategoryProducts.value?.productCategory?.products?.nodes ||
43-
allProducts.value?.products?.nodes ||
44-
[]
45-
);
32+
const props = defineProps({
33+
products: {
34+
type: Array,
35+
required: true,
36+
},
4637
});
4738
4839
/**
@@ -68,18 +59,6 @@ const productLink = (product) => {
6859
*/
6960
const productImage = (product) =>
7061
product.image ? product.image.sourceUrl : config.public.placeholderImage;
71-
72-
const productVariables = { limit: 99 };
73-
const { data: allProducts } = await useAsyncQuery(
74-
FETCH_ALL_PRODUCTS_QUERY,
75-
productVariables,
76-
);
77-
78-
const categoryVariables = { id: props.categoryId };
79-
const { data: allCategoryProducts } = await useAsyncQuery(
80-
GET_PRODUCTS_FROM_CATEGORY_QUERY,
81-
categoryVariables,
82-
);
8362
</script>
8463

8564
<style scoped>

pages/category/[category].vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<template>
2-
<ProductsShowAll
3-
:category-id="route.query.id"
4-
:category-slug="route.params.product"
5-
/>
2+
<ProductsShowAll :products="products" />
63
</template>
74

85
<script setup>
6+
import GET_PRODUCTS_FROM_CATEGORY_QUERY from "@/apollo/queries/GET_PRODUCTS_FROM_CATEGORY_QUERY.gql";
97
const route = useRoute();
108
9+
const { data } = await useAsyncQuery(GET_PRODUCTS_FROM_CATEGORY_QUERY, { id: route.query.id });
10+
const products = computed(() => data.value?.productCategory?.products?.nodes || []);
11+
1112
useHead({
1213
title: route.params.category,
1314
titleTemplate: "%s - Nuxt 3 Woocommerce",

pages/index.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<template>
22
<div>
33
<IndexHero />
4-
<ProductsShowAll />
4+
<ProductsShowAll :products="products" />
55
</div>
66
</template>
77

88
<script setup>
9+
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
10+
11+
const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY);
12+
const products = computed(() => data.value?.products?.nodes || []);
13+
914
useHead({
1015
title: "Index",
1116
titleTemplate: "%s - Nuxt 3 Woocommerce",

pages/products.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<template>
2-
<ProductsShowAll />
2+
<ProductsShowAll :products="products" />
33
</template>
44

55
<script setup>
6+
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
7+
8+
const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY);
9+
const products = computed(() => data.value?.products?.nodes || []);
10+
611
useHead({
712
title: "Products",
813
titleTemplate: "%s - Nuxt 3 Woocommerce",

0 commit comments

Comments
 (0)