Skip to content

Commit

Permalink
A few more fixes/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
domino14 committed Sep 15, 2018
1 parent 24b8f83 commit 7ee2187
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
2 changes: 1 addition & 1 deletion djAerolith/whitleyCards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion djAerolith/wordwalls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});
Expand Down
17 changes: 8 additions & 9 deletions djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,30 @@ MinMaxValues.propTypes = {
index: PropTypes.number.isRequired,
};

const ListValue = props => (
const StringValue = props => (
<div className="col-xs-4 col-sm-6" style={{ marginTop: '2px' }}>
<TextInput
colSize={12}
label="Comma-separated values"
value={props.valueList}
value={props.value}
onChange={event => props.modifySearchParam(
props.index,
'valueList',
'value',
event.target.value,
)}
/>
</div>);

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 = (
<MinMaxValues
Expand All @@ -117,8 +116,8 @@ const SearchRow = (props) => {
break;
case SearchTypesInputs.ONE_STRING:
specificForm = (
<ListValue
valueList={props.searchCriterion.valueList}
<StringValue
value={props.searchCriterion.value}
index={props.index}
modifySearchParam={props.modifySearchParam}
/>);
Expand Down
47 changes: 39 additions & 8 deletions djAerolith/wordwalls/static/js/wordwalls/newtable/search/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const SearchTypesEnum = {
3: {
name: 'has_tags',
displayName: 'Has Tags',
valueList: '',
default: '',
inputType: SearchTypesInputs.ONE_STRING,
},
4: {
Expand Down Expand Up @@ -125,6 +125,10 @@ class SearchCriterion {
this.options = options;
}

inputType() {
return SearchTypesEnum.properties[this.searchType].inputType;
}

deepCopy() {
return new SearchCriterion(this.searchType, {
...this.options,
Expand All @@ -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;
}
}
}

/**
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 7ee2187

Please sign in to comment.