Skip to content

Commit

Permalink
chore: Add demo (#80)
Browse files Browse the repository at this point in the history
* chore: Add gatsby-image dependencies

* feat: Query content, render data

* chore: Add, initialise tailwindcss

* feat: Grid for the index page

* feat: Build product pages

* feat: Use createResolvers to add formattedPrice field

* feat: Product grid design

* feat: Product page design
  • Loading branch information
Jonathan Steele authored and ynnoj committed Jul 23, 2020
1 parent 1d28db9 commit 810e833
Show file tree
Hide file tree
Showing 12 changed files with 2,704 additions and 90 deletions.
11 changes: 11 additions & 0 deletions demo/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

import Layout from './src/components/layout'

import './src/styles/main.css'

const wrapPageElement = ({ element, props }) => {
return <Layout {...props}>{element}</Layout>
}

export { wrapPageElement }
3 changes: 3 additions & 0 deletions demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require('dotenv').config()

module.exports = {
plugins: [
'gatsby-plugin-postcss',
'gatsby-plugin-sharp',
{
resolve: 'gatsby-source-graphcms',
options: {
Expand All @@ -10,5 +12,6 @@ module.exports = {
token: process.env.GRAPHCMS_TOKEN,
},
},
'gatsby-transformer-sharp',
],
}
48 changes: 48 additions & 0 deletions demo/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const path = require('path')

exports.createPages = async ({ actions: { createPage }, graphql }) => {
const { data } = await graphql(`
{
products: allGraphCmsProduct {
nodes {
description {
text
}
formattedPrice
id
name
slug
}
}
}
`)

data.products.nodes.forEach((product) => {
createPage({
component: path.resolve(`src/templates/product-page.js`),
context: {
id: product.id,
product,
},
path: `/products/${product.slug}`,
})
})
}

exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
GraphCMS_Product: {
formattedPrice: {
type: 'String',
resolve: (source) => {
return new Intl.NumberFormat('en-US', {
currency: 'USD',
style: 'currency',
}).format(source.price)
},
},
},
}

createResolvers(resolvers)
}
1 change: 1 addition & 0 deletions demo/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { wrapPageElement } from './gatsby-browser'
8 changes: 8 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
},
"dependencies": {
"gatsby": "2.23.22",
"gatsby-image": "2.4.13",
"gatsby-plugin-postcss": "2.3.11",
"gatsby-plugin-sharp": "2.6.19",
"gatsby-source-graphcms": "2.0.0",
"gatsby-transformer-sharp": "2.5.11",
"react": "16.13.1",
"react-dom": "16.13.1"
},
"devDependencies": {
"postcss-preset-env": "6.7.0",
"tailwindcss": "1.4.6"
}
}
3 changes: 3 additions & 0 deletions demo/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: [require('postcss-preset-env'), require('tailwindcss')],
}
7 changes: 7 additions & 0 deletions demo/src/components/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

function Layout({ children }) {
return <div className="max-w-4xl mx-auto px-4 py-8">{children}</div>
}

export default Layout
58 changes: 58 additions & 0 deletions demo/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react'
import { graphql, Link } from 'gatsby'
import Img from 'gatsby-image'

const IndexPage = ({ data: { products } }) => {
return (
<ul className="gap-6 grid grid-cols-1 max-w-6xl md:grid-cols-3 mx-auto">
{products.nodes.map((product) => {
const [mainImage] = product.images

return (
<li key={product.id} className="bg-white rounded-lg shadow">
<Link to={`/products/${product.slug}`}>
<div className="flex-1 flex flex-col p-8">
{mainImage && (
<Img
fluid={mainImage.localFile.childImageSharp.fluid}
fadeIn={false}
/>
)}
<h2 className="my-4 text-gray-900 text-xl leading-5 font-medium">
{product.name}
</h2>
<p className="font-semibold text-purple-600">
{product.formattedPrice}
</p>
</div>
</Link>
</li>
)
})}
</ul>
)
}

export const query = graphql`
query PageQuery {
products: allGraphCmsProduct {
nodes {
formattedPrice
id
images {
localFile {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
name
slug
}
}
}
`

export default IndexPage
7 changes: 7 additions & 0 deletions demo/src/styles/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
@apply bg-gray-100;
}
56 changes: 56 additions & 0 deletions demo/src/templates/product-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { graphql } from 'gatsby'
import Img from 'gatsby-image'

const ProductPage = ({ data: { productImages }, pageContext: { product } }) => {
const [mainImage] = productImages.nodes

return (
<div className="bg-white flex flex-1 flex-col md:flex-row p-8 rounded-lg shadow">
<div className="flex flex-1 flex-col">
<h2 className="my-4 text-gray-900 text-3xl leading-5 font-medium">
{product.name}
</h2>
<p className="font-semibold text-purple-600 text-lg">
{product.formattedPrice}
</p>
{product.description && (
<React.Fragment>
<hr className="my-4" />
<p className="leading-relaxed md:text-xl text-lg">
{product.description.text}
</p>
</React.Fragment>
)}
</div>
{mainImage && (
<div className="w-full md:w-3/5">
<Img
fluid={mainImage.localFile.childImageSharp.fluid}
fadeIn={false}
/>
</div>
)}
</div>
)
}

export const query = graphql`
query ProductImageQuery($id: String!) {
productImages: allGraphCmsAsset(
filter: { productImages: { elemMatch: { id: { eq: $id } } } }
) {
nodes {
localFile {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`

export default ProductPage
8 changes: 8 additions & 0 deletions demo/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
purge: ['./src/components/**/*.js', './src/pages/**/*.js'],
theme: {
extend: {},
},
variants: {},
plugins: [],
}
Loading

0 comments on commit 810e833

Please sign in to comment.