Skip to content

Commit

Permalink
Merge pull request jamstack-cms#2 from joeynimu/feature-index-and-cat…
Browse files Browse the repository at this point in the history
…egories-pages

added backend for index/home and categories pages
  • Loading branch information
joeynimu committed Mar 1, 2021
2 parents f2a4ca4 + ac55e42 commit f180fde
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 112 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -6,6 +6,7 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"generate": "npx prisma generate",
"push-db": "npx prisma db push --preview-feature",
"seed-db": "npx prisma db seed --preview-feature",
"db-studio": "npx prisma studio"
Expand All @@ -14,6 +15,7 @@
"@prisma/client": "2.15.0",
"@stripe/react-stripe-js": "^1.1.2",
"@stripe/stripe-js": "^1.11.0",
"@types/react": "^17.0.2",
"autoprefixer": "^10.1.0",
"next": "10.0.4",
"postcss": "^8.2.2",
Expand Down
74 changes: 0 additions & 74 deletions pages/categories.js

This file was deleted.

70 changes: 70 additions & 0 deletions pages/categories.tsx
@@ -0,0 +1,70 @@
import Head from "next/head"
import { titleIfy, slugify } from "../utils/helpers"
import { DisplayMedium } from "../components"
import CartLink from "../components/CartLink"
import prisma from "../lib/prisma-client"
import { FC } from "react"
import { Category, Product } from "@prisma/client"

type categoryType = Category & {
products: Product[]
}

type Props = {
categories: categoryType[]
}

const Categories: FC<Props> = ({ 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 pb-8 sm:pt-10">
<h1 className="text-5xl font-light">All categories</h1>
</div>
<div className="flex flex-col items-center">
{/* <div className="flex flex-col justify-between my-4 lg:my-8 lg:flex-row"> */}
<div className="grid grid-cols-1 gap-4 lg:grid-cols-3 md:grid-cols-2">
{categories.map((category, index) => (
<DisplayMedium
key={index}
imageSrc={category.image}
subtitle={`${category.products.length} items`}
title={titleIfy(category.name)}
link={`/category/${slugify(category.name)}`}
/>
))}
</div>
</div>
</div>
</>
)
}

export async function getStaticProps() {
const categories = await prisma.category.findMany({
include: {
products: true,
},
})

return {
props: {
categories,
},
}
}

export default Categories
68 changes: 38 additions & 30 deletions pages/index.js → pages/index.tsx
Expand Up @@ -7,11 +7,29 @@ import {
DisplaySmall,
DisplayMedium,
} from "../components"
import { FC } from "react"
import { titleIfy, slugify } from "../utils/helpers"
import { fetchInventory } from "../utils/inventoryProvider"
import CartLink from "../components/CartLink"
import prismaClient from "../lib/prisma-client"
import { Category, Product } from "@prisma/client"

const Home = ({ inventoryData = [], categories: categoryData = [] }) => {
type inventoryDataType = Product & {
categories: Category[]
}

type categoryDataType = Category & {
products: Product[]
}

type Props = {
inventoryData: inventoryDataType[] | []
categories: categoryDataType[] | []
}

const Home: FC<Props> = ({
inventoryData = [],
categories: categoryData = [],
}) => {
const inventory = inventoryData.slice(0, 4)
const categories = categoryData.slice(0, 2)

Expand Down Expand Up @@ -46,13 +64,13 @@ const Home = ({ inventoryData = [], categories: categoryData = [] }) => {
<div className="grid grid-cols-1 gap-4 my-4 lg:my-8 lg:grid-cols-2">
<DisplayMedium
imageSrc={categories[0].image}
subtitle={`${categories[0].itemCount} items`}
subtitle={`${categories[0].products.length} items`}
title={titleIfy(categories[0].name)}
link={`/category/${slugify(categories[0].name)}`}
/>
<DisplayMedium
imageSrc={categories[1].image}
subtitle={`${categories[1].itemCount} items`}
subtitle={`${categories[1].products.length} items`}
title={titleIfy(categories[1].name)}
link={`/category/${slugify(categories[1].name)}`}
/>
Expand All @@ -68,28 +86,28 @@ const Home = ({ inventoryData = [], categories: categoryData = [] }) => {
<DisplaySmall
imageSrc={inventory[0].image}
title={inventory[0].name}
subtitle={inventory[0].categories[0]}
subtitle={inventory[0].categories[0].name}
link={`/product/${slugify(inventory[0].name)}`}
/>

<DisplaySmall
imageSrc={inventory[1].image}
title={inventory[1].name}
subtitle={inventory[1].categories[0]}
subtitle={inventory[1].categories[0].name}
link={`/product/${slugify(inventory[1].name)}`}
/>

<DisplaySmall
imageSrc={inventory[2].image}
title={inventory[2].name}
subtitle={inventory[2].categories[0]}
subtitle={inventory[2].categories[0].name}
link={`/product/${slugify(inventory[2].name)}`}
/>

<DisplaySmall
imageSrc={inventory[3].image}
title={inventory[3].name}
subtitle={inventory[3].categories[0]}
subtitle={inventory[3].categories[0].name}
link={`/product/${slugify(inventory[3].name)}`}
/>
</div>
Expand All @@ -98,32 +116,22 @@ const Home = ({ inventoryData = [], categories: categoryData = [] }) => {
}

export async function getStaticProps() {
const inventory = await fetchInventory()
const categories = await prismaClient.category.findMany({
include: {
products: true,
},
})

const inventoryCategorized = inventory.reduce((acc, next) => {
const categories = next.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
}, [])
const inventoryData = await prismaClient.product.findMany({
include: {
categories: true,
},
})

return {
props: {
inventoryData: inventory,
categories: inventoryCategorized,
inventoryData,
categories,
},
}
}
Expand Down
29 changes: 21 additions & 8 deletions yarn.lock
Expand Up @@ -172,10 +172,10 @@
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-2.15.0-25.e51dc3b5a9ee790a07104bec1c9477d51740fe54.tgz#d9c887bc8f30d1f107c9021b2565a8672d62622d"
integrity sha512-KDxk7Zsc9tDoShCE7v+O1SnCUTUkMdfezjbuz9CBvdEBGMtYLgyHaZAO8M038uqy8KjgwV9PzyoLqvVfzfsngg==

"@prisma/engines@2.16.0-44.854c8ba7f0dce66f115af36af24e66989a8c02a1":
version "2.16.0-44.854c8ba7f0dce66f115af36af24e66989a8c02a1"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-2.16.0-44.854c8ba7f0dce66f115af36af24e66989a8c02a1.tgz#c7a53f31eafca0e4c010663740ee22ea678f757e"
integrity sha512-vt8w+vqCEaTUGZes7mrY5BkF3zZIPTQ9x+C9Ynmr9d+WW54vTqJt7lDFS6eps0LYoqdcCHEs3r/SydCqjII+OQ==
"@prisma/engines@2.17.0-35.3c463ebd78b1d21d8fdacdd27899e280cf686223":
version "2.17.0-35.3c463ebd78b1d21d8fdacdd27899e280cf686223"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-2.17.0-35.3c463ebd78b1d21d8fdacdd27899e280cf686223.tgz#08bc3633fd27fb1935805ef16c37802ed713db5b"
integrity sha512-FKjVD6NYbGiQhwas3hA2uMpNchz+Mf3tv5qA8Ci9cAkKHGqt3jWjjUAK9juVBqeOcv4OPimQYMrkRX6SvaxBjg==

"@stripe/react-stripe-js@^1.1.2":
version "1.1.2"
Expand All @@ -199,6 +199,19 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.22.tgz#0d29f382472c4ccf3bd96ff0ce47daf5b7b84b18"
integrity sha512-g+f/qj/cNcqKkc3tFqlXOYjrmZA+jNBiDzbP3kH+B+otKFqAdPgVTGP1IeKRdMml/aE69as5S4FqtxAbl+LaMw==

"@types/prop-types@*":
version "15.7.3"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==

"@types/react@^17.0.2":
version "17.0.2"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.2.tgz#3de24c4efef902dd9795a49c75f760cbe4f7a5a8"
integrity sha512-Xt40xQsrkdvjn1EyWe1Bc0dJLcil/9x2vAuW7ya+PuQip4UYUaXyhzWmAbwRsdMgwOFHpfp7/FFZebDU6Y8VHA==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@webassemblyjs/ast@1.9.0":
version "1.9.0"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964"
Expand Down Expand Up @@ -3157,11 +3170,11 @@ pretty-hrtime@^1.0.3:
integrity sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=

prisma@^2.16.0:
version "2.16.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-2.16.0.tgz#a4d3d82932f8aa4b4c5415d254bd9966ed9b5c33"
integrity sha512-tV+28U0dfmuC0lRWZlqI3PLx+5U2UFw4h5fikBexglXsMOE60S732UDnQPOKS/wztJE7EDS36lk6FInFVDpkPQ==
version "2.17.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-2.17.0.tgz#686469914ed13d4b0926ee5f17efb7a9ab741e8a"
integrity sha512-NypJI7OCXCfDkRKubbVb3nmPeRJ1SjQfg6QAwK06KsreBZl1F96rFz2iB2bl4kIrhLAbIySBjwUJlG87Jsxt7g==
dependencies:
"@prisma/engines" "2.16.0-44.854c8ba7f0dce66f115af36af24e66989a8c02a1"
"@prisma/engines" "2.17.0-35.3c463ebd78b1d21d8fdacdd27899e280cf686223"

process-nextick-args@~2.0.0:
version "2.0.1"
Expand Down

0 comments on commit f180fde

Please sign in to comment.