DAVIDS/public/script.jsx

168 lines
4.8 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
var Authenticate = React.createClass({
2016-04-21 01:10:27 +02:00
logout: function () {
this.props.refreshAuth(null, null);
2016-04-23 02:11:16 +02:00
window.location.href = "/";
2016-04-21 01:10:27 +02:00
},
refreshToken: function (event) {
event.preventDefault();
var self = this;
$.ajax({
url: '/refresh_token',
data: {
'refresh_token': self.props.auth.refresh_token
},
success: function (data) {
self.props.refreshAuth(data.access_token, self.props.auth.refresh_token);
},
error: function (xhr, status, err) {
console.error(status, err);
}
});
},
render: function () {
2016-04-21 01:10:27 +02:00
var auth;
2016-04-22 00:12:57 +02:00
var Nav = ReactBootstrap.Nav,
NavItem = ReactBootstrap.NavItem,
Navbar = ReactBootstrap.Navbar;
2016-04-21 01:10:27 +02:00
if (this.props.auth.access_token == null) {
auth = (
2016-04-22 00:12:57 +02:00
<Nav pullRight>
<NavItem eventKey={1} href="/login"><strong>Login With Spotify</strong></NavItem>
</Nav>
);
}
else {
2016-04-21 01:10:27 +02:00
auth = (
2016-04-22 00:12:57 +02:00
<Nav pullRight>
<NavItem eventKey={2} onClick={this.refreshToken} href="#">Refresh my token</NavItem>
<NavItem eventKey={3} onClick={this.logout} href="#"><strong>Logout</strong></NavItem>
</Nav>
);
2016-04-17 20:37:09 +02:00
}
2016-04-21 01:10:27 +02:00
return (
2016-04-22 00:12:57 +02:00
<Navbar fluid={true}>
<Navbar.Header>
<Navbar.Brand>
<a href="#">Duplicate Finder</a>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#" active>Finder</NavItem>
</Nav>
{auth}
</Navbar.Collapse>
</Navbar>
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')
);