Files
Iris/src/js/components/SearchForm.js

98 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-11-03 08:57:16 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { hashHistory } from 'react-router'
2016-11-03 08:57:16 +13:00
import { createStore, bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
import * as helpers from '../helpers'
2016-11-03 08:57:16 +13:00
import * as uiActions from '../services/ui/actions'
class SearchForm extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2016-11-03 08:57:16 +13:00
super(props);
this.state = {
query: '',
in_focus: false
2016-11-03 08:57:16 +13:00
}
}
2017-06-21 09:16:26 +12:00
componentDidMount(){
if (this.props.query){
2018-01-05 21:31:59 +13:00
var query = this.props.query.replace("search:","");
this.setState({query: query});
}
2017-06-21 09:16:26 +12:00
}
componentWillReceiveProps(newProps){
if (newProps.query && newProps.query != this.state.query && !this.state.in_focus){
2018-01-05 21:31:59 +13:00
this.setState({query: newProps.query.replace("search:","")});
2017-06-21 09:16:26 +12:00
}
}
2016-11-03 08:57:16 +13:00
handleSubmit(e){
e.preventDefault()
// check for uri type matching
switch (helpers.uriType(this.state.query)){
case 'album':
hashHistory.push(global.baseURL+'album/'+encodeURIComponent(this.state.query))
break
case 'artist':
hashHistory.push(global.baseURL+'artist/'+encodeURIComponent(this.state.query))
break
case 'playlist':
hashHistory.push(global.baseURL+'playlist/'+encodeURIComponent(this.state.query))
break
default:
2018-01-05 21:31:59 +13:00
var available_views = ["artist","album","playlist","track"];
var view_defined = false;
var query = this.state.query;
for (var i = 0; i < available_views.length; i++){
if (query.startsWith(available_views[i]+':')){
view_defined = true;
}
}
if (!view_defined){
query = "all:"+query;
}
hashHistory.push(global.baseURL+'search/search:'+query);
break
}
2016-11-03 08:57:16 +13:00
return false
}
render(){
return (
<form className="search-form" onSubmit={e => this.handleSubmit(e)}>
<label>
<input
type="text"
placeholder="Search..."
onChange={ e => this.setState({ query: e.target.value }) }
onFocus={e => this.setState({in_focus: true})}
onBlur={e => this.setState({in_focus: false})}
value={ this.state.query } />
</label>
2016-11-03 08:57:16 +13:00
</form>
)
}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch)
2016-11-03 08:57:16 +13:00
}
}
export default connect(mapDispatchToProps)(SearchForm)