Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Keep state of attribute filters in relay variables
Marcin Gębalae986d5581f3d
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex ff04b0f59d..af0327e822 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -84,7 +84,6 @@ class CategoryType(DjangoObjectType):price_lte = args.get('price_lte')price_gte = args.get('price_gte')if attributes_filter:-attributes = ProductAttribute.objects.prefetch_related('values')attributes_map = {attribute.name: attribute.pkfor attribute in attributes}diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.jsindex c91bb57662..b657b8111f 100644--- a/saleor/static/js/components/CategoryPage.js+++ b/saleor/static/js/components/CategoryPage.js@@ -17,6 +17,28 @@ const getVarFromQueryString = (key, defaultValue = null) => {};+const getAttributesFromQueryString = (exclude) => {+ // Exclude parameter is used to exclude other query string parameters than+ // product attribute filters.+ const urlParams = queryString.parse(location.search);+ let attributes = [];+ Object.keys(urlParams).forEach(key => {+ if (!exclude.includes(key)) {+ if (Array.isArray(urlParams[key])) {+ const values = urlParams[key];+ values.map((valueSlug) => {+ attributes.push(`${key}:${valueSlug}`);+ });+ } else {+ const valueSlug = urlParams[key];+ attributes.push(`${key}:${valueSlug}`);+ }+ }+ });+ return attributes;+};++const floatOrNull = (value) => {const parsed = parseFloat(value);return isNaN(parsed) ? null : parsed;@@ -37,8 +59,19 @@ class CategoryPage extends Component {});}- updateAttributesFilter = (attributes) => {- this.props.relay.setVariables({attributesFilter: attributes});+ updateAttributesFilter = (key) => {+ // Create a new attributesFilter array by cloning the current one to make+ // Relay refetch products with new attributes. Passing the same array (even+ // if it's modified) would not result in new query, but would return cached+ // results.+ const attributesFilter = this.props.relay.variables.attributesFilter.slice(0);+ const index = attributesFilter.indexOf(key);+ if (index < 0) {+ attributesFilter.push(key);+ } else {+ attributesFilter.splice(index, 1);+ }+ this.props.relay.setVariables({ attributesFilter });}updatePriceFilter = (minPrice, maxPrice) => {@@ -93,6 +126,7 @@ class CategoryPage extends Component {/><ProductFiltersattributes={attributes}+ checkedAttributes={variables.attributesFilter}onFilterChanged={this.updateAttributesFilter}/><PriceFilter@@ -118,7 +152,7 @@ class CategoryPage extends Component {export default Relay.createContainer(CategoryPage, {initialVariables: {- attributesFilter: [],+ attributesFilter: getAttributesFromQueryString(['count', 'minPrice', 'maxPrice']),count: floatOrNull(getVarFromQueryString('count', PAGINATE_BY)),minPrice: floatOrNull(getVarFromQueryString('minPrice')),maxPrice: floatOrNull(getVarFromQueryString('maxPrice'))diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.jsindex e7f52b45bb..7269a0c0f9 100644--- a/saleor/static/js/components/ProductFilters.js+++ b/saleor/static/js/components/ProductFilters.js@@ -1,4 +1,3 @@-import queryString from 'query-string';import React, { Component, PropTypes } from 'react';import Relay from 'react-relay';@@ -9,61 +8,20 @@ class ProductFilters extends Component {static propTypes = {attributes: PropTypes.array,+ checkedAttributes: PropTypes.array,onFilterChanged: PropTypes.func.isRequired};- constructor(props) {- super(props);- let initialState = {};- props.attributes.forEach(attribute => {- attribute.values.forEach(value => {- initialState[this.getFilterKey(attribute.name, value.slug)] = false;- });- });- this.state = initialState;- }-getFilterKey(attributeName, valueSlug) {- // Returns a key that identifies a filter in the state.return `${attributeName}:${valueSlug}`;}- filterChangedCallback() {- const checked = Object.keys(this.state).filter(- key => this.state[key] === true);- this.props.onFilterChanged(checked);- }-- onClick = (name, value) => {- this.toggleFilter(name, value);- this.filterChangedCallback();+ onClick = (attributeName, valueSlug) => {+ this.props.onFilterChanged(this.getFilterKey(attributeName, valueSlug));};- toggleFilter(attributeName, valueSlug) {- const key = this.getFilterKey(attributeName, valueSlug);- if (key in this.state) {- this.setState(Object.assign(this.state, {[key]: !this.state[key]}));- }- }-- componentDidMount() {- let urlParams = queryString.parse(location.search);- Object.keys(urlParams).map((attributeName) => {- if (Array.isArray(urlParams[attributeName])) {- const values = urlParams[attributeName];- values.map((valueSlug) => {- this.toggleFilter(attributeName, valueSlug);- });- } else {- const valueSlug = urlParams[attributeName];- this.toggleFilter(attributeName, valueSlug);- }- });- this.filterChangedCallback();- }-render() {- const { attributes } = this.props;+ const { attributes, checkedAttributes } = this.props;return (<div className="attributes">{attributes && (attributes.map((attribute) => {@@ -73,12 +31,11 @@ class ProductFilters extends Component {<h3>{attribute.display}</h3>{attribute.values.map((value) => {const key = this.getFilterKey(attribute.name, value.slug);- const checked = this.state[key];return (<li key={value.id} className="item"><AttributeInputattribute={attribute}- checked={checked}+ checked={checkedAttributes.includes(key)}onClick={this.onClick}value={value}/>