Skip to content

Commit ada6b78

Browse files
committed
Refactor and improve ProductsShowAll
1 parent ec4f583 commit ada6b78

File tree

2 files changed

+50
-64
lines changed

2 files changed

+50
-64
lines changed

components/Checkout/CheckoutForm.vue

+9
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";
4141
4242
import CHECKOUT_MUTATION from "@/apollo/mutations/CHECKOUT_MUTATION.gql";
4343
44+
/**
45+
* Returns an input string with its first character capitalized.
46+
*
47+
* @param {string} input - The string to capitalize the first character of.
48+
* @return {string} The input string with the first character capitalized.
49+
*/
4450
const upperCaseFirstChar = (input) =>
4551
input.charAt(0).toUpperCase() + input.slice(1);
4652
4753
const paymentMethod = "cod";
4854
55+
/**
56+
* Handles the submission of a checkout form with the provided user information.
57+
*/
4958
const handleSubmit = ({
5059
firstName,
5160
lastName,

components/Products/ProductsShowAll.vue

+41-64
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,25 @@
11
<template>
2-
<template v-if="allCategoryProducts?.productCategory?.products?.nodes">
3-
<section>
4-
<div id="product-container" class="flex flex-wrap items-center">
5-
<template
6-
v-for="product in allCategoryProducts.productCategory.products.nodes"
2+
<div id="product-container" class="flex flex-wrap items-center">
3+
<template v-for="product in products" :key="product.id">
4+
<div class="flex flex-col mt-6 sm:w-1/2 md:w-1/3 lg:w-1/4 lg:mr-4">
5+
<NuxtLink
6+
class="text-black cursor-pointer hover:underline"
7+
:to="productLink(product)"
78
>
8-
<div
9-
v-if="product.slug"
10-
:key="product.id"
11-
class="flex flex-col mt-6 sm:w1/2 md:w-1/3 lg:w-1/4 lg:mr-4"
12-
>
13-
<NuxtLink
14-
class="text-black cursor-pointer hover:underline"
15-
:to="{
16-
path: '/product/' + product.slug,
17-
query: { id: product.databaseId },
18-
}"
19-
>
20-
<ProductImage :alt="product.name" :src="productImage(product)" />
21-
<div class="flex justify-center pt-3">
22-
<p class="text-2xl font-bold text-center cursor-pointer">
23-
{{ product.name }}
24-
</p>
25-
</div>
26-
</NuxtLink>
27-
<ProductPrice
28-
:product="product"
29-
priceFontSize="normal"
30-
:shouldCenterPrice="true"
31-
/>
9+
<ProductImage :alt="product.name" :src="productImage(product)" />
10+
<div class="flex justify-center pt-3">
11+
<p class="text-2xl font-bold text-center cursor-pointer">
12+
{{ product.name }}
13+
</p>
3214
</div>
33-
</template>
15+
</NuxtLink>
16+
<ProductPrice
17+
:product="product"
18+
priceFontSize="normal"
19+
:shouldCenterPrice="true"
20+
/>
3421
</div>
35-
</section>
36-
</template>
37-
<div v-else>
38-
<section>
39-
<div id="product-container" class="flex flex-wrap items-center">
40-
<template v-for="product in allProducts.products.nodes">
41-
<div
42-
v-if="product.slug"
43-
:key="product.id"
44-
class="flex flex-col mt-6 sm:w1/2 md:w-1/3 lg:w-1/4 lg:mr-4"
45-
>
46-
<NuxtLink
47-
class="text-black cursor-pointer hover:underline"
48-
:to="{
49-
path: '/product/' + product.slug,
50-
query: { id: product.databaseId },
51-
}"
52-
>
53-
<ProductImage :alt="product.name" :src="productImage(product)" />
54-
<div class="flex justify-center pt-3">
55-
<p class="text-2xl font-bold text-center cursor-pointer">
56-
{{ product.name }}
57-
</p>
58-
</div>
59-
</NuxtLink>
60-
<ProductPrice
61-
:product="product"
62-
priceFontSize="normal"
63-
:shouldCenterPrice="true"
64-
/>
65-
</div>
66-
</template>
67-
</div>
68-
</section>
22+
</template>
6923
</div>
7024
</template>
7125

@@ -83,6 +37,29 @@ const props = defineProps({
8337
8438
const config = useRuntimeConfig();
8539
40+
const products = computed(() => {
41+
return (
42+
allCategoryProducts.value?.productCategory?.products?.nodes ||
43+
allProducts.value?.products?.nodes ||
44+
[]
45+
);
46+
});
47+
48+
/**
49+
* Returns the path and query parameters for a product link.
50+
*
51+
* @param {Object} product - Object containing product information.
52+
* @param {string} product.slug - The product's URL slug.
53+
* @param {number} product.databaseId - The product's database ID.
54+
* @return {Object} An object containing the product's path and query parameters.
55+
*/
56+
const productLink = (product) => {
57+
return {
58+
path: "/product/" + product.slug,
59+
query: { id: product.databaseId },
60+
};
61+
};
62+
8663
/**
8764
* Returns the source URL of a product image or a placeholder image if the product does not have an image.
8865
*

0 commit comments

Comments
 (0)