-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathcategories.js
More file actions
77 lines (71 loc) · 2.15 KB
/
categories.js
File metadata and controls
77 lines (71 loc) · 2.15 KB
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
import Head from 'next/head'
import { titleIfy , slugify } from '../utils/helpers'
import { DisplayMedium } from '../components'
import CartLink from '../components/CartLink'
import { fetchInventory } from '../utils/inventoryProvider'
function Categories ({ categories = [] }) {
return (
<>
<div className="w-full">
<CartLink />
<Head>
<title>Jamstack ECommerce - All Categories</title>
<meta name="description" content={`Jamstack ECommerce - All categories`} />
<meta property="og:title" content="Jamstack ECommerce - All Categories" key="title" />
</Head>
<div className="
pt-4 sm:pt-10 pb-8
">
<h1 className="text-5xl font-light">All categories</h1>
</div>
<div className="flex flex-col items-center">
{/* <div className="my-4 lg:my-8 flex flex-col lg:flex-row justify-between"> */}
<div className="grid gap-4
lg:grid-cols-3 md:grid-cols-2 grid-cols-1">
{
categories.map((category, index) => (
<DisplayMedium
key={index}
imageSrc={category.image}
subtitle={`${category.itemCount} items`}
title={titleIfy(category.name)}
link={`/category/${slugify(category.name)}`}
/>
))
}
</div>
</div>
</div>
</>
)
}
export async function getStaticProps() {
const inventory = await fetchInventory()
const inventoryCategories = inventory.reduce((acc, next) => {
const categories = next.categories
if(categories) {
categories.forEach(c => {
const index = acc.findIndex(item => item.name === c)
if (index !== -1) {
const item = acc[index]
item.itemCount = item.itemCount + 1
acc[index] = item
} else {
const item = {
name: c,
image: next.image,
itemCount: 1
}
acc.push(item)
}
})
}
return acc
}, [])
return {
props: {
categories: inventoryCategories
}
}
}
export default Categories