2015-10-12 13:57:37 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react/addons';
|
|
|
|
|
|
|
|
import Input from 'react-bootstrap/lib/Input';
|
|
|
|
import Glyphicon from 'react-bootstrap/lib/Glyphicon';
|
|
|
|
import { getLangText } from '../utils/lang_utils';
|
|
|
|
|
|
|
|
|
|
|
|
const { func, string, number } = React.PropTypes;
|
|
|
|
const { update } = React.addons;
|
|
|
|
|
|
|
|
const SearchBar = React.createClass({
|
|
|
|
propTypes: {
|
2015-10-12 17:10:04 +02:00
|
|
|
// a function that accepts a string as a search query and updates the
|
|
|
|
// propagated `searchQuery` after successfully retrieving the
|
|
|
|
// request from the server
|
2015-10-12 13:57:37 +02:00
|
|
|
searchFor: func.isRequired,
|
2015-10-12 16:38:00 +02:00
|
|
|
searchQuery: string.isRequired,
|
|
|
|
|
2015-10-12 13:57:37 +02:00
|
|
|
className: string,
|
|
|
|
|
2015-10-12 17:10:04 +02:00
|
|
|
// the number of milliseconds the component
|
|
|
|
// should wait before requesting search results from the server
|
2015-10-12 13:57:37 +02:00
|
|
|
threshold: number.isRequired
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
timers: [],
|
2015-10-12 16:38:00 +02:00
|
|
|
searchQuery: '',
|
2015-10-12 17:10:04 +02:00
|
|
|
loading: false
|
2015-10-12 13:57:37 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-10-12 16:38:00 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const searchQueryProps = this.props.searchQuery;
|
|
|
|
const searchQueryPrevProps = prevProps.searchQuery;
|
|
|
|
const searchQueryState = this.state.searchQuery;
|
2015-10-12 17:10:04 +02:00
|
|
|
const { loading } = this.state;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Condition: `loading` must be true, which implies that `evaluateTimer`,
|
|
|
|
* has already been called
|
|
|
|
*
|
|
|
|
* AND
|
|
|
|
*
|
|
|
|
* 2. Condition: `searchQueryProps` and `searchQueryState` are true and equal
|
|
|
|
* (which means that the search query has been propagated to the inner
|
|
|
|
* fetch method of `fetchPieceList`, which in turn means that a fetch
|
|
|
|
* has completed)
|
|
|
|
*
|
|
|
|
* OR
|
|
|
|
*
|
|
|
|
* 3. Condition: `searchQueryProps` and `searchQueryState` can be any value (`true` or
|
|
|
|
* `false`, as long as they're equal). This case only occurs when the user
|
|
|
|
* has entered a `searchQuery` and deletes the query in one go, reseting
|
|
|
|
* `searchQueryProps` to empty string ('' === false) again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const firstCondition = !!loading;
|
|
|
|
const secondCondition = searchQueryProps && searchQueryState && searchQueryProps === searchQueryState;
|
|
|
|
const thirdCondition = !searchQueryPrevProps && searchQueryProps === searchQueryState;
|
|
|
|
|
|
|
|
if(firstCondition && (secondCondition || thirdCondition)) {
|
|
|
|
this.setState({ loading: false });
|
2015-10-12 16:38:00 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-12 13:57:37 +02:00
|
|
|
startTimers(searchQuery) {
|
|
|
|
const { timers } = this.state;
|
|
|
|
const { threshold } = this.props;
|
2015-10-12 17:10:04 +02:00
|
|
|
|
|
|
|
// the timer waits for the specified threshold time in milliseconds
|
|
|
|
// and then calls `evaluateTimer`, that checks if another letter
|
|
|
|
// has been added to the query (by checking if there are newer
|
|
|
|
// timers on the stack aka. `this.state.timers`)
|
2015-10-12 13:57:37 +02:00
|
|
|
const timer = {
|
|
|
|
searchQuery,
|
2015-10-12 16:38:00 +02:00
|
|
|
cb: setTimeout(this.evaluateTimer(timers.length, searchQuery), threshold)
|
2015-10-12 13:57:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const newTimers = update(timers, {$push: [timer]});
|
|
|
|
this.setState({ timers: newTimers });
|
|
|
|
},
|
|
|
|
|
2015-10-12 16:38:00 +02:00
|
|
|
evaluateTimer(timerIndex, searchQuery) {
|
|
|
|
return () => {
|
|
|
|
const { timers } = this.state;
|
|
|
|
|
2015-10-12 17:10:04 +02:00
|
|
|
// If there have been no timers added to `this.state.timers`,
|
|
|
|
// while the `threshold` was passed, we start loading
|
|
|
|
// by querying the server.
|
2015-10-12 16:38:00 +02:00
|
|
|
if(timers.length <= timerIndex + 1) {
|
2015-10-12 17:10:04 +02:00
|
|
|
// However, we're doing that only after setting `this.state.loading`
|
|
|
|
// to `true`, as we want to display a little spinner while loading.
|
|
|
|
// We also clean the now worthless `timers` array.
|
|
|
|
this.setState({ timers: [], loading: true }, () => {
|
2015-10-12 16:38:00 +02:00
|
|
|
// search for the query
|
|
|
|
this.props.searchFor(searchQuery);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-10-12 13:57:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
handleChange({ target: { value }}) {
|
2015-10-12 17:10:04 +02:00
|
|
|
// On each letter entry we're updating the state of the component
|
|
|
|
// and start a timer, which we're also pushing to the state
|
|
|
|
// of the component
|
2015-10-12 13:57:37 +02:00
|
|
|
this.startTimers(value);
|
2015-10-12 16:38:00 +02:00
|
|
|
this.setState({ searchQuery: value });
|
2015-10-12 13:57:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2015-10-12 16:38:00 +02:00
|
|
|
let searchIcon = <Glyphicon glyph='search' className="filter-glyph"/>;
|
2015-10-12 13:57:37 +02:00
|
|
|
const { className } = this.props;
|
2015-10-12 17:10:04 +02:00
|
|
|
const { loading } = this.state;
|
2015-10-12 16:38:00 +02:00
|
|
|
|
2015-10-12 17:10:04 +02:00
|
|
|
if(loading) {
|
2015-10-12 16:38:00 +02:00
|
|
|
searchIcon = <span className="glyph-ascribe-spool-chunked ascribe-color spin"/>;
|
|
|
|
}
|
2015-10-12 13:57:37 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
<Input
|
|
|
|
type='text'
|
|
|
|
placeholder={getLangText('Search%s', '...')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
addonAfter={searchIcon} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default SearchBar;
|