Skip to content

Commit 9838d55

Browse files
committed
Refactor price display
1 parent 01b1059 commit 9838d55

File tree

3 files changed

+39
-58
lines changed

3 files changed

+39
-58
lines changed

components/Products/ProductPrice.vue

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,59 @@
66
:class="shouldCenterPrice ? 'justify-center' : ''"
77
>
88
<p class="pt-1 mt-4 text-gray-900" :class="getFontSizeClass">
9-
<span v-if="hasVariations"> {{ formatPrice(variantPrice) }}</span>
10-
<span v-else>{{ formatPrice(salePrice) }}</span>
9+
{{ formatPrice(displayPrice) }}
1110
</p>
1211
<p
13-
v-if="hasVariations"
1412
class="pt-1 pl-4 mt-4 text-gray-900 line-through"
1513
:class="getSaleFontSizeClass"
1614
>
17-
>
18-
{{ formatPrice(variantPrice) }}
19-
</p>
20-
<p
21-
v-else
22-
class="pt-1 pl-4 mt-4 text-gray-900 line-through"
23-
:class="getSaleFontSizeClass"
24-
>
25-
{{ formatPrice(regularPrice) }}
15+
{{ formatPrice(basePrice) }}
2616
</p>
2717
</div>
2818
<p
2919
v-else
30-
class="flex justify-center pt-1 mt-4 text-gray-900"
31-
:class="getFontSizeClass"
20+
class="flex pt-1 mt-4 text-2xl text-gray-900"
21+
:class="shouldCenterPrice ? 'justify-center' : ''"
3222
>
33-
{{ formatPrice(nonSalePrice) }}
23+
{{ formatPrice(displayPrice) }}
3424
</p>
3525
</div>
3626
</template>
3727

3828
<script setup>
39-
/**
40-
* Vue.js component that renders a product's price.
41-
*
42-
* @typedef {Object} PriceComponentProps
43-
* @property {string} [variantPrice] - The price of a product's variant.
44-
* @property {boolean} onSale - Whether or not the product is on sale.
45-
* @property {boolean} hasVariations - Whether or not the product has variations.
46-
* @property {string} [salePrice] - The sale price of a product.
47-
* @property {string} regularPrice - The regular price of a product.
48-
* @property {string} nonSalePrice - The price of a product that is not on sale.
49-
* @property {string} [priceFontSize] - The font size of the price.
50-
* @property {boolean} [shouldCenterPrice] - Whether or not the price should be centered.
51-
*
52-
* @typedef {Object} PriceComponentComputedProperties
53-
* @property {string} getFontSizeClass - The font size CSS class for the regular price.
54-
* @property {string} getSaleFontSizeClass - The font size CSS class for the sale price.
55-
*
56-
* @typedef {Object} PriceComponentExports
57-
* @property {PriceComponentProps} props - The component's props.
58-
* @property {PriceComponentComputedProperties} computed - The component's computed properties.
59-
*
60-
* @type {PriceComponentExports}
61-
*/
62-
63-
import { formatPrice } from "@/utils/functions";
29+
import { computed } from "vue";
30+
import {
31+
formatPrice,
32+
filteredVariantPrice,
33+
hasVariations,
34+
} from "@/utils/functions";
6435
6536
const props = defineProps({
66-
variantPrice: { type: String, required: false },
67-
onSale: { type: Boolean, required: true },
68-
hasVariations: { type: Boolean, required: true },
69-
salePrice: { type: String, required: false },
70-
regularPrice: { type: String, required: true },
71-
nonSalePrice: { type: String, required: true },
72-
priceFontSize: { type: String, required: false },
73-
shouldCenterPrice: { type: Boolean, required: false },
37+
product: Object,
38+
priceFontSize: String,
39+
shouldCenterPrice: Boolean,
40+
});
41+
42+
const onSale = computed(() => props.product.onSale);
43+
44+
const productHasVariations = computed(() => hasVariations(props.product));
45+
46+
const basePrice = computed(() => {
47+
if (productHasVariations.value) {
48+
return filteredVariantPrice(props.product.price);
49+
} else {
50+
return props.product.regularPrice;
51+
}
52+
});
53+
54+
const displayPrice = computed(() => {
55+
if (onSale.value) {
56+
return productHasVariations.value
57+
? filteredVariantPrice(props.product.price)
58+
: props.product.salePrice;
59+
} else {
60+
return props.product.price;
61+
}
7462
});
7563
7664
const getFontSizeClass = computed(() => {

components/Products/ProductsShowAll.vue

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,9 @@
7373
</div>
7474
</NuxtLink>
7575
<ProductPrice
76-
:variantPrice="filteredVariantPrice(product.price)"
77-
:onSale="product.onSale"
78-
:hasVariations="hasVariations(product)"
79-
:salePrice="product.salePrice"
80-
:regularPrice="product.regularPrice"
81-
:nonSalePrice="product.price"
76+
:product="product"
8277
priceFontSize="normal"
83-
shouldCenterPrice="true"
78+
:shouldCenterPrice="true"
8479
/>
8580
</div>
8681
</template>

components/Products/ProductsSingleProduct.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
<p class="text-3xl font-bold text-left">
99
{{ data.product.name }}
1010
</p>
11-
<ProductPrice :variantPrice="filteredVariantPrice(data.product.price)" :onSale="data.product.onSale"
12-
:hasVariations="hasVariations(data)" :salePrice="data.product.salePrice"
13-
:regularPrice="data.product.regularPrice" :nonSalePrice="data.product.price" priceFontSize="big" />
11+
<ProductPrice :product="data.product" :shouldCenterPrice="false" priceFontSize="big" />
1412
<p class="pt-1 mt-6 text-2xl text-gray-900">
1513
{{ stripHTML(data.product.description) }}
1614
</p>

0 commit comments

Comments
 (0)