-
Notifications
You must be signed in to change notification settings - Fork 9
/
[id].js
247 lines (231 loc) · 7.1 KB
/
[id].js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import Head from 'next/head'
import Link from 'next/link'
import styled from 'styled-components'
import ProductSlider from '../../components/productPage/ProductSlider'
import ProductInfo from '../../components/productPage/ProductInfo'
import AddToCart from '../../components/productPage/AddToCart'
import RelatedProducts from '../../components/productPage/RelatedProducts'
import Reviews from '../../components/productPage/Reviews'
export async function getServerSideProps(ctx) {
const data = await fetch(`${
process.env.NODE_ENV === "production"
? process.env.PROD_CMS_URL
: process.env.DEV_CMS_URL
}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query {
product(id: ${ctx.params.id}) {
data {
id
attributes {
title
company
description
price
available
image {
data {
id
attributes {
name
alternativeText
url
formats
}
}
}
category {
data {
attributes {
name
}
}
}
discount {
data {
attributes {
discountPercent
discountMultiplier
}
}
}
relatedProducts {
products {
data {
id
attributes {
title
image {
data {
attributes {
name
url
alternativeText
formats
}
}
}
}
}
}
}
}
}
}
}
`
})
})
.then(r => {
if (r.status >= 400) {
const err = new Error('Error in pages/product-page/[id].js, getServerSideProps() function, first fetch, .then statement, if (r.status >= 400) condition')
err.data = r
throw err
}
return r.json()
})
.catch(err => console.error('Error in pages/product-page/[id].js, getServerSideProps() function, first fetch, .catch statement, err object:', err))
const dataItem = data.data.product.data
const reviewsData = await fetch(`${
process.env.NODE_ENV === "production"
? process.env.PROD_CMS_URL
: process.env.DEV_CMS_URL
}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query {
reviews(filters: { productId: { eq: "${ctx.params.id}" }}) {
data {
id
attributes {
name
reviewText
createdAt
}
}
}
}
`
})
})
.then(r => {
if (r.status >= 400) {
const err = new Error('Error in pages/product-page/[id].js, getServerSideProps() function, second fetch, .then statement, if (r.status >= 400) condition')
err.data = r
throw err
}
return r.json()
})
.catch(err => console.error('Error in pages/product-page/[id].js, getServerSideProps() function, second fetch, .catch statement, err object:', err))
const reviewList = reviewsData.data.reviews.data
return {
props: { dataItem, reviewList }
}
}
export default function ProductPage({ dataItem, reviewList }) {
const id = dataItem.id
const attributes = dataItem.attributes
const title = attributes.title
const company = attributes.company
const description = attributes.description
const price = attributes.price
const available = attributes.available
const category = attributes.category.data.attributes.name
const categoryPath = category.trim().toLowerCase().replace(' ', '-')
const images = attributes.image.data
let discountAttributes
let discountPercent
let discountMultiplier
if (attributes.discount.data !== null) {
discountAttributes = attributes.discount.data.attributes
discountPercent = discountAttributes.discountPercent
discountMultiplier = discountAttributes.discountMultiplier
}
let relatedProducts
if (attributes.relatedProducts !== null) {
relatedProducts = attributes.relatedProducts.products.data
}
return (
<>
<Head>
<title>Buy {title} - Alimazon</title>
<meta name="description" content={`${title} - the LOWEST price, the BEST quality!!!`} />
</Head>
<DivProductPage>
<nav aria-label="breadcrumb" className="nav">
<ol className="breadcrumb">
<li className="breadcrumb-item"><Link href="/"><a>Home</a></Link></li>
<li className="breadcrumb-item">
<Link href="/products/[category]/[page]" as={`/products/${categoryPath}/1`}>
<a>{category}</a>
</Link>
</li>
<li className="breadcrumb-item active" aria-current="page">{title}</li>
</ol>
</nav>
<ProductSlider images={images} />
<ProductInfo
attributes={attributes}
title={title}
company={company}
description={description}
price={price}
discountMultiplier={discountMultiplier}
discountPercent={discountPercent}
/>
<AddToCart id={id} available={available} />
<Reviews id={id} reviewList={reviewList} />
{
attributes.relatedProducts !== null
? (
<RelatedProducts relatedProducts={relatedProducts} />
) : null
}
</DivProductPage>
</>
)
}
const DivProductPage = styled.div`
grid-area: 2 / 2 / 3 / 3;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: auto 1fr auto auto auto;
> .nav {
grid-area: 1 / 1 / 2 / 3;
}
> .carousel-root {
margin-bottom: 2em;
max-width: 400px;
max-height: 400px;
grid-area: 2 / 1 / 3 / 2;
justify-self: center;
> div > .thumbs-wrapper > ul {
padding: 0;
display: flex;
justify-content: center;
> :nth-child(3) {
margin-right: 0;
}
}
}
@media only screen and (max-width: 850px) {
display: flex;
flex-direction: column;
align-items: center;
> .nav {
align-self: flex-start;
}
> .carousel-root {
justify-self: auto;
}
}
`