Skip to content

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

Move helper functions to separate module

Marcin Gębala4e8d40f52327
diff --git a/saleor/static/js/category.js b/saleor/static/js/category.js
index e3f65269ab..a80e4956f5 100644
--- a/saleor/static/js/category.js
+++ b/saleor/static/js/category.js
@@ -15,12 +15,7 @@ class App extends React.Component {
}
render() {
- return (
- <CategoryPage
- category={this.props.root.category}
- attributes={this.props.root.attributes}
- />
- );
+ return <CategoryPage {...this.props.root} />;
}
}
@@ -36,7 +31,7 @@ const RelayApp = Relay.createContainer(App, {
}
attributes(categoryPk: $categoryId) {
${ProductFilters.getFragment('attributes')}
- },
+ }
__debug {
sql {
sql
diff --git a/saleor/static/js/components/categoryPage/CategoryPage.js b/saleor/static/js/components/categoryPage/CategoryPage.js
index 2651082906..45bf35b8be 100644
--- a/saleor/static/js/components/categoryPage/CategoryPage.js
+++ b/saleor/static/js/components/categoryPage/CategoryPage.js
@@ -7,40 +7,10 @@ import PriceFilter from './PriceFilter';
import ProductFilters from './ProductFilters';
import ProductList from './ProductList';
import SortBy from './SortBy';
+import { floatOrNull, getFromQuery, getAttributesFromQuery } from './utils';
const PAGINATE_BY = 20;
-const getVarFromQueryString = (key, defaultValue = null) => {
- let value = queryString.parse(location.search)[key];
- return value || defaultValue;
-};
-
-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;
-};
-
class CategoryPage extends Component {
static propTypes = {
@@ -176,11 +146,11 @@ class CategoryPage extends Component {
export default Relay.createContainer(CategoryPage, {
initialVariables: {
- attributesFilter: getAttributesFromQueryString(['count', 'minPrice', 'maxPrice', 'sortBy']),
- count: floatOrNull(getVarFromQueryString('count', PAGINATE_BY)),
- minPrice: floatOrNull(getVarFromQueryString('minPrice')),
- maxPrice: floatOrNull(getVarFromQueryString('maxPrice')),
- sortBy: getVarFromQueryString('sortBy', 'name')
+ attributesFilter: getAttributesFromQuery(['count', 'minPrice', 'maxPrice', 'sortBy']),
+ count: floatOrNull(getFromQuery('count', PAGINATE_BY)),
+ minPrice: floatOrNull(getFromQuery('minPrice')),
+ maxPrice: floatOrNull(getFromQuery('maxPrice')),
+ sortBy: getFromQuery('sortBy', 'name')
},
fragments: {
category: () => Relay.QL`
@@ -199,7 +169,13 @@ export default Relay.createContainer(CategoryPage, {
url
slug
}
- products (first: $count, attributes: $attributesFilter, priceGte: $minPrice, priceLte: $maxPrice, orderBy: $sortBy) {
+ products (
+ first: $count,
+ attributes: $attributesFilter,
+ priceGte: $minPrice,
+ priceLte: $maxPrice,
+ orderBy: $sortBy
+ ) {
${ProductList.getFragment('products')}
}
}
diff --git a/saleor/static/js/components/categoryPage/utils.js b/saleor/static/js/components/categoryPage/utils.js
new file mode 100644
index 0000000000..3e9bde96f9
--- /dev/null
+++ b/saleor/static/js/components/categoryPage/utils.js
@@ -0,0 +1,32 @@
+import queryString from 'query-string';
+
+export const getFromQuery = (key, defaultValue = null) => {
+ let value = queryString.parse(location.search)[key];
+ return value || defaultValue;
+};
+
+export const getAttributesFromQuery = (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;
+};
+
+export const floatOrNull = (value) => {
+ const parsed = parseFloat(value);
+ return isNaN(parsed) ? null : parsed;
+};