Figuring out structure
This commit is contained in:
84
src/components/Album.js
Executable file
84
src/components/Album.js
Executable file
@ -0,0 +1,84 @@
|
||||
|
||||
import React, { PropTypes } from 'react'
|
||||
import { connect } from 'react-redux'
|
||||
import { bindActionCreators } from 'redux'
|
||||
|
||||
import TrackList from './TrackList'
|
||||
import * as albumActions from '../actions/albumActions'
|
||||
|
||||
class Album extends React.Component{
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
album: false
|
||||
}
|
||||
}
|
||||
|
||||
// on render
|
||||
componentDidMount(){
|
||||
this.loadAlbum( this.props.params.id );
|
||||
}
|
||||
|
||||
// when props changed
|
||||
componentWillReceiveProps( nextProps ){
|
||||
if( nextProps.params.id != this.props.params.id ){
|
||||
this.loadAlbum( nextProps.params.id );
|
||||
}
|
||||
}
|
||||
|
||||
loadAlbum( id ){
|
||||
let self = this;
|
||||
|
||||
$.ajax({
|
||||
method: 'GET',
|
||||
cache: true,
|
||||
url: 'https://api.spotify.com/v1/albums/'+id,
|
||||
success: function(album){
|
||||
self.setState({ album: album });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
renderAlbum(){
|
||||
if( this.state.album ){
|
||||
return (
|
||||
<div>
|
||||
<h3>{ this.state.album.name }</h3>
|
||||
<h3>{ this.state.album.label }</h3>
|
||||
<TrackList tracks={this.state.album.tracks.items} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
render(){
|
||||
return (
|
||||
<div>
|
||||
<h3>Single album</h3>
|
||||
{ this.renderAlbum() }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Export our component
|
||||
*
|
||||
* We also integrate our global store, using connect()
|
||||
**/
|
||||
|
||||
const mapStateToProps = (state, ownProps) => {
|
||||
return state;
|
||||
}
|
||||
|
||||
const mapDispatchToProps = (dispatch) => {
|
||||
return {
|
||||
albumActions: bindActionCreators(albumActions, dispatch)
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(Album)
|
||||
81
src/components/App.js
Executable file
81
src/components/App.js
Executable file
@ -0,0 +1,81 @@
|
||||
|
||||
import React, { PropTypes } from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { createStore, bindActionCreators } from 'redux'
|
||||
import { Link } from 'react-router'
|
||||
import { connect } from 'react-redux'
|
||||
|
||||
import * as actions from '../actions/index'
|
||||
import Services from '../services/Services'
|
||||
|
||||
|
||||
/**
|
||||
* The application 'brain'
|
||||
*
|
||||
* All data handling and fetching is done through this handler
|
||||
**/
|
||||
class App extends React.Component{
|
||||
|
||||
constructor(props){
|
||||
super(props);
|
||||
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
}
|
||||
|
||||
handleClick(){
|
||||
console.log('click');
|
||||
//this.props.providerActions.authorizeSpotify();
|
||||
Services.get('services.mopidy')
|
||||
.then( function(MopidyService){
|
||||
MopidyService.connection.playback.getState()
|
||||
.then( function(state){
|
||||
console.log('App.js > playback state', state);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
componentDidMount(){
|
||||
Services.get('services.mopidy')
|
||||
.then( function(MopidyService){
|
||||
MopidyService.connection.playback.getState()
|
||||
.then( function(state){
|
||||
console.log('App.js > playback state', state);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
render(){
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ul role="nav">
|
||||
<li><Link to="/queue">Now playing</Link></li>
|
||||
<li><Link to="/album/6N51k5TP5pSZYPf7bLffLe">808's and Heartbreak</Link></li>
|
||||
<li><Link to="/album/1PgfRdl3lPyACfUGH4pquG">A million</Link></li>
|
||||
<li><Link to="/library/albums">Library: Albums</Link></li>
|
||||
<li><Link to="/album">Album</Link></li>
|
||||
<li><a onClick={this.handleClick}>Authorize</a></li>
|
||||
</ul>
|
||||
{this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)(App)
|
||||
67
src/components/Queue.js
Executable file
67
src/components/Queue.js
Executable file
@ -0,0 +1,67 @@
|
||||
|
||||
import React, { PropTypes } from 'react'
|
||||
import { connect } from 'react-redux'
|
||||
import { bindActionCreators } from 'redux'
|
||||
|
||||
import Services from '../services/Services'
|
||||
import TrackList from '../components/TrackList'
|
||||
import * as queueActions from '../actions/queueActions'
|
||||
|
||||
class NowPlaying extends React.Component{
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
tracks: []
|
||||
}
|
||||
}
|
||||
|
||||
// on render
|
||||
componentDidMount(){
|
||||
var self = this;
|
||||
Services.get('services.mopidy')
|
||||
.then( function(MopidyService){
|
||||
MopidyService.connection.tracklist.getTlTracks()
|
||||
.then( function(tracks){
|
||||
self.setState({ tracks : tracks });
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
renderTracks(){
|
||||
if( this.state.tracks ){
|
||||
return (
|
||||
<TrackList tracks={this.state.tracks} />
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
render(){
|
||||
return (
|
||||
<div>
|
||||
<h3>Now playing</h3>
|
||||
{ this.renderTracks() }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Export our component
|
||||
*
|
||||
* We also integrate our global store, using connect()
|
||||
**/
|
||||
|
||||
const mapStateToProps = (state, ownProps) => {
|
||||
return state;
|
||||
}
|
||||
|
||||
const mapDispatchToProps = (dispatch) => {
|
||||
return {
|
||||
queueActions: bindActionCreators(queueActions, dispatch)
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(NowPlaying)
|
||||
42
src/components/TrackList.js
Executable file
42
src/components/TrackList.js
Executable file
@ -0,0 +1,42 @@
|
||||
|
||||
import React, { PropTypes } from 'react'
|
||||
|
||||
export default class Tracklist extends React.Component{
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
flattenTracks(){
|
||||
var tracks = [];
|
||||
if( this.props.tracks ){
|
||||
var originalTracks = this.props.tracks;
|
||||
|
||||
for( var i = 0; i < originalTracks.length; i++ ){
|
||||
var track = originalTracks[i];
|
||||
if( typeof(track.track) !== 'undefined' ){
|
||||
track.track.tlid = track.tlid;
|
||||
track = track.track;
|
||||
}
|
||||
tracks.push( track );
|
||||
}
|
||||
}
|
||||
return tracks;
|
||||
}
|
||||
|
||||
render(){
|
||||
|
||||
if( this.flattenTracks ){
|
||||
return (
|
||||
<ul>
|
||||
{
|
||||
this.flattenTracks().map( track =>
|
||||
<li key={track.uri}>{ track.name }</li>
|
||||
)
|
||||
}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user