Skip to content

Commit

Permalink
Merge pull request #297 from domino14/fix-cards-search
Browse files Browse the repository at this point in the history
Fix cards search!
  • Loading branch information
domino14 authored Sep 15, 2018
2 parents e5cee57 + 7ee2187 commit fccde91
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 168 deletions.
2 changes: 1 addition & 1 deletion djAerolith/current_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_VERSION = '1.1.3.0'
CURRENT_VERSION = '1.1.3.1'
141 changes: 45 additions & 96 deletions djAerolith/flashcards/static/js/flashcards/views/word_search_form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import PropTypes from 'prop-types';

import SearchRows from '../../../../../wordwalls/static/js/wordwalls/newtable/search/rows';
import { SearchTypesEnum,
searchCriterionToAdd,
SearchCriterion } from '../../../../../wordwalls/static/js/wordwalls/newtable/search/types';

import Select from '../../../../../wordwalls/static/js/wordwalls/forms/select';
import ContainerWithSearchRows from '../../../../../wordwalls/static/js/wordwalls/newtable/dialog_container_with_search_rows';


const allowedSearchTypes = new Set([
SearchTypesEnum.PROBABILITY,
Expand All @@ -23,92 +24,31 @@ const allowedSearchTypes = new Set([
SearchTypesEnum.NUM_VOWELS,
]);

const lexOptions = [{
value: 'America',
displayValue: 'America',
}, {
value: 'CSW15',
displayValue: 'CSW15',
}];

class WordSearchForm extends React.Component {
constructor(props) {
super(props);
this.searchSubmit = this.searchSubmit.bind(this);

this.state = {
wordSearchCriteria: [
new SearchCriterion(SearchTypesEnum.LENGTH, {
minValue: 7,
maxValue: 7,
}),
new SearchCriterion(SearchTypesEnum.PROBABILITY, {
minValue: 1,
maxValue: 200,
}),
],
lexicon: 'America', // we will build a giant word wall
lexicon: 'America',
};

this.addSearchRow = this.addSearchRow.bind(this);
this.removeSearchRow = this.removeSearchRow.bind(this);
this.modifySearchParam = this.modifySearchParam.bind(this);
this.modifySearchType = this.modifySearchType.bind(this);
}

addSearchRow() {
const toadd = searchCriterionToAdd(this.state.wordSearchCriteria);
if (!toadd) {
return;
}
const newCriteria = this.state.wordSearchCriteria.concat(toadd);
this.setState({
wordSearchCriteria: newCriteria,
});
}

removeSearchRow(criteriaIndex) {
const currentCriteria = this.state.wordSearchCriteria;
currentCriteria.splice(criteriaIndex, 1);
this.setState({
wordSearchCriteria: currentCriteria,
});
}

modifySearchParam(index, paramName, paramValue) {
const criteria = this.state.wordSearchCriteria;
criteria[index].setOption(paramName, paramValue);
this.setState({
wordSearchCriteria: criteria,
});
}

modifySearchType(index, value) {
const criteria = this.state.wordSearchCriteria;
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: '',
});
}
this.setState({
wordSearchCriteria: criteria,
searchSubmit() {
this.props.loadWords({
searchCriteria: this.props.searches.map(s => s.toJSObj()),
lexicon: this.state.lexicon,
});
}

/**
* Turn the search criteria into something the back end would understand.
* Copied verbatim from table_creator.jsx
* @return {Array.<Object>}
*/
searchCriteriaMapper() {
return this.state.wordSearchCriteria.map(criterion => criterion.toJSObj());
}

render() {
return (
<form>
Expand All @@ -118,13 +58,7 @@ class WordSearchForm extends React.Component {
colSize={6}
label="Lexicon"
selectedValue={this.state.lexicon}
options={[{
value: 'America',
displayValue: 'America',
}, {
value: 'CSW15',
displayValue: 'CSW15',
}]}
options={lexOptions}
onChange={(event) => {
this.setState({
lexicon: event.target.value,
Expand All @@ -137,11 +71,11 @@ class WordSearchForm extends React.Component {
<div className="row" style={{ marginTop: 15 }}>
<div className="col-xs-12">
<SearchRows
searches={this.state.wordSearchCriteria}
addSearchRow={this.addSearchRow}
removeSearchRow={this.removeSearchRow}
modifySearchType={this.modifySearchType}
modifySearchParam={this.modifySearchParam}
searches={this.props.searches}
addSearchRow={this.props.addSearchRow}
removeSearchRow={this.props.removeSearchRow}
modifySearchType={this.props.onSearchTypeChange}
modifySearchParam={this.props.onSearchParamChange}
allowedSearchTypes={allowedSearchTypes}
/>
</div>
Expand All @@ -152,12 +86,7 @@ class WordSearchForm extends React.Component {
<button
className="btn btn-primary"
type="button"
onClick={() => {
this.props.loadWords({
searchCriteria: this.searchCriteriaMapper(),
lexicon: this.state.lexicon,
});
}}
onClick={this.searchSubmit}
>Search
</button>
</div>
Expand All @@ -169,7 +98,27 @@ class WordSearchForm extends React.Component {
}

WordSearchForm.propTypes = {
searches: PropTypes.arrayOf(PropTypes.instanceOf(SearchCriterion)).isRequired,
loadWords: PropTypes.func.isRequired,
addSearchRow: PropTypes.func.isRequired,
removeSearchRow: PropTypes.func.isRequired,
onSearchTypeChange: PropTypes.func.isRequired,
onSearchParamChange: PropTypes.func.isRequired,
};

export default WordSearchForm;
const DialogContainer = ContainerWithSearchRows(
WordSearchForm,
allowedSearchTypes,
[
new SearchCriterion(SearchTypesEnum.LENGTH, {
minValue: 7,
maxValue: 7,
}),
new SearchCriterion(SearchTypesEnum.PROBABILITY, {
minValue: 1,
maxValue: 200,
}),
],
);

export default DialogContainer;
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
46 changes: 20 additions & 26 deletions djAerolith/wordwalls/static/js/wordwalls/newtable/search/row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import Select from '../../forms/select';
import NumberInput from '../../forms/number_input';
import TextInput from '../../forms/text_input';

import { SearchTypesEnum, searchCriteriaOptions } from './types';

import {
SearchTypesEnum,
searchCriteriaOptions,
SearchTypesInputs } from './types';

const NumberValue = props => (
<div className="col-xs-3">
Expand Down Expand Up @@ -65,65 +67,57 @@ 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;
switch (props.searchType) {
case SearchTypesEnum.PROBABILITY:
case SearchTypesEnum.POINTS:
case SearchTypesEnum.LENGTH:
case SearchTypesEnum.NUM_VOWELS:
case SearchTypesEnum.NUM_ANAGRAMS:
// XXX TODO this case should be in types.js with more generic code;
// i.e. it should specify how many inputs and of what type, and this
// should just draw that.

switch (SearchTypesEnum.properties[props.searchType].inputType) {
case SearchTypesInputs.TWO_NUMBERS:
specificForm = (
<MinMaxValues
minValue={props.searchCriteria.minValue}
maxValue={props.searchCriteria.maxValue}
minValue={props.searchCriterion.minValue}
maxValue={props.searchCriterion.maxValue}
minAllowed={props.minAllowedValue}
maxAllowed={props.maxAllowedValue}
modifySearchParam={props.modifySearchParam}
index={props.index}
/>);
break;
case SearchTypesEnum.FIXED_LENGTH:
case SearchTypesEnum.NUM_TWO_BLANKS:
case SearchTypesEnum.MAX_SOLUTIONS:
case SearchTypesInputs.ONE_NUMBER:
specificForm = (
<NumberValue
label="Value"
paramName="value"
defaultValue={props.searchCriteria.value}
defaultValue={props.searchCriterion.value}
minAllowed={props.minAllowedValue}
maxAllowed={props.maxAllowedValue}
modifySearchParam={props.modifySearchParam}
index={props.index}
/>);
break;
case SearchTypesEnum.TAGS:
case SearchTypesInputs.ONE_STRING:
specificForm = (
<ListValue
valueList={props.searchCriteria.valueList}
<StringValue
value={props.searchCriterion.value}
index={props.index}
modifySearchParam={props.modifySearchParam}
/>);
Expand Down Expand Up @@ -168,7 +162,7 @@ const SearchRow = (props) => {
SearchRow.propTypes = {
searchType: PropTypes.number.isRequired,
index: PropTypes.number.isRequired,
searchCriteria: PropTypes.objectOf(PropTypes.any).isRequired,
searchCriterion: PropTypes.objectOf(PropTypes.any).isRequired,
minAllowedValue: PropTypes.number,
maxAllowedValue: PropTypes.number,
addRow: PropTypes.func.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SearchRows = props => (
key={`row${idx + 0}`}
index={idx}
searchType={search.searchType}
searchCriteria={search.options}
searchCriterion={search.options}
minAllowedValue={SearchTypesEnum.properties[search.searchType].minAllowed}
maxAllowedValue={SearchTypesEnum.properties[search.searchType].maxAllowed}
addRow={props.addSearchRow}
Expand Down
Loading

0 comments on commit fccde91

Please sign in to comment.