DAVIDS/src/public/components/app.js

154 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-04-26 01:46:59 +02:00
/*
* Copyright (c) 2016 Gabriel Augendre <gabriel@augendre.info>
* Free software under MIT License. See LICENSE file.
*/
2016-04-23 02:23:11 +02:00
'use strict';
var React = require('react');
var $ = require('jquery');
var DuplicateFinderBox = require('./duplicate-finder-box');
var Authenticate = require('./authenticate');
2016-04-23 16:18:41 +02:00
var About = require('./about');
2016-04-23 02:23:11 +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;
}
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,
playlists: null,
2016-04-23 16:18:41 +02:00
playlistsLoading: true,
activeTab: {
finder: true,
about: false
}
2016-04-23 02:23:11 +02:00
};
},
getPlaylists: function () {
this.setState({
playlistsLoading: true
});
var self = this;
$.ajax({
url: '/get_playlists',
data: {
'access_token': this.state.access_token
},
success: function (data) {
var pl = data.data;
self.setState({
playlists: pl,
playlistsLoading: false
});
},
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) {
this.setState({
access_token: access,
refresh_token: refresh
});
if (!(access == null && refresh == null)) {
this.getPlaylists();
}
},
componentDidMount: function () {
if (this.isLoggedIn()) {
this.getPlaylists();
}
},
2016-04-23 16:18:41 +02:00
showFinder: function (e) {
e.preventDefault();
this.setState({
activeTab: {
finder: true,
about: false
}
});
},
showAbout: function (e) {
e.preventDefault();
this.setState({
activeTab: {
finder: false,
about: true
}
});
},
2016-04-23 02:23:11 +02:00
render: function () {
var auth = {
access_token: this.state.access_token,
refresh_token: this.state.refresh_token
};
2016-04-23 16:18:41 +02:00
var content;
if (this.state.activeTab.finder) {
2016-04-23 02:23:11 +02:00
content = (
2016-04-23 16:18:41 +02:00
<div>
2016-04-23 19:27:51 +02:00
<h2>Login</h2>
2016-04-23 16:18:41 +02:00
<p>Please <a href="/login">login with Spotify</a> first :)</p>
</div>
2016-04-23 02:23:11 +02:00
);
2016-04-23 16:18:41 +02:00
if (this.isLoggedIn()) {
var playlistsLoading = this.state.playlistsLoading;
content = (
<DuplicateFinderBox playlistsLoading={playlistsLoading} auth={auth}
playlists={this.state.playlists}/>
);
}
}
else if (this.state.activeTab.about) {
content = (<About />);
}
else {
content = <p>Error...</p>
2016-04-23 02:23:11 +02:00
}
return (
<div>
2016-04-23 16:18:41 +02:00
<Authenticate activeTab={this.state.activeTab} showFinder={this.showFinder} showAbout={this.showAbout}
auth={auth} refreshAuth={this.refreshAuth}/>
2016-04-23 02:23:11 +02:00
<div className="container-fluid">
2016-04-26 02:51:54 +02:00
<h1>DAVIDS <small className="hidden-sm hidden-xs">DuplicAtes VIewer and Detector for Spotify</small></h1>
2016-04-23 02:23:11 +02:00
{content}
</div>
2016-04-26 01:42:59 +02:00
<footer className="footer">
<div className="container-fluid">
<p className="text-muted">
2016-04-26 02:50:53 +02:00
Copyright © {(new Date()).getFullYear()} Gabriel Augendre &bull; <a
2016-04-26 03:03:42 +02:00
href="https://github.com/Crocmagnon/DAVIDS/tree/master/LICENSE">License (MIT)</a> &bull; <a
2016-04-26 02:50:53 +02:00
href="https://github.com/Crocmagnon/">Other work</a>
2016-04-26 01:42:59 +02:00
</p>
</div>
</footer>
2016-04-23 02:23:11 +02:00
</div>
);
}
});
module.exports = App;