Skip to content

Commit a2978d9

Browse files
committed
Update SingleProduct.component.tsx
1 parent 983d0a9 commit a2978d9

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

src/components/Product/SingleProduct.component.tsx

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { filteredVariantPrice, paddedPrice } from '@/utils/functions/functions';
33
import AddToCart, { IProductRootObject } from './AddToCart.component';
44
import LoadingSpinner from '@/components/LoadingSpinner/LoadingSpinner.component';
@@ -45,28 +45,11 @@ interface IVariations {
4545
nodes: IVariationNodes[];
4646
}
4747

48-
interface IProduct {
49-
__typename: string;
50-
id: string;
51-
databaseId: number;
52-
averageRating: number;
53-
slug: string;
54-
description: string;
55-
onSale: boolean;
56-
image: IImage;
57-
name: string;
58-
salePrice?: string;
59-
regularPrice: string;
60-
price: string;
61-
stockQuantity: number;
62-
allPaColors?: IAllPaColors;
63-
allPaSizes?: IAllPaSizes;
64-
variations?: IVariations;
65-
}
66-
6748
const SingleProduct: React.FC<IProductRootObject> = ({ product }) => {
6849
const [isLoading, setIsLoading] = useState<boolean>(true);
69-
const [selectedVariation, setSelectedVariation] = useState<number | undefined>();
50+
const [selectedVariation, setSelectedVariation] = useState<
51+
number | undefined
52+
>();
7053

7154
const placeholderFallBack = 'https://via.placeholder.com/600';
7255

@@ -80,14 +63,18 @@ const SingleProduct: React.FC<IProductRootObject> = ({ product }) => {
8063
}
8164
}, [product.variations]);
8265

83-
let { description, image, name, onSale, price, regularPrice, salePrice } = product;
66+
let { description, image, name, onSale, price, regularPrice, salePrice } =
67+
product;
8468

8569
if (price) price = paddedPrice(price, 'kr');
8670
if (regularPrice) regularPrice = paddedPrice(regularPrice, 'kr');
8771
if (salePrice) salePrice = paddedPrice(salePrice, 'kr');
8872

89-
if (process.browser) {
90-
DESCRIPTION_WITHOUT_HTML = new DOMParser().parseFromString(description, 'text/html').body.textContent;
73+
if (typeof window !== 'undefined') {
74+
DESCRIPTION_WITHOUT_HTML = new DOMParser().parseFromString(
75+
description,
76+
'text/html',
77+
).body.textContent;
9178
}
9279

9380
return (
@@ -112,59 +99,80 @@ const SingleProduct: React.FC<IProductRootObject> = ({ product }) => {
11299
) : (
113100
<img
114101
id="product-image"
115-
src={process.env.NEXT_PUBLIC_PLACEHOLDER_LARGE_IMAGE_URL ?? placeholderFallBack}
102+
src={
103+
process.env.NEXT_PUBLIC_PLACEHOLDER_LARGE_IMAGE_URL ??
104+
placeholderFallBack
105+
}
116106
alt={name}
117107
className="w-full h-auto object-cover rounded-lg shadow-md"
118108
/>
119109
)}
120110
</div>
121111
<div className="flex flex-col space-y-4">
122-
<h1 className="text-3xl font-bold text-center md:text-left">{name}</h1>
112+
<h1 className="text-3xl font-bold text-center md:text-left">
113+
{name}
114+
</h1>
123115
<div className="text-center md:text-left">
124116
{onSale ? (
125117
<div className="flex flex-col md:flex-row items-center md:items-start space-y-2 md:space-y-0 md:space-x-4">
126118
<p className="text-3xl font-bold text-red-600">
127-
{product.variations ? filteredVariantPrice(price, '') : salePrice}
119+
{product.variations
120+
? filteredVariantPrice(price, '')
121+
: salePrice}
128122
</p>
129123
<p className="text-xl text-gray-500 line-through">
130-
{product.variations ? filteredVariantPrice(price, 'right') : regularPrice}
124+
{product.variations
125+
? filteredVariantPrice(price, 'right')
126+
: regularPrice}
131127
</p>
132128
</div>
133129
) : (
134130
<p className="text-2xl font-bold">{price}</p>
135131
)}
136132
</div>
137-
<p className="text-gray-600 text-center md:text-left">{DESCRIPTION_WITHOUT_HTML}</p>
133+
<p className="text-gray-600 text-center md:text-left">
134+
{DESCRIPTION_WITHOUT_HTML}
135+
</p>
138136
{Boolean(product.stockQuantity) && (
139137
<p className="text-sm font-semibold text-center md:text-left">
140138
{product.stockQuantity} på lager
141139
</p>
142140
)}
143141
{product.variations && (
144142
<div className="w-full">
145-
<label htmlFor="variant" className="block text-sm font-medium text-gray-700 mb-2">
143+
<label
144+
htmlFor="variant"
145+
className="block text-sm font-medium text-gray-700 mb-2"
146+
>
146147
Varianter
147148
</label>
148149
<select
149150
id="variant"
150151
name="variant"
151152
className="w-full px-4 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
152-
onChange={(e) => setSelectedVariation(Number(e.target.value))}
153+
onChange={(e) =>
154+
setSelectedVariation(Number(e.target.value))
155+
}
153156
>
154-
{product.variations.nodes.map(({ id, name, databaseId, stockQuantity }) => {
155-
const filteredName = name.split('- ').pop();
156-
return (
157-
<option key={id} value={databaseId}>
158-
{filteredName} - ({stockQuantity} på lager)
159-
</option>
160-
);
161-
})}
157+
{product.variations.nodes.map(
158+
({ id, name, databaseId, stockQuantity }) => {
159+
const filteredName = name.split('- ').pop();
160+
return (
161+
<option key={id} value={databaseId}>
162+
{filteredName} - ({stockQuantity} på lager)
163+
</option>
164+
);
165+
},
166+
)}
162167
</select>
163168
</div>
164169
)}
165170
<div className="flex justify-center md:justify-start">
166171
{product.variations ? (
167-
<AddToCart product={product} variationId={selectedVariation} />
172+
<AddToCart
173+
product={product}
174+
variationId={selectedVariation}
175+
/>
168176
) : (
169177
<AddToCart product={product} />
170178
)}

0 commit comments

Comments
 (0)