Skip to content

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

Fix url params without initial state

Krzysztof Białogłowiczd5875e7a28e0
diff --git a/saleor/static/js/components/CategoryFilter.js b/saleor/static/js/components/CategoryFilter.js
index 04d8f4fad1..c71e66f0cd 100644
--- a/saleor/static/js/components/CategoryFilter.js
+++ b/saleor/static/js/components/CategoryFilter.js
@@ -9,7 +9,6 @@ export default class CategoryFilter extends Component {
render() {
const { category } = this.props;
- console.log(this.props.category);
return (
<div className="categories">
<h3>Categories:</h3>
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index 823301876f..bc22e7bffa 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -1,16 +1,26 @@
import React, { Component, PropTypes } from 'react';
import Relay from 'react-relay';
+import queryString from 'query-string';
import CategoryFilter from './CategoryFilter';
import PriceFilter from './PriceFilter';
import ProductFilters from './ProductFilters';
import ProductList from './ProductList';
+
const PAGINATE_BY = 20;
class CategoryPage extends Component {
+ constructor(props) {
+ super(props);
+ this.state = {
+ attributesFilters: [],
+ priceFilters: []
+ };
+ }
+
static propTypes = {
attributes: PropTypes.array,
category: PropTypes.object,
@@ -27,15 +37,74 @@ class CategoryPage extends Component {
this.props.relay.setVariables({
attributesFilter: attributes
});
+ this.setState({
+ attributesFilters: attributes
+ }, () => {
+ this.setUrlParams();
+ });
+
}
setPriceFilter = (minPrice, maxPrice) => {
+ let enabled = [];
+
this.props.relay.setVariables({
minPrice: minPrice,
maxPrice: maxPrice
});
+
+ if (minPrice && maxPrice) {
+ enabled = [`minPrice=${minPrice}`, `maxPrice=${maxPrice}`]
+ } else {
+ if (minPrice) {
+ enabled = [`minPrice=${minPrice}`]
+ } else if (maxPrice) {
+ enabled = [`maxPrice=${maxPrice}`]
+ }
+ }
+
+ this.setState({
+ priceFilters: enabled
+ }, () => {
+ this.setUrlParams();
+ });
}
+ setUrlParams = () => {
+ let url = '';
+ let attributesFilter = this.state.attributesFilters;
+ let priceFilters = this.state.priceFilters;
+
+ if (attributesFilter) {
+ attributesFilter.map((param, index) => {
+ param = param.replace(':', '=');
+ if (index == 0) {
+ url += '?' + param;
+ } else {
+ url += '&' + param;
+ }
+ })
+ }
+
+ if (priceFilters) {
+ priceFilters.map((param, index) => {
+ if (index == 0) {
+ url += '?' + param;
+ } else {
+ url += '&' + param;
+ }
+ })
+ }
+
+ if (attributesFilter.length == 0 && priceFilters.length == 0) {
+ url = location.href.split('?')[0];
+ }
+
+ history.pushState({}, null , url);
+
+ }
+
+
render() {
const category = this.props.category;
const attributes = this.props.attributes;
@@ -50,9 +119,11 @@ class CategoryPage extends Component {
<ProductFilters
attributes={attributes}
onFilterChanged={this.setAttributesFilter}
+ urlParams = {this.setUrlParams}
/>
<PriceFilter
onFilterChanged={this.setPriceFilter}
+ urlParams = {this.setUrlParams}
/>
</div>
</div>
diff --git a/saleor/static/js/components/PriceFilter.js b/saleor/static/js/components/PriceFilter.js
index 325e1c96e8..52e2e9b4a7 100644
--- a/saleor/static/js/components/PriceFilter.js
+++ b/saleor/static/js/components/PriceFilter.js
@@ -1,17 +1,19 @@
import React, { Component, PropTypes } from 'react';
-
+import queryString from 'query-string';
export default class PriceFilter extends Component {
static propTypes = {
- onFilterChanged: PropTypes.func.isRequired
+ onFilterChanged: PropTypes.func.isRequired,
+ urlParams: PropTypes.func.isRequired
}
constructor(props) {
super(props);
this.state = {
minPrice: null,
- maxPrice: null
+ maxPrice: null,
+ filters: []
}
}
@@ -41,6 +43,7 @@ export default class PriceFilter extends Component {
let { minPrice, maxPrice } = this.state;
minPrice = this.parseValue(minPrice);
maxPrice = this.parseValue(maxPrice);
+
this.props.onFilterChanged(minPrice, maxPrice);
}
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index b1aebeca94..039d3a2dde 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -8,6 +8,7 @@ class ProductFilters extends Component {
static propTypes = {
attributes: PropTypes.array,
onFilterChanged: PropTypes.func.isRequired,
+ urlParams: PropTypes.func.isRequired
};
constructor(props) {
@@ -19,7 +20,6 @@ class ProductFilters extends Component {
onClick = (attribute, value, event) => {
const attrValue = `${attribute}:${value}`;
- let url = '';
const element = event.target;
this.setState({
@@ -29,25 +29,11 @@ class ProductFilters extends Component {
});
const enabled = Object.keys(this.state.filters).filter(key => this.state.filters[key] === true);
this.props.onFilterChanged(enabled);
-
- enabled.map((param, index) => {
- param = param.replace(':', '=');
- if (index == 0) {
- url += '?' + param;
- } else {
- url += '&' + param;
- }
- });
-
- if (enabled.length == 0) {
- url = location.href.split('?')[0];
- }
-
- history.pushState({}, null , url);
+
}
componentDidMount() {
- let url_params = queryString.parse(location.search);
+ /*let url_params = queryString.parse(location.search);
Object.keys(url_params).map((params) => {
let attrValue = '';
if (Array.isArray(url_params[params])) {
@@ -74,7 +60,7 @@ class ProductFilters extends Component {
})
const enabled = Object.keys(this.state.filters).filter(key => this.state.filters[key] === true);
- this.props.onFilterChanged(enabled);
+ this.props.onFilterChanged(enabled);*/
}
render() {