Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
feat: download image assets (#27)
Browse files Browse the repository at this point in the history
* cherrypick

* add images to schema

* chore: use new schema customization for linked file nodes

* chore: set default publicKey

* fix: add to assetIds if imageNode is defined

* chore: use const vs let

* chore(deps): update gatsby monorepo

* refactor: tidy up gatsby config

* docs: add downloadImageAssets config example

* docs: add image assets instructions

* fix: set empty images array

Co-authored-by: Renovate Bot <[email protected]>
  • Loading branch information
notrab and renovate-bot authored Sep 16, 2020
1 parent b585cc0 commit 48683fd
Show file tree
Hide file tree
Showing 12 changed files with 2,103 additions and 98 deletions.
7 changes: 6 additions & 1 deletion demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ require('dotenv').config();

module.exports = {
plugins: [
'gatsby-plugin-sharp',
{
resolve: '@chec/gatsby-source-chec',
options: {
publicKey: process.env.CHEC_PUBLIC_KEY,
publicKey:
process.env.CHEC_PUBLIC_KEY ||
'pk_184625ed86f36703d7d233bcf6d519a4f9398f20048ec',
downloadImageAssets: true,
},
},
'gatsby-transformer-sharp',
],
};
3 changes: 3 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"dependencies": {
"gatsby": "2.24.60",
"@chec/gatsby-source-chec": "*",
"gatsby-image": "2.4.16",
"gatsby-plugin-sharp": "2.6.30",
"gatsby-transformer-sharp": "2.5.13",
"react": "16.13.1",
"react-dom": "16.13.1"
}
Expand Down
54 changes: 26 additions & 28 deletions demo/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import React from 'react';
import { graphql, useStaticQuery, Link } from 'gatsby';
import { graphql, Link } from 'gatsby';

import ProductList from '../components/ProductList';

const pageQuery = graphql`
export default function IndexPage({
data: { merchant, products, categories },
}) {
return (
<React.Fragment>
<h1>{merchant.name}</h1>

<h3>Categories</h3>

<ul>
{categories.nodes.map(({ name, slug }) => (
<li key={slug}>
<Link to={`/categories/${slug}`}>{name}</Link>
</li>
))}
</ul>

<h3>Products</h3>

<ProductList products={products.nodes} />
</React.Fragment>
);
}

export const pageQuery = graphql`
{
merchant: checMerchant {
name: business_name
Expand All @@ -27,29 +51,3 @@ const pageQuery = graphql`
}
}
`;

const IndexPage = () => {
const { merchant, products, categories } = useStaticQuery(pageQuery);

return (
<React.Fragment>
<h1>{merchant.name}</h1>

<h3>Categories</h3>

<ul>
{categories.nodes.map(({ name, slug }) => (
<li key={slug}>
<Link to={`/categories/${slug}`}>{name}</Link>
</li>
))}
</ul>

<h3>Products</h3>

<ProductList products={products.nodes} />
</React.Fragment>
);
};

export default IndexPage;
21 changes: 19 additions & 2 deletions demo/src/templates/ProductPage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import React from 'react';
import { graphql } from 'gatsby';
import Img from 'gatsby-image';

export default function ProductPage({ data: { product } }) {
const { name, price, images } = product;
const [mainImage] = images;

return (
<React.Fragment>
<h1>{product.name}</h1>
<p>{product.price.formatted_with_symbol}</p>
{mainImage && (
<Img
fluid={mainImage.childImageSharp.fluid}
style={{ maxWidth: '50%' }}
/>
)}
<h1>{name}</h1>
<p>{price.formatted_with_symbol}</p>
</React.Fragment>
);
}
Expand All @@ -18,6 +28,13 @@ export const pageQuery = graphql`
price {
formatted_with_symbol
}
images {
childImageSharp {
fluid(maxWidth: 560) {
...GatsbyImageSharpFluid
}
}
}
}
}
`;
9 changes: 9 additions & 0 deletions gatsby-source-chec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins: [
resolve: `@chec/gatsby-source-chec`,
options: {
publicKey: '...',
downloadImageAssets: true, // false by default
},
},
];
Expand Down Expand Up @@ -52,3 +53,11 @@ plugins: [
}
}
```

## Downloading image assets

This plugin provides you the option to download product asset images, and cache them in your Gatsby project. This works great with [`gatsby-image`](https://www.gatsbyjs.org/packages/gatsby-image).

Add `downloadImageAssets: true` to your plugin options.

These assets will be added as `images` to the `ChecProduct` nodes.
2 changes: 2 additions & 0 deletions gatsby-source-chec/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
exports.onPreBootstrap = require('./gatsby/node/onPreBootstrap');
exports.sourceNodes = require('./gatsby/node/sourceNodes');
exports.createSchemaCustomization = require('./gatsby/node/createSchemaCustomization');
exports.onCreateNode = require('./gatsby/node/onCreateNode');
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type ChecProduct implements Node {
categories: [ChecCategory] @link
images: [File] @link
}
type ChecCategory implements Node {
Expand Down
49 changes: 49 additions & 0 deletions gatsby-source-chec/gatsby/node/onCreateNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { createRemoteFileNode } = require('gatsby-source-filesystem');

const withPluginOptions = require('../../plugin-options');

const onCreateNode = async (
{ node, actions: { createNode }, createNodeId, store, cache },
pluginOptions
) => {
const { downloadImageAssets } = withPluginOptions(pluginOptions);

node.images = [];

if (
downloadImageAssets &&
node.internal.type === 'ChecProduct' &&
node.assets
) {
const getImageAssets = async () => {
const assetIds = [];

const processImageAssets = node.assets.map(async (asset) => {
if (!asset.is_image) return;

try {
const imageNode = await createRemoteFileNode({
url: encodeURI(asset.url),
store,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
});

if (imageNode) assetIds.push(imageNode.id);
} catch (e) {
console.error('gatsby-source-chec: ERROR', e);
}
});

await Promise.all(processImageAssets);

return assetIds;
};

node.images = await getImageAssets();
}
};

module.exports = onCreateNode;
12 changes: 12 additions & 0 deletions gatsby-source-chec/gatsby/node/onPreBootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const withPluginOptions = require('../../plugin-options');

const onPreBootstrap = ({ reporter }, pluginOptions) => {
const { publicKey } = withPluginOptions(pluginOptions);

if (!publicKey)
return reporter.panicOnBuild(
`@chec/gatsby-source-chec: You must provide a 'publicKey' for your Chec store`
);
};

module.exports = onPreBootstrap;
5 changes: 0 additions & 5 deletions gatsby-source-chec/gatsby/node/sourceNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ const sourceNodes = async (
{ actions, createContentDigest, reporter },
{ publicKey }
) => {
if (!publicKey)
return reporter.panicOnBuild(
`@chec/gatsby-source-chec: You must provide a 'publicKey' for your Chec store`
);

const { createNode } = actions;

const commerce = new Chec(publicKey);
Expand Down
6 changes: 6 additions & 0 deletions gatsby-source-chec/plugin-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const pluginOptions = (options) => ({
downloadImageAssets: false,
...options,
});

module.exports = pluginOptions;
Loading

0 comments on commit 48683fd

Please sign in to comment.