-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data): Add live GraphQL data to product detail page
Basic implementation of a GraphQL query for product details. Builds on #52 by replicating the inline query declaration. Had to plumb out the child components for the new data shape; in doing so, I made a few reusable functions. - Added GraphQL query to `packages/venia-concept/src/RootComponents/Product.js`. - Resolves from URL by using the `url_key` in a GraphQL query. - Can also resolve by SKU. - Modified prop types and render method to accommodate live data shape. - Added `<Currency />` component whose signature matches the Magento GraphQl `Money` type. - Uses the `window.Intl` standard object to format. - Modified the `Gallery` and `ProductImageCarousel` components to use new data shape. - Moved shared constant data URIs to a single `src/shared` folder, to replicate placeholder logic. - Created a shared `propShapes.js` file containing commonly used prop type expressions. - Anticipating that `url_key` would be a common way to navigate, I made a `url_key` utility function. - Added a `makeProductMediaPath` utility function, for turning product image file paths from API responses into relative URLs. - Though [magento/graphql-ce/issues/88](magento/graphql-ce#88) is still a problem for production, I found that **when `magento-sample-data` is installed, it symlinks into the `pub/media` folder so you can use simpler URLs.** - You can see this for yourself with `ls -l <magento-root>/pub/media/catalog/product`. - So I added a `makePathPrepender` function, which we'll later use often, that can create functions like `makeProductMediaPath`. - I hardcoded `/media/catalog/products` in the code, but I also added an environment variable to `.env` and `webpack.config.js` for configuring that URL per instance. - Optimize queries with fragments - Centralize queries in query file to be preprocessed - Make link to product detail on category page - Resolve media URL issue - Test with image galleries Closes #87.
- Loading branch information
Showing
13 changed files
with
265 additions
and
97 deletions.
There are no files selected for viewing
186 changes: 113 additions & 73 deletions
186
packages/venia-concept/src/RootComponents/Product/Product.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Component, createElement } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default class Currency extends Component { | ||
|
||
static propTypes = { | ||
locale: PropTypes.string, | ||
value: PropTypes.number.isRequired, | ||
currency: PropTypes.string.isRequired, | ||
tagName: PropTypes.string | ||
}; | ||
|
||
static defaultProps = { | ||
tagName: 'span' | ||
}; | ||
|
||
guessLocalLanguage() { | ||
if (!window.navigator) return; | ||
return window.navigator | ||
&& (navigator.languages && navigator.languages[0]) // HTML5 spec | ||
|| navigator.language // HTML5 spec | ||
|| 'en_US'; // America!!! | ||
} | ||
|
||
|
||
render() { | ||
const { tagName: Tag, value, locale, currency, ...attrs } = this.props; | ||
const formatter = new Intl.NumberFormat(locale || this.guessLocalLanguage(), { | ||
style: 'currency', | ||
currency | ||
}); | ||
return ( | ||
<Tag {...attrs}> | ||
{formatter.format(value)} | ||
</Tag> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
packages/venia-concept/src/constants.js → packages/venia-concept/src/shared/images.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// corresponds to a 300x372 transparent png | ||
// keep in sync with constants above | ||
// TODO: generate this programmatically? | ||
export const imagePlaceholderUri = | ||
export const transparentPlaceholder = | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAfCAQAAAC4ua71AAAAGklEQVR42mNkIBkwjmoZ1TKqZVTLqJYRpgUAaP0AIAQAObYAAAAASUVORK5CYII='; | ||
|
||
export const grayPlaceholder = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAFCAQAAADIpIVQAAAADklEQVR42mNkgAJGIhgAALQABsHyMOcAAAAASUVORK5CYII='; |
Oops, something went wrong.