Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
WIP Refactor CategoryPage to keep state in relay variables; use query-string for persisting state in URL;
Marcin Gębala6f2a11348e11
diff --git a/.eslintrc.json b/.eslintrc.jsonindex a352719231..6c42b9358f 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -11,7 +11,7 @@"no-trailing-spaces": ["warn"],"no-undef": ["error"],"no-unused-vars": ["warn"],- "react/jsx-no-bind": ["error"],+ "react/jsx-no-bind": ["error", {"ignoreRefs": true}],"semi": ["error", "always"],"strict": ["error", "never"],"quotes": ["warn", "single"]diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.jsindex e977bbb835..c91bb57662 100644--- a/saleor/static/js/components/CategoryPage.js+++ b/saleor/static/js/components/CategoryPage.js@@ -1,3 +1,4 @@+import queryString from 'query-string';import React, { Component, PropTypes } from 'react';import Relay from 'react-relay';@@ -10,15 +11,19 @@ import ProductList from './ProductList';const PAGINATE_BY = 20;-class CategoryPage extends Component {+const getVarFromQueryString = (key, defaultValue = null) => {+ let value = queryString.parse(location.search)[key];+ return value ? value : defaultValue;+};+++const floatOrNull = (value) => {+ const parsed = parseFloat(value);+ return isNaN(parsed) ? null : parsed;+};- constructor(props) {- super(props);- this.state = {- attributesFilters: [],- priceFilters: []- };- }++class CategoryPage extends Component {static propTypes = {attributes: PropTypes.array,@@ -26,86 +31,58 @@ class CategoryPage extends Component {relay: PropTypes.object}- loadMore = () => {+ incrementProductsCount = () => {this.props.relay.setVariables({count: this.props.relay.variables.count + PAGINATE_BY});}- setAttributesFilter = (attributes) => {- this.props.relay.setVariables({- attributesFilter: attributes- });- this.setState({- attributesFilters: attributes- }, () => {- this.setUrlParams();- });+ updateAttributesFilter = (attributes) => {+ this.props.relay.setVariables({attributesFilter: attributes});}- setPriceFilter = (minPrice, maxPrice) => {- let enabled = [];-+ updatePriceFilter = (minPrice, maxPrice) => {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();+ minPrice: floatOrNull(minPrice),+ maxPrice: floatOrNull(maxPrice)});}- 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;- }- });+ persistStateInUrl() {+ const { attributesFilter, count, maxPrice, minPrice } = this.props.relay.variables;+ let urlParams = {};+ if (minPrice) {+ urlParams['minPrice'] = minPrice;}-- if (priceFilters) {- priceFilters.map((param, index) => {- if (index == 0 && attributesFilter == 0) {- url += '?' + param;- } else {- url += '&' + param;- }- });+ if (maxPrice) {+ urlParams['maxPrice'] = maxPrice;}-- if (attributesFilter.length == 0 && priceFilters.length == 0) {- url = location.href.split('?')[0];+ if (count > PAGINATE_BY) {+ urlParams['count'] = count;}-+ attributesFilter.forEach(filter => {+ const [ attributeName, valueSlug ] = filter.split(':');+ if (attributeName in urlParams) {+ urlParams[attributeName].push(valueSlug);+ } else {+ urlParams[attributeName] = [valueSlug];+ }+ });+ const url = Object.keys(urlParams).length ?+ '?' + queryString.stringify(urlParams) :+ location.href.split('?')[0];history.pushState({}, null , url);-}+ componentDidUpdate() {+ // Persist current state of relay variables as query string. Current+ // variables are available in props after component rerenders, so it has to+ // be called inside componentDidUpdate method.+ this.persistStateInUrl();+ }render() {- const category = this.props.category;- const attributes = this.props.attributes;+ const { attributes, category, relay: { variables } } = this.props;return (<div className="row"><div className="col-md-3">@@ -116,19 +93,19 @@ class CategoryPage extends Component {/><ProductFiltersattributes={attributes}- onFilterChanged={this.setAttributesFilter}- urlParams = {this.setUrlParams}+ onFilterChanged={this.updateAttributesFilter}/><PriceFilter- onFilterChanged={this.setPriceFilter}- urlParams = {this.setUrlParams}+ onFilterChanged={this.updatePriceFilter}+ maxPrice={variables.maxPrice}+ minPrice={variables.minPrice}/></div></div><div className="col-md-9"><div className="row"><ProductList- onLoadMore={this.loadMore}+ onLoadMore={this.incrementProductsCount}products={category.products}/></div>@@ -138,12 +115,13 @@ class CategoryPage extends Component {}}+export default Relay.createContainer(CategoryPage, {initialVariables: {attributesFilter: [],- count: PAGINATE_BY,- minPrice: null,- maxPrice: null+ count: floatOrNull(getVarFromQueryString('count', PAGINATE_BY)),+ minPrice: floatOrNull(getVarFromQueryString('minPrice')),+ maxPrice: floatOrNull(getVarFromQueryString('maxPrice'))},fragments: {category: () => Relay.QL`diff --git a/saleor/static/js/components/PriceFilter.js b/saleor/static/js/components/PriceFilter.jsindex 9b8889f52f..f7c9bd8f92 100644--- a/saleor/static/js/components/PriceFilter.js+++ b/saleor/static/js/components/PriceFilter.js@@ -3,17 +3,9 @@ import React, { Component, PropTypes } from 'react';export default class PriceFilter extends Component {static propTypes = {- onFilterChanged: PropTypes.func.isRequired,- urlParams: PropTypes.func.isRequired- }-- constructor(props) {- super(props);- this.state = {- minPrice: null,- maxPrice: null,- filters: []- };+ minPrice: PropTypes.number,+ maxPrice: PropTypes.number,+ onFilterChanged: PropTypes.func.isRequired}checkKey = (event) => {@@ -22,53 +14,35 @@ export default class PriceFilter extends Component {}};- onChange = (event) => {- const { name, value } = event.target;- this.setState({[name]: value});- }-- parseValue = (value) => {- if (value) {- try {- return parseFloat(value);- } catch (error) {- return null;- }- }- return null;- }-updateFilter = () => {- let { minPrice, maxPrice } = this.state;- minPrice = this.parseValue(minPrice);- maxPrice = this.parseValue(maxPrice);+ const minPrice = this.minPriceInput.value;+ const maxPrice = this.maxPriceInput.value;this.props.onFilterChanged(minPrice, maxPrice);}render() {+ const { maxPrice, minPrice } = this.props;return (<div className="price-range"><h3>Price range</h3><input- id="minPrice"className="form-control"- name="minPrice"- onChange={this.onChange}+ defaultValue={minPrice}+ min="0"onKeyUp={this.checkKey}placeholder="min"+ ref={input => this.minPriceInput = input}type="number"- value={this.state.minPrice}/><span>—</span><input- id="maxPrice"- name="maxPrice"className="form-control"- onChange={this.onChange}+ defaultValue={maxPrice}+ min="0"onKeyUp={this.checkKey}placeholder="max"+ ref={input => this.maxPriceInput = input}type="number"- value={this.state.maxPrice}/><button className="btn" onClick={this.updateFilter}>Update</button></div>diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.jsindex 64e53e8786..e7f52b45bb 100644--- a/saleor/static/js/components/ProductFilters.js+++ b/saleor/static/js/components/ProductFilters.js@@ -9,8 +9,7 @@ class ProductFilters extends Component {static propTypes = {attributes: PropTypes.array,- onFilterChanged: PropTypes.func.isRequired,- urlParams: PropTypes.func.isRequired+ onFilterChanged: PropTypes.func.isRequired};constructor(props) {