0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-17 14:32:41 +00:00

Simplify getSortedBrews logic

This commit is contained in:
G.Ambatte
2022-09-03 22:28:03 +12:00
parent e5fe6b1fd9
commit 85cad49b03

View File

@@ -1,3 +1,4 @@
/*eslint max-lines: ["warn", {"max": 300, "skipBlankLines": true, "skipComments": true}]*/
require('./listPage.less'); require('./listPage.less');
const React = require('react'); const React = require('react');
const createClass = require('create-react-class'); const createClass = require('create-react-class');
@@ -30,10 +31,10 @@ const ListPage = createClass({
}); });
return { return {
filterString : this.props.query?.filter || '', filterString : this.props.query?.filter || '',
sortType : this.props.query?.sort || 'alpha', sortType : this.props.query?.sort || 'alpha',
sortDir : this.props.query?.dir || 'asc', sortDir : this.props.query?.dir || 'asc',
query : this.props.query, query : this.props.query,
brewCollection : brewCollection brewCollection : brewCollection
}; };
}, },
@@ -184,19 +185,15 @@ const ListPage = createClass({
getSortedBrews : function(brews){ getSortedBrews : function(brews){
const testString = _.deburr(this.state.filterString).toLowerCase(); const testString = _.deburr(this.state.filterString).toLowerCase();
const checkString = (stringToTest)=>{
return (_.deburr(stringToTest).toLowerCase().includes(testString));
};
brews = _.filter(brews, (brew)=>{ brews = _.filter(brews, (brew)=>{
const brewStrings = [] const brewStrings = _.deburr([
.concat(brew.title) brew.title,
.concat(brew.description) brew.description,
.concat(brew.tags); brew.tags].join('\n')
.toLowerCase());
return _.filter(brewStrings, (brewString)=>{return checkString(brewString);}).length > 0; return brewStrings.includes(testString);
}); });
return _.orderBy(brews, (brew)=>{ return this.sortBrewOrder(brew); }, this.state.sortDir); return _.orderBy(brews, (brew)=>{ return this.sortBrewOrder(brew); }, this.state.sortDir);
}, },