Skip to content

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

Separate graphQL query to components

Krzysztof Białogłowicz7f3eba1a1745
diff --git a/saleor/static/js/category.js b/saleor/static/js/category.js
index 6de2c39631..98ebc5d656 100644
--- a/saleor/static/js/category.js
+++ b/saleor/static/js/category.js
@@ -1,8 +1,11 @@
import React from 'react'
import ReactDOM from 'react-dom'
-import Relay from 'react-relay';
+import Relay from 'react-relay'
+
+//import AppRoute from './components/AppRoute'
import CategoryPage from './components/CategoryPage'
+import ProductFilters from './components/ProductFilters'
const categoryPage = document.getElementById('category-page');
const categoryData = JSON.parse(categoryPage.getAttribute('data-category'));
@@ -18,7 +21,8 @@ class App extends React.Component {
render() {
return (
<CategoryPage
- data={ this.props.viewer }
+ category = {this.props.viewer.category}
+ attributes = {this.props.viewer.attributes}
/>
);
}
@@ -31,47 +35,18 @@ const RelayApp = Relay.createContainer(App, {
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
- attributes {
- id,
- pk,
- name,
- values {
- id,
- display
- }
- }
category(pk: $categoryId) {
- id,
- name,
- children(first: 20) {
- edges {
- node {
- id,
- name,
- slug
- }
- }
- },
- products(first: 20) {
- edges {
- node {
- id,
- name,
- imageUrl,
- price {
- gross,
- currency
- },
- url
- }
- }
- }
+ ${CategoryPage.getFragment('category')}
+ }
+ attributes {
+ ${ProductFilters.getFragment('attributes')}
}
}
`,
},
});
+
const Viewer = {
queries: {
viewer: () => Relay.QL`
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index a7fbecfc9c..302acb8f31 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -1,23 +1,25 @@
import React, { Component, PropTypes } from 'react'
-
+import Relay from 'react-relay'
import ProductList from './ProductList'
import ProductFilters from './ProductFilters'
-export default class CategoryPage extends Component {
+class CategoryPage extends Component {
static propTypes = {
- data: PropTypes.object,
+ category: PropTypes.object,
+ attributes: PropTypes.array
}
render() {
- const category = this.props.data.category;
- const products = this.props.data.category.products.edges;
- const attributes = this.props.data.attributes;
+ const category = this.props.category;
+ const attributes = this.props.attributes;
+ const products = this.props.category.products;
+
return (
- <div className="row">
+ <div className="row">
<div className="col-md-3">
<ProductFilters categories={category} attributes={attributes} />
</div>
@@ -25,9 +27,32 @@ export default class CategoryPage extends Component {
<div className="row">
<ProductList products={products} />
</div>
- </div>
+ </div>
</div>
)
}
-}
\ No newline at end of file
+}
+
+export default Relay.createContainer(CategoryPage, {
+ fragments: {
+ category: () => Relay.QL`
+ fragment on CategoryType {
+ id,
+ name,
+ children(first: 20) {
+ edges {
+ node {
+ id,
+ name,
+ slug
+ }
+ }
+ }
+ products (first: 20) {
+ ${ProductList.getFragment('products')}
+ }
+ }
+ `,
+ },
+});
\ No newline at end of file
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index 524c3d9699..62e30a3b61 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -1,7 +1,8 @@
-import React, { Component, findDOMNode, PropTypes } from 'react'
+import React, { Component, PropTypes } from 'react'
+import Relay from 'react-relay';
-export default class ProductFilters extends Component {
+class ProductFilters extends Component {
static propTypes = {
categories: PropTypes.object,
@@ -15,7 +16,7 @@ export default class ProductFilters extends Component {
const { attributes } = this.props;
return (
- <div>
+ <div className="product-filters">
<h2>Filters:</h2>
<ul className="categories list-group">
<li className="list-group-item active">{categoryName}</li>
@@ -29,22 +30,48 @@ export default class ProductFilters extends Component {
</ul>
{attributes ? (attributes.map((attribute) => {
return (
- <ul className="attributes list-group" key={attribute.id}>
- <li className="list-group-item active">
- {attribute.name}
- </li>
- {attribute.values.map((value) => {
- return (
- <li key={value.id} className="list-group-item">
- {value.display}
- </li>
- )
- })}
- </ul>
+ <div key={attribute.id} className="attribute">
+ <ul className={attribute.name}>
+ <h3>{attribute.display}</h3>
+ {attribute.values.map((value) => {
+ const colorStyle = {
+ backgroundColor: value.color
+ }
+ return (
+ <li key={value.id} className="item" style={colorStyle}>
+ {value.display}
+ </li>
+ )
+ })}
+ </ul>
+ </div>
)
})
) : (null)}
+
</div>
)
}
-}
\ No newline at end of file
+}
+
+export default Relay.createContainer(ProductFilters, {
+ fragments: {
+ attributes: () => Relay.QL`
+ fragment on ProductAttributeType @relay(plural: true) {
+ id
+ name
+ display
+ values {
+ id
+ display
+ color
+ }
+ }
+ `,
+ },
+});
+
+
+
+
+
diff --git a/saleor/static/js/components/ProductList.js b/saleor/static/js/components/ProductList.js
index f3937646bb..911a5b97bd 100644
--- a/saleor/static/js/components/ProductList.js
+++ b/saleor/static/js/components/ProductList.js
@@ -1,13 +1,13 @@
import React, { Component, PropTypes } from 'react'
-
-export default class ProductList extends Component {
+import Relay from 'react-relay'
+class ProductList extends Component {
static propTypes = {
- products: PropTypes.array
+ products: PropTypes.object
};
render() {
- const { products } = this.props;
+ const products = this.props.products.edges;
return (
<div>
{products ? (products.map((item) => {
@@ -35,3 +35,25 @@ export default class ProductList extends Component {
)
}
}
+
+export default Relay.createContainer(ProductList, {
+ fragments: {
+ products: () => Relay.QL`
+ fragment on ProductTypeConnection {
+ edges {
+ node {
+ id
+ name
+ price {
+ gross
+ net
+ currency
+ }
+ imageUrl
+ url
+ }
+ }
+ }
+ `,
+ },
+});