Skip to content

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

Refactor product filters component

Marcin Gębala92b4869041c1
diff --git a/.eslintrc.json b/.eslintrc.json
index dc803b3515..a352719231 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -10,6 +10,7 @@
"indent": ["error", 2],
"no-trailing-spaces": ["warn"],
"no-undef": ["error"],
+ "no-unused-vars": ["warn"],
"react/jsx-no-bind": ["error"],
"semi": ["error", "always"],
"strict": ["error", "never"],
diff --git a/saleor/static/js/components/AttributeInput.js b/saleor/static/js/components/AttributeInput.js
new file mode 100644
index 0000000000..c7907f2a8e
--- /dev/null
+++ b/saleor/static/js/components/AttributeInput.js
@@ -0,0 +1,32 @@
+import React, { PropTypes } from 'react';
+
+
+const AttributeInput = ({attribute, checked, onClick, value}) => {
+
+ const handleChange = (event) => {
+ const { name, value } = event.target;
+ onClick(name, value);
+ };
+
+ return (
+ <label>
+ <input
+ checked={checked}
+ name={attribute.name}
+ onChange={handleChange}
+ type="checkbox"
+ value={value.slug}
+ />
+ {value.display}
+ </label>
+ );
+};
+
+AttributeInput.propTypes = {
+ checked: PropTypes.bool,
+ attribute: PropTypes.object.isRequired,
+ value: PropTypes.object.isRequired,
+ onClick: PropTypes.func.isRequired
+};
+
+export default AttributeInput;
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index 49735a3d41..122d7620da 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -1,6 +1,9 @@
+import queryString from 'query-string';
import React, { Component, PropTypes } from 'react';
import Relay from 'react-relay';
+import AttributeInput from './AttributeInput';
+
class ProductFilters extends Component {
@@ -12,21 +15,44 @@ class ProductFilters extends Component {
constructor(props) {
super(props);
- this.state = {
- filters: {}
- };
+ this.state = {};
+ }
+
+ 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 = (event) => {
- const { name, value } = event.target;
- const attrValue = `${name}:${value}`;
- this.setState({
- filters: Object.assign(
- this.state.filters,
- {[attrValue]: !this.state.filters[attrValue]})
+ onClick = (name, value) => {
+ this.toggleFilter(name, value);
+ this.filterChangedCallback();
+ };
+
+ toggleFilter(attributeName, valueSlug) {
+ const key = this.getFilterKey(attributeName, valueSlug);
+ 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);
+ }
});
- const enabled = Object.keys(this.state.filters).filter(key => this.state.filters[key] === true);
- this.props.onFilterChanged(enabled);
+ this.filterChangedCallback();
}
render() {
@@ -39,12 +65,16 @@ class ProductFilters extends Component {
<ul>
<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">
- <label>
- <input name={attribute.name} type="checkbox" value={value.slug} onClick={this.onClick} />
- {value.display}
- </label>
+ <AttributeInput
+ attribute={attribute}
+ checked={checked}
+ onClick={this.onClick}
+ value={value}
+ />
</li>
);
})}