Skip to content

Refactor to follow best practices #1484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions components/Products/ProductsShowAll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,16 @@
</template>

<script setup>
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
import GET_PRODUCTS_FROM_CATEGORY_QUERY from "@/apollo/queries/GET_PRODUCTS_FROM_CATEGORY_QUERY.gql";

import ProductImage from "@/components/Products/ProductImage.vue";
import ProductPrice from "@/components/Products/ProductPrice.vue";

const props = defineProps({
categoryId: { type: String, required: false },
categorySlug: { type: String, required: false },
});

const config = useRuntimeConfig();

const products = computed(() => {
return (
allCategoryProducts.value?.productCategory?.products?.nodes ||
allProducts.value?.products?.nodes ||
[]
);
const props = defineProps({
products: {
type: Array,
required: true,
},
});

/**
Expand All @@ -68,18 +59,6 @@ const productLink = (product) => {
*/
const productImage = (product) =>
product.image ? product.image.sourceUrl : config.public.placeholderImage;

const productVariables = { limit: 99 };
const { data: allProducts } = await useAsyncQuery(
FETCH_ALL_PRODUCTS_QUERY,
productVariables,
);

const categoryVariables = { id: props.categoryId };
const { data: allCategoryProducts } = await useAsyncQuery(
GET_PRODUCTS_FROM_CATEGORY_QUERY,
categoryVariables,
);
</script>

<style scoped>
Expand Down
9 changes: 5 additions & 4 deletions pages/category/[category].vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<template>
<ProductsShowAll
:category-id="route.query.id"
:category-slug="route.params.product"
/>
<ProductsShowAll :products="products" />
</template>

<script setup>
import GET_PRODUCTS_FROM_CATEGORY_QUERY from "@/apollo/queries/GET_PRODUCTS_FROM_CATEGORY_QUERY.gql";
const route = useRoute();

const { data } = await useAsyncQuery(GET_PRODUCTS_FROM_CATEGORY_QUERY, { id: route.query.id });
const products = computed(() => data.value?.productCategory?.products?.nodes || []);

useHead({
title: route.params.category,
titleTemplate: "%s - Nuxt 3 Woocommerce",
Expand Down
7 changes: 6 additions & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<template>
<div>
<IndexHero />
<ProductsShowAll />
<ProductsShowAll :products="products" />
</div>
</template>

<script setup>
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";

const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY);
const products = computed(() => data.value?.products?.nodes || []);

useHead({
title: "Index",
titleTemplate: "%s - Nuxt 3 Woocommerce",
Expand Down
7 changes: 6 additions & 1 deletion pages/products.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<template>
<ProductsShowAll />
<ProductsShowAll :products="products" />
</template>

<script setup>
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";

const { data } = await useAsyncQuery(FETCH_ALL_PRODUCTS_QUERY);
const products = computed(() => data.value?.products?.nodes || []);

useHead({
title: "Products",
titleTemplate: "%s - Nuxt 3 Woocommerce",
Expand Down