DAVIDS/public/script.jsx

108 lines
2.9 KiB
React
Raw Normal View History

2016-04-17 17:38:58 +02:00
'use strict';
var $ = window.$ = window.jQuery = require('jquery');
require('bootstrap');
2016-04-23 01:33:07 +02:00
var React = require('react');
var ReactDOM = require('react-dom');
var ReactBootstrap = require('react-bootstrap');
2016-04-21 01:10:27 +02:00
/**
* Obtains parameters from the hash of the URL
* @return Object
*/
function getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while (e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}
2016-04-23 02:13:48 +02:00
var DuplicateFinderBox = require('./components/duplicate-finder-box');
2016-04-17 20:37:09 +02:00
2016-04-23 02:17:40 +02:00
var Authenticate = require('./components/authenticate');
2016-04-21 01:10:27 +02:00
var App = React.createClass({
getInitialState: function () {
var params = getHashParams();
var access_token = params.access_token || null;
var refresh_token = params.refresh_token || null;
return {
access_token: access_token,
refresh_token: refresh_token,
2016-04-21 02:02:16 +02:00
playlists: null,
playlistsLoading: true
2016-04-21 01:10:27 +02:00
};
},
getPlaylists: function () {
2016-04-21 02:02:16 +02:00
this.setState({
playlistsLoading: true
});
2016-04-21 01:10:27 +02:00
var self = this;
$.ajax({
url: '/get_playlists',
data: {
'access_token': this.state.access_token
},
success: function (data) {
var pl = data.data;
self.setState({
2016-04-21 02:02:16 +02:00
playlists: pl,
playlistsLoading: false
2016-04-21 01:10:27 +02:00
});
},
error: function (xhr, response, err) {
console.error(response, err);
}
});
},
isLoggedIn: function () {
return !(this.state.access_token == null && this.state.refresh_token == null);
},
refreshAuth: function (access, refresh) {
2016-04-21 01:17:57 +02:00
this.setState({
access_token: access,
refresh_token: refresh
});
if (!(access == null && refresh == null)) {
2016-04-21 01:10:27 +02:00
this.getPlaylists();
}
},
componentDidMount: function () {
2016-04-21 01:17:57 +02:00
if (this.isLoggedIn()) {
2016-04-21 01:10:27 +02:00
this.getPlaylists();
}
},
render: function () {
var auth = {
access_token: this.state.access_token,
refresh_token: this.state.refresh_token
};
2016-04-21 01:17:57 +02:00
var content = <p>Please log in with Spotify :)</p>;
2016-04-21 01:10:27 +02:00
if (this.isLoggedIn()) {
2016-04-21 02:02:16 +02:00
var playlistsLoading = this.state.playlistsLoading;
2016-04-21 01:10:27 +02:00
content = (
2016-04-21 02:02:16 +02:00
<DuplicateFinderBox playlistsLoading={playlistsLoading} auth={auth} playlists={this.state.playlists}/>
2016-04-21 01:10:27 +02:00
);
}
return (
<div>
<Authenticate auth={auth} refreshAuth={this.refreshAuth}/>
2016-04-21 01:12:41 +02:00
<div className="container-fluid">
2016-04-21 01:10:27 +02:00
{content}
</div>
</div>
);
}
});
2016-04-17 19:37:16 +02:00
ReactDOM.render(
2016-04-21 01:10:27 +02:00
<App />,
document.getElementById('content')
);