|
1 | 1 | import Link from 'next/link'; |
2 | 2 | import { v4 as uuidv4 } from 'uuid'; |
3 | 3 |
|
4 | | -import { filteredVariantPrice } from 'utils/functions/functions'; |
| 4 | +import { filteredVariantPrice, paddedPrice } from 'utils/functions/functions'; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * Displays all of the products as long as length is defined. |
8 | 8 | * Does a map() over the props array and utilizes uuidv4 for unique key values. |
9 | 9 | * @param {Object} products |
10 | 10 | */ |
11 | | -const IndexProducts = ({ products }) => ( |
12 | | - <section className="container mx-auto bg-white"> |
13 | | - <div id="product-container" className="flex flex-wrap items-center"> |
14 | | - {products ? ( |
15 | | - products.map( |
16 | | - ({ |
17 | | - databaseId, |
18 | | - name, |
19 | | - price, |
20 | | - regularPrice, |
21 | | - salePrice, |
22 | | - onSale, |
23 | | - slug, |
24 | | - image, |
25 | | - variations, |
26 | | - }) => ( |
27 | | - <div key={uuidv4()} className="flex flex-col p-6 md:w-1/2 xl:w-1/4"> |
28 | | - <Link |
29 | | - href={`/produkt/${encodeURIComponent( |
30 | | - slug |
31 | | - )}?id=${encodeURIComponent(databaseId)}`} |
32 | | - > |
33 | | - <a> |
34 | | - {image ? ( |
35 | | - <img |
36 | | - id="product-image" |
37 | | - className="transition duration-500 ease-in-out transform cursor-pointer hover:grow hover:shadow-lg hover:scale-105" |
38 | | - alt={name} |
39 | | - src={image.sourceUrl} |
40 | | - /> |
41 | | - ) : ( |
42 | | - <img |
43 | | - id="product-image" |
44 | | - className="transition duration-500 ease-in-out transform cursor-pointer hover:grow hover:shadow-lg hover:scale-105" |
45 | | - alt={name} |
46 | | - src={process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL} |
47 | | - /> |
48 | | - )} |
49 | | - </a> |
50 | | - </Link> |
| 11 | +const IndexProducts = ({ products }) => { |
| 12 | + return ( |
| 13 | + <section className="container mx-auto bg-white"> |
| 14 | + <div id="product-container" className="flex flex-wrap items-center"> |
| 15 | + {products ? ( |
| 16 | + products.map( |
| 17 | + ({ |
| 18 | + databaseId, |
| 19 | + name, |
| 20 | + price, |
| 21 | + regularPrice, |
| 22 | + salePrice, |
| 23 | + onSale, |
| 24 | + slug, |
| 25 | + image, |
| 26 | + variations, |
| 27 | + }) => { |
| 28 | + // Add padding/empty character after currency symbol here |
| 29 | + if (price) { |
| 30 | + price = paddedPrice(price, 'kr'); |
| 31 | + } |
| 32 | + if (regularPrice) { |
| 33 | + regularPrice = paddedPrice(regularPrice, 'kr'); |
| 34 | + } |
| 35 | + if (salePrice) { |
| 36 | + salePrice = paddedPrice(salePrice, 'kr'); |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <div |
| 41 | + key={uuidv4()} |
| 42 | + className="flex flex-col p-6 md:w-1/2 xl:w-1/4" |
| 43 | + > |
| 44 | + <Link |
| 45 | + href={`/produkt/${encodeURIComponent( |
| 46 | + slug |
| 47 | + )}?id=${encodeURIComponent(databaseId)}`} |
| 48 | + > |
| 49 | + <a> |
| 50 | + {image ? ( |
| 51 | + <img |
| 52 | + id="product-image" |
| 53 | + className="transition duration-500 ease-in-out transform cursor-pointer hover:grow hover:shadow-lg hover:scale-105" |
| 54 | + alt={name} |
| 55 | + src={image.sourceUrl} |
| 56 | + /> |
| 57 | + ) : ( |
| 58 | + <img |
| 59 | + id="product-image" |
| 60 | + className="transition duration-500 ease-in-out transform cursor-pointer hover:grow hover:shadow-lg hover:scale-105" |
| 61 | + alt={name} |
| 62 | + src={ |
| 63 | + process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL |
| 64 | + } |
| 65 | + /> |
| 66 | + )} |
| 67 | + </a> |
| 68 | + </Link> |
51 | 69 |
|
52 | | - <Link |
53 | | - href={`/produkt/${encodeURIComponent( |
54 | | - slug |
55 | | - )}?id=${encodeURIComponent(databaseId)}`} |
56 | | - > |
57 | | - <a> |
58 | | - <div className="flex justify-center pt-3"> |
59 | | - <p className="font-bold text-center cursor-pointer"> |
60 | | - {name} |
61 | | - </p> |
62 | | - </div> |
63 | | - </a> |
64 | | - </Link> |
65 | | - {/* Display sale price when on sale */} |
66 | | - {onSale && ( |
67 | | - <div className="flex justify-center"> |
68 | | - <div className="pt-1 text-gray-900"> |
69 | | - {variations && filteredVariantPrice(price)} |
70 | | - {!variations && salePrice} |
71 | | - </div> |
72 | | - <div className="pt-1 ml-2 text-gray-900 line-through"> |
73 | | - {variations && filteredVariantPrice(price, 'right')} |
74 | | - {!variations && regularPrice} |
75 | | - </div> |
| 70 | + <Link |
| 71 | + href={`/produkt/${encodeURIComponent( |
| 72 | + slug |
| 73 | + )}?id=${encodeURIComponent(databaseId)}`} |
| 74 | + > |
| 75 | + <a> |
| 76 | + <div className="flex justify-center pt-3"> |
| 77 | + <p className="font-bold text-center cursor-pointer"> |
| 78 | + {name} |
| 79 | + </p> |
| 80 | + </div> |
| 81 | + </a> |
| 82 | + </Link> |
| 83 | + {/* Display sale price when on sale */} |
| 84 | + {onSale && ( |
| 85 | + <div className="flex justify-center"> |
| 86 | + <div className="pt-1 text-gray-900"> |
| 87 | + {variations && filteredVariantPrice(price)} |
| 88 | + {!variations && salePrice} |
| 89 | + </div> |
| 90 | + <div className="pt-1 ml-2 text-gray-900 line-through"> |
| 91 | + {variations && filteredVariantPrice(price, 'right')} |
| 92 | + {!variations && regularPrice} |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + )} |
| 96 | + {/* Display regular price when not on sale */} |
| 97 | + {!onSale && ( |
| 98 | + <p className="pt-1 text-center text-gray-900">{price}</p> |
| 99 | + )} |
76 | 100 | </div> |
77 | | - )} |
78 | | - {/* Display regular price when not on sale */} |
79 | | - {!onSale && ( |
80 | | - <p className="pt-1 text-center text-gray-900"> {price}</p> |
81 | | - )} |
82 | | - </div> |
| 101 | + ); |
| 102 | + } |
83 | 103 | ) |
84 | | - ) |
85 | | - ) : ( |
86 | | - <div className="mx-auto text-xl font-bold text-center text-gray-800 no-underline uppercase"> |
87 | | - Ingen produkter funnet |
88 | | - </div> |
89 | | - )} |
90 | | - </div> |
91 | | - </section> |
92 | | -); |
| 104 | + ) : ( |
| 105 | + <div className="mx-auto text-xl font-bold text-center text-gray-800 no-underline uppercase"> |
| 106 | + Ingen produkter funnet |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </section> |
| 111 | + ); |
| 112 | +}; |
93 | 113 |
|
94 | 114 | export default IndexProducts; |
0 commit comments