Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add category id to initialData

Krzysztof Białogłowicz95313a1cf26a
diff --git a/saleor/static/js/category.js b/saleor/static/js/category.js
index 47219a0770..efaf5c8311 100644
--- a/saleor/static/js/category.js
+++ b/saleor/static/js/category.js
@@ -4,6 +4,9 @@ import Relay from 'react-relay';
import CategoryPage from './components/CategoryPage'
+const categoryPage = document.getElementById('category-page');
+const categoryData = JSON.parse(categoryPage.getAttribute('data-category'));
+
Relay.injectNetworkLayer(
new Relay.DefaultNetworkLayer('/graphql/', {
@@ -13,26 +16,30 @@ Relay.injectNetworkLayer(
class App extends React.Component {
render() {
- return <CategoryPage products={ this.props.viewer.products.edges } />
+ return <CategoryPage products={ this.props.viewer.category.products.edges } />
}
}
const RelayApp = Relay.createContainer(App, {
+ initialVariables: {
+ categoryId: categoryData.id
+ },
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
- products(first: 20) {
- edges {
- node {
- id,
- name,
- description,
- imageUrl,
- price {
- gross,
- currency
- },
- url
+ category(pk: $categoryId) {
+ products(first: 20) {
+ edges {
+ node {
+ id,
+ name,
+ imageUrl,
+ price {
+ gross,
+ currency
+ },
+ url
+ }
}
}
}
diff --git a/templates/category/index.html b/templates/category/index.html
index 4da195dc34..7da1767c02 100644
--- a/templates/category/index.html
+++ b/templates/category/index.html
@@ -25,7 +25,7 @@
{% endblock breadcrumb %}
{% block content %}
- <div id="category-page"></div>
+ <div id="category-page" data-category='{"id":{{category.id}},"name":"{{category}}"}'></div>
<div class="row">
<div class="col-md-3">
<ul class="list-group">