-
Notifications
You must be signed in to change notification settings - Fork 8
/
[page].js
169 lines (159 loc) · 4.74 KB
/
[page].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
import Head from 'next/head'
import Link from 'next/link'
import { useRouter } from 'next/router'
import ReactPaginate from 'react-paginate'
import styled from 'styled-components'
import ProductListItem from '../../../components/ProductListItem'
export async function getServerSideProps(ctx) {
const categoryPath = ctx.params.category
const categoryName = categoryPath.trim().replace('-', ' ').replace(/(^\w|\s\w)/g, m => m.toUpperCase())
const lowerCaseCategoryName = categoryPath.trim().toLowerCase().replace('-', ' ')
const page = ctx.params.page
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 {
products(filters: { category: { name: { eq: "${categoryName}" }}}, pagination: { page: ${page}, pageSize: 8 } ) {
data {
id
attributes {
title
price
available
image {
data {
attributes {
name
alternativeText
url
}
}
}
discount {
data {
attributes {
discountPercent
discountMultiplier
}
}
}
}
}
meta {
pagination {
total
}
}
}
}
`
})
})
.then(r => {
if (r.status >= 400) {
const err = new Error('Error in pages/products/[category]/[page].js, getServerSideProps() function, .then statement, if (r.status >= 400) condtion')
err.data = r
throw err
}
return r.json()
})
.catch(err => console.error('Error in pages/products/[category]/[page].js, getServerSideProps() function, .catch statement, err object:', err))
const categoryItems = data.data.products.data
const totalItems = data.data.products.meta.pagination.total
return { props: { categoryPath, categoryName, lowerCaseCategoryName, categoryItems, totalItems }}
}
export default function CategoryPage({
categoryPath,
categoryName,
lowerCaseCategoryName,
categoryItems,
totalItems
}) {
const router = useRouter()
const { page } = router.query
const paginate = e => router.push({ pathname: `/products/${categoryPath}/${e.selected + 1}`})
return (
<>
<Head>
<title>{categoryName} - Alimazon</title>
<meta name="description" content={`The BEST ${lowerCaseCategoryName} in the world!!!`} />
</Head>
<DivProducts>
<nav aria-label="breadcrumb">
<ol className="breadcrumb">
<Link href="/"><a className="breadcrumb-item"><li>Home</li></a></Link>
<li className="breadcrumb-item active" aria-current="page">{categoryName}</li>
</ol>
</nav>
<div className="category-items">
{categoryItems.map(item => <ProductListItem key={item.id} item={item} />)}
</div>
<ReactPaginate
forcePage={page - 1}
pageCount={Math.ceil(totalItems / 8)}
pageRangeDisplayed={2}
marginPagesDisplayed={3}
onPageChange={paginate}
previousLabel={'«'}
nextLabel={'»'}
breakLabel={'...'}
activeClassName={'active'}
disableInitialCallback={true}
/>
</DivProducts>
</>
)
}
const DivProducts = styled.div`
grid-area: 2 / 2 / 3 / 3;
display: flex;
flex-direction: column;
> .category-items {
display: grid;
grid-template-columns: repeat(auto-fit, 290px);
grid-row-gap: .5rem;
grid-column-gap: .5rem;
justify-content: center;
}
> :last-child {
align-self: center;
display: flex;
list-style: none;
height: 2.5em;
border: 1px solid black;
border-radius: 5px;
width: fit-content;
align-items: center;
padding: 0;
margin-top: 2.5em;
> li > a {
display: flex;
align-items: center;
height: 100%;
padding: 0 10px;
cursor: pointer;
&:hover {
text-decoration: underline;
}
}
> .active > a {
font-weight: bold;
text-decoration: underline;
pointer-events: none;
}
> .disabled > a {
text-decoration: none;
&:hover {
text-decoration: none;
}
}
}
`