Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add product gallery + basic CSS styling #246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions using-pagination/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import Link from 'next/link';
import { gql } from 'graphql-request';

import { graphcms } from '../../lib/_graphcms';

const limit = 1;

const singleProductStyle = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to improve styling for all our examples. I'm happy if you want to add these here, but FYI, these may get removed with an updated design when we get some design resources to help here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's definitely a thing! Do you already have basic styling from other example that I could use here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these may get removed with an updated design when we get some design resources to help here

display: 'flex',
flexDirection: 'column',
width: '200px',
margin: '10px',
};

function SingleProduct({product, index}) {
const { image } = product;
return (
<div style={singleProductStyle}>
<img src={image.url} width={200} height={200} alt={product.name} title={product.name} />
<Link href={`/products/${index+1}`}>
<a>{product.name}</a>
</Link>
</div>
)
}

const productListStyle = {
display: 'flex'
};

function IndexPage({ products }) {
return products.map(({ node: product }) => (
<pre>{JSON.stringify(product, 2, null)}</pre>
));
return (
<div style={productListStyle}>
{products.map(({ node: product }, index) => {
return <SingleProduct key={product.id} product={product} index={index} />
})}
</div>
);
}

export async function getStaticProps() {
Expand All @@ -18,6 +46,17 @@ export async function getStaticProps() {
node {
id
name
image {
id
url(transformation: {
image: {
resize: {
width: 200,
height: 200
}
}
})
}
}
}
pageInfo {
Expand Down
48 changes: 45 additions & 3 deletions using-pagination/src/pages/products/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ import { graphcms } from '../../../lib/_graphcms';

const limit = 1;

const singleProductStyle = {
display: 'flex',
flexDirection: 'column',
width: '200px',
margin: '10px',
color: '#000',
};

function SingleProduct({product}) {
const { image } = product;
return (
<div style={singleProductStyle} key={product.id}>
<img src={image.url} width={200} height={200} alt={product.name} title={product.name} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not recommended you use <img /> if you know the width/height but instead use Next.js Image.

However, I would not show an image at all here. We have an example for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@notrab we're using Next.js 9 on this project and next/image was introduced in v10. Unless we upgrade it, it's not possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not really relevant though. We should aim to keep these updated regardless.

<h3>{product.name}</h3>
</div>
)
}

const navStyle = {
display: 'inline-block',
backgroundColor: '#ccc',
color: '#111',
textDecoration: 'none',
padding: '2px 6px',
marginRight: '5px',
}

function ProductPage({
currentPageNumber,
hasNextPage,
Expand All @@ -13,15 +40,19 @@ function ProductPage({
}) {
return (
<React.Fragment>
<pre>{JSON.stringify(products, 2, null)}</pre>
<Link href={'/'}>
<a>Home</a>
</Link>
<p>Page {currentPageNumber}</p>
<SingleProduct product={products[0].node} />
{hasPreviousPage ? (
<Link href={`/products/${currentPageNumber - 1}`}>
<a>Previous page</a>
<a style={navStyle}>Previous page</a>
</Link>
) : null}
{hasNextPage ? (
<Link href={`/products/${currentPageNumber + 1}`}>
<a>Next page</a>
<a style={navStyle}>Next page</a>
</Link>
) : null}
</React.Fragment>
Expand Down Expand Up @@ -75,6 +106,17 @@ export async function getStaticProps({ params }) {
node {
id
name
image {
id
url(transformation: {
image: {
resize: {
width: 200,
height: 200
}
}
})
}
}
}
pageInfo {
Expand Down