import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import { Link, hashHistory } from 'react-router' import ArtistSentence from './ArtistSentence' import * as helpers from '../helpers' import * as uiActions from '../services/ui/actions' import * as spotifyActions from '../services/spotify/actions' class AddSeedField extends React.Component{ constructor(props) { super(props) this.state = { value: '' } this.id = helpers.generateGuid() this.timer = null this.handleClick = this.handleClick.bind(this) } componentDidMount(){ window.addEventListener("click", this.handleClick, false) if (!this.props.genres){ this.props.spotifyActions.getGenres() } } componentWillUnmount(){ window.removeEventListener("click", this.handleClick, false) } handleClick(e){ if ($(e.target).closest('.add-seed-field').length <= 0){ this.props.spotifyActions.clearAutocompleteResults(this.id) } } handleChange(e,value){ var self = this // update our local state this.setState({value: value}) // start a timer to perform the actual search // this provides a wee delay between key presses to avoid request spamming clearTimeout(this.timer) this.timer = setTimeout( function(){ self.setState({searching: true}) self.props.spotifyActions.getAutocompleteResults(self.id,value,['artist','track','genre']) }, 500 ) } handleSelect(e,item){ this.setState({value: ''}) this.props.onSelect(e,item.uri) this.props.spotifyActions.clearAutocompleteResults(this.id) // Add our selected item to our global index switch (helpers.uriType(item.uri)){ case 'artist': this.props.uiActions.artistLoaded(item.uri,item) break case 'track': this.props.uiActions.trackLoaded(item.uri,item) break } } results(){ if (typeof(this.props.results) === 'undefined'){ return null } else if (typeof(this.props.results[this.id]) === 'undefined'){ return null } else { return this.props.results[this.id] } } renderResults(type){ var results = this.results() if (!results || typeof(results[type]) === 'undefined' || results[type].length <= 0) return null // only show the first 3 var items = results[type].slice(0,3) return (