2019-09-05 15:36:11 +12:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
|
|
|
|
|
|
import * as helpers from '../../helpers';
|
|
|
|
|
import * as uiActions from '../../services/ui/actions';
|
|
|
|
|
|
2019-10-18 14:02:10 +13:00
|
|
|
class SearchForm extends React.Component {
|
2019-09-05 15:36:11 +12:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
term: this.props.term,
|
|
|
|
|
pristine: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (this.state.pristine && this.state.term == '' && this.state.term !== nextProps.term) {
|
|
|
|
|
this.setState({ term: nextProps.term, pristine: false });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleBlur(e) {
|
|
|
|
|
this.setState({ pristine: false });
|
|
|
|
|
if (this.props.onBlur) {
|
|
|
|
|
this.props.onBlur(this.state.term);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleFocus(e) {
|
|
|
|
|
this.setState({ pristine: false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSubmit(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
// check for uri type matching
|
2019-10-18 14:02:10 +13:00
|
|
|
switch (helpers.uriType(this.state.term)) {
|
2019-09-05 15:36:11 +12:00
|
|
|
case 'album':
|
|
|
|
|
this.props.history.push(`/album/${encodeURIComponent(this.state.term)}`);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'artist':
|
|
|
|
|
this.props.history.push(`/artist/${encodeURIComponent(this.state.term)}`);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'playlist':
|
|
|
|
|
this.props.history.push(`/playlist/${encodeURIComponent(this.state.term)}`);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'track':
|
|
|
|
|
this.props.history.push(`/track/${encodeURIComponent(this.state.term)}`);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this.props.onSubmit(this.state.term);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<form className="search-form" onSubmit={(e) => this.handleSubmit(e)}>
|
|
|
|
|
<label>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder="Search..."
|
|
|
|
|
onChange={(e) => this.setState({ term: e.target.value, pristine: false })}
|
|
|
|
|
onBlur={(e) => this.handleBlur}
|
|
|
|
|
onFocus={(e) => this.handleFocus}
|
2019-10-18 14:02:10 +13:00
|
|
|
value={this.state.term}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
});
|
2016-11-03 08:57:16 +13:00
|
|
|
|
2019-10-18 14:02:10 +13:00
|
|
|
export default connect(mapDispatchToProps)(SearchForm);
|