Files
Iris/src/js/components/TrackList.js

84 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-10-03 14:44:27 +13:00
import React, { PropTypes } from 'react'
2016-10-14 18:15:34 +13:00
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as actions from '../services/mopidy/actions'
2016-10-14 18:15:34 +13:00
2016-10-03 21:21:14 +13:00
import Track from './Track'
2016-10-03 14:44:27 +13:00
2016-10-14 18:15:34 +13:00
class TrackList extends React.Component{
2016-10-03 14:44:27 +13:00
constructor(props) {
super(props);
2016-10-06 16:38:06 +13:00
this.state = {
tracks: this.props.tracks
}
2016-10-03 14:44:27 +13:00
}
2016-10-06 16:38:06 +13:00
toggleSelected( index ){
var tracks = this.state.tracks;
tracks[index].selected = !tracks[index].selected;
this.setState({ tracks: tracks });
}
2016-10-06 16:38:06 +13:00
playTrack( index ){
var tracks = this.state.tracks;
console.log('playTrack', tracks[index].tlid)
2016-10-14 18:15:34 +13:00
this.props.actions.changeTrack( index )
2016-10-06 16:38:06 +13:00
}
2016-10-03 14:44:27 +13:00
2016-10-06 16:38:06 +13:00
componentWillReceiveProps( nextProps ){
this.setState({ tracks: nextProps.tracks });
}
render(){
let self = this;
if( this.state.tracks ){
2016-10-03 14:44:27 +13:00
return (
<ul>
{
2016-10-06 16:38:06 +13:00
this.state.tracks.map(
(track, index) => {
// flatten nested track objects (as in the case of TlTracks)
2016-10-03 21:21:14 +13:00
if( typeof(track.track) !== 'undefined' ){
2016-10-06 16:38:06 +13:00
for( var property in track.track ){
if( track.track.hasOwnProperty(property) ){
track[property] = track.track[property]
}
}
2016-10-03 21:21:14 +13:00
}
2016-10-06 16:38:06 +13:00
return <Track
key={index+'_'+track.uri}
track={track}
playTrack={() => self.playTrack(index)}
toggleSelected={() => self.toggleSelected(index)} />
2016-10-03 21:21:14 +13:00
}
2016-10-03 14:44:27 +13:00
)
}
</ul>
);
}
return null;
}
2016-10-14 18:15:34 +13:00
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return state;
}
const mapDispatchToProps = (dispatch) => {
return {
actions: bindActionCreators(actions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(TrackList)