|
6 | 6 | :class="shouldCenterPrice ? 'justify-center' : ''" |
7 | 7 | > |
8 | 8 | <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) }} |
11 | 10 | </p> |
12 | 11 | <p |
13 | | - v-if="hasVariations" |
14 | 12 | class="pt-1 pl-4 mt-4 text-gray-900 line-through" |
15 | 13 | :class="getSaleFontSizeClass" |
16 | 14 | > |
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) }} |
26 | 16 | </p> |
27 | 17 | </div> |
28 | 18 | <p |
29 | 19 | 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' : ''" |
32 | 22 | > |
33 | | - {{ formatPrice(nonSalePrice) }} |
| 23 | + {{ formatPrice(displayPrice) }} |
34 | 24 | </p> |
35 | 25 | </div> |
36 | 26 | </template> |
37 | 27 |
|
38 | 28 | <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"; |
64 | 35 |
|
65 | 36 | 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 | + } |
74 | 62 | }); |
75 | 63 |
|
76 | 64 | const getFontSizeClass = computed(() => { |
|
0 commit comments