Clean up usage of Redux for searchTerm

Now that the search term is encoded, we need to be sure both the search fields get a decoded string. Parse, decode and set this string via Redux in componentDidMount, and use it in both places.
This commit is contained in:
Ray Schamp 2017-02-10 18:54:20 -05:00
parent 05adcde88d
commit c8a7d3cdc7

View file

@ -21,13 +21,25 @@ require('./search.scss');
var Search = injectIntl(React.createClass({ var Search = injectIntl(React.createClass({
type: 'Search', type: 'Search',
getDefaultProps: function () { getDefaultProps: function () {
var query = window.location.search;
var pathname = window.location.pathname.toLowerCase(); var pathname = window.location.pathname.toLowerCase();
if (pathname[pathname.length - 1] === '/') { if (pathname[pathname.length - 1] === '/') {
pathname = pathname.substring(0, pathname.length - 1); pathname = pathname.substring(0, pathname.length - 1);
} }
var start = pathname.lastIndexOf('/'); var start = pathname.lastIndexOf('/');
var type = pathname.substring(start + 1, pathname.length); var type = pathname.substring(start + 1, pathname.length);
return {
tab: type,
loadNumber: 16
};
},
getInitialState: function () {
return {
loaded: [],
offset: 0
};
},
componentDidMount: function () {
var query = window.location.search;
var q = query.lastIndexOf('q='); var q = query.lastIndexOf('q=');
var term = ''; var term = '';
if (q !== -1) { if (q !== -1) {
@ -40,22 +52,8 @@ var Search = injectIntl(React.createClass({
term = term.substring(0, term.indexOf('&')); term = term.substring(0, term.indexOf('&'));
} }
term = term.split('+').join(' '); term = term.split('+').join(' ');
return {
tab: type,
searchTerm: term,
loadNumber: 16
};
},
getInitialState: function () {
return {
loaded: [],
offset: 0
};
},
componentDidMount: function () {
this.getSearchMore(); this.getSearchMore();
this.props.dispatch(navigationActions.setSearchTerm(this.props.searchTerm)); this.props.dispatch(navigationActions.setSearchTerm(decodeURI(term)));
}, },
getSearchMore: function () { getSearchMore: function () {
var termText = ''; var termText = '';
@ -113,7 +111,7 @@ var Search = injectIntl(React.createClass({
<Input type="text" <Input type="text"
aria-label={formatMessage({id: 'general.search'})} aria-label={formatMessage({id: 'general.search'})}
placeholder={formatMessage({id: 'general.search'})} placeholder={formatMessage({id: 'general.search'})}
value={decodeURI(this.props.searchTerm)} value={this.props.searchTerm}
name="q" /> name="q" />
</Form> </Form>
</div> </div>
@ -143,7 +141,7 @@ var Search = injectIntl(React.createClass({
var mapStateToProps = function (state) { var mapStateToProps = function (state) {
return { return {
navigation: state.searchTerm searchTerm: state.navigation
}; };
}; };