From 7ee218798b266c5660b57953d078e630ff9dd172 Mon Sep 17 00:00:00 2001 From: Cesar Del Solar Date: Sat, 15 Sep 2018 15:08:40 -0400 Subject: [PATCH] A few more fixes/refactor --- djAerolith/whitleyCards/views.py | 2 +- djAerolith/wordwalls/api.py | 2 +- .../dialog_container_with_search_rows.jsx | 22 +-------- .../js/wordwalls/newtable/search/row.jsx | 17 ++++--- .../js/wordwalls/newtable/search/types.js | 47 +++++++++++++++---- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/djAerolith/whitleyCards/views.py b/djAerolith/whitleyCards/views.py index 81e6aa5d..516d3273 100644 --- a/djAerolith/whitleyCards/views.py +++ b/djAerolith/whitleyCards/views.py @@ -41,7 +41,7 @@ def search_criteria_to_b64(request_post): break new_search['searchType'] = search_type - for k in ('minValue', 'maxValue', 'valueList'): + for k in ('minValue', 'maxValue', 'value'): v = request_post.get('{}[{}]'.format(key, k)) if v: new_search[k] = v diff --git a/djAerolith/wordwalls/api.py b/djAerolith/wordwalls/api.py index 7f9949b3..6f8e71fd 100644 --- a/djAerolith/wordwalls/api.py +++ b/djAerolith/wordwalls/api.py @@ -209,7 +209,7 @@ def build_search_criteria(user, lexicon, fe_search_criteria): }) elif criterion['searchType'] == SearchDescription.HAS_TAGS: - tags = criterion['valueList'].split(',') + tags = criterion['value'].split(',') new_tags = [] for t in tags: stripped = t.strip() diff --git a/djAerolith/wordwalls/static/js/wordwalls/newtable/dialog_container_with_search_rows.jsx b/djAerolith/wordwalls/static/js/wordwalls/newtable/dialog_container_with_search_rows.jsx index 60241ef5..1a25269d 100644 --- a/djAerolith/wordwalls/static/js/wordwalls/newtable/dialog_container_with_search_rows.jsx +++ b/djAerolith/wordwalls/static/js/wordwalls/newtable/dialog_container_with_search_rows.jsx @@ -5,7 +5,7 @@ */ import React from 'react'; -import { SearchTypesEnum, searchCriterionToAdd } from './search/types'; +import { searchCriterionToAdd } from './search/types'; function withSearchRows(WrappedDialogContainer, allowedSearchTypes, searchCriteria) { return class extends React.Component { @@ -55,25 +55,7 @@ function withSearchRows(WrappedDialogContainer, allowedSearchTypes, searchCriter searchTypeChange(index, value) { const criteria = this.state.searchCriteria; const searchType = parseInt(value, 10); - - criteria[index].searchType = searchType; - // Reset the values. - if ([SearchTypesEnum.LENGTH, SearchTypesEnum.NUM_ANAGRAMS, SearchTypesEnum.NUM_VOWELS, - SearchTypesEnum.POINTS, SearchTypesEnum.PROBABILITY].includes(searchType)) { - // Defaults to two options for this criteria - min/max - criteria[index].setOptions({ - minValue: SearchTypesEnum.properties[searchType].defaultMin, - maxValue: SearchTypesEnum.properties[searchType].defaultMax, - }); - } else if (searchType === SearchTypesEnum.FIXED_LENGTH) { - criteria[index].setOptions({ - value: SearchTypesEnum.properties[searchType].default, - }); - } else if (searchType === SearchTypesEnum.TAGS) { - criteria[index].setOptions({ - valueList: '', - }); - } + criteria[index].resetSearchType(searchType); this.setState({ searchCriteria: criteria, }); diff --git a/djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx b/djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx index 5d6e105f..1ca3ae6b 100644 --- a/djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx +++ b/djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx @@ -67,31 +67,30 @@ MinMaxValues.propTypes = { index: PropTypes.number.isRequired, }; -const ListValue = props => ( +const StringValue = props => (
props.modifySearchParam( props.index, - 'valueList', + 'value', event.target.value, )} />
); -ListValue.propTypes = { - valueList: PropTypes.string.isRequired, +StringValue.propTypes = { + value: PropTypes.string.isRequired, index: PropTypes.number.isRequired, modifySearchParam: PropTypes.func.isRequired, }; const SearchRow = (props) => { let specificForm; - const inputType = SearchTypesEnum.properties[props.searchType].inputType; - switch (inputType) { + switch (SearchTypesEnum.properties[props.searchType].inputType) { case SearchTypesInputs.TWO_NUMBERS: specificForm = ( { break; case SearchTypesInputs.ONE_STRING: specificForm = ( - ); diff --git a/djAerolith/wordwalls/static/js/wordwalls/newtable/search/types.js b/djAerolith/wordwalls/static/js/wordwalls/newtable/search/types.js index 14d2c9a5..3a85802e 100644 --- a/djAerolith/wordwalls/static/js/wordwalls/newtable/search/types.js +++ b/djAerolith/wordwalls/static/js/wordwalls/newtable/search/types.js @@ -41,7 +41,7 @@ const SearchTypesEnum = { 3: { name: 'has_tags', displayName: 'Has Tags', - valueList: '', + default: '', inputType: SearchTypesInputs.ONE_STRING, }, 4: { @@ -125,6 +125,10 @@ class SearchCriterion { this.options = options; } + inputType() { + return SearchTypesEnum.properties[this.searchType].inputType; + } + deepCopy() { return new SearchCriterion(this.searchType, { ...this.options, @@ -148,20 +152,47 @@ class SearchCriterion { */ setOption(optionName, optionValue) { const valueModifier = (val) => { - if (['minValue', 'maxValue', 'value'].includes(optionName)) { - return parseInt(val, 10) || 0; - } else if (optionName === 'valueList') { - return val.trim(); + switch (this.inputType()) { + case SearchTypesInputs.ONE_NUMBER: + case SearchTypesInputs.TWO_NUMBERS: + return parseInt(val, 10) || 0; + case SearchTypesInputs.ONE_STRING: + return val.trim(); + default: + throw new Error('Unsupported option name'); } - throw new Error('Unsupported option name'); }; - this.options[optionName] = valueModifier(optionValue); } setOptions(options) { Object.keys(options).forEach(key => this.setOption(key, options[key])); } + + resetSearchType(searchType) { + this.searchType = searchType; + // Reset the values. + switch (SearchTypesEnum.properties[searchType].inputType) { + case SearchTypesInputs.TWO_NUMBERS: + this.setOptions({ + minValue: SearchTypesEnum.properties[searchType].defaultMin, + maxValue: SearchTypesEnum.properties[searchType].defaultMax, + }); + break; + case SearchTypesInputs.ONE_NUMBER: + this.setOptions({ + value: SearchTypesEnum.properties[searchType].default, + }); + break; + case SearchTypesInputs.ONE_STRING: + this.setOptions({ + value: SearchTypesEnum.properties[searchType].default, + }); + break; + default: + break; + } + } } /** @@ -190,7 +221,7 @@ function searchCriterionToAdd(wordSearchCriteria, allowedSearchTypes) { switch (typeProps.inputType) { case SearchTypesInputs.ONE_STRING: return new SearchCriterion(newtypeId, { - valueList: '', + value: SearchTypesEnum.properties[newtypeId].default, }); case SearchTypesInputs.TWO_NUMBERS: return new SearchCriterion(newtypeId, {