2016-04-17 17:38:58 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-23 01:52:52 +02:00
|
|
|
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 01:59:06 +02:00
|
|
|
var DuplicatesBox = require('./components/dups/duplicates-box');
|
2016-04-17 20:37:09 +02:00
|
|
|
|
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
var PlaylistBox = React.createClass({
|
2016-04-21 01:10:27 +02:00
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
|
|
|
currentId: null
|
|
|
|
};
|
|
|
|
},
|
2016-04-22 17:52:59 +02:00
|
|
|
clickOnItem: function (id, uid, event) {
|
|
|
|
event.preventDefault();
|
2016-04-21 01:10:27 +02:00
|
|
|
this.setState({currentId: id});
|
|
|
|
this.props.handleClick(id, uid);
|
|
|
|
},
|
2016-04-20 19:29:16 +02:00
|
|
|
render: function () {
|
2016-04-22 17:52:59 +02:00
|
|
|
var ListGroup = ReactBootstrap.ListGroup,
|
|
|
|
ListGroupItem = ReactBootstrap.ListGroupItem;
|
2016-04-21 01:10:27 +02:00
|
|
|
var currentId = this.state.currentId;
|
2016-04-22 17:52:59 +02:00
|
|
|
|
2016-04-22 00:12:57 +02:00
|
|
|
var playlists = <p>No playlist found.</p>;
|
2016-04-21 01:26:21 +02:00
|
|
|
if (this.props.playlists) {
|
|
|
|
playlists = this.props.playlists.map(function (pl) {
|
2016-04-21 01:10:27 +02:00
|
|
|
var id = pl.id;
|
|
|
|
return (
|
2016-04-23 00:10:56 +02:00
|
|
|
<ListGroupItem href="#" onClick={this.clickOnItem.bind(this, id, pl.owner.id)} active={currentId == id} key={id}>{pl.name}</ListGroupItem>
|
2016-04-21 01:10:27 +02:00
|
|
|
);
|
|
|
|
}, this);
|
|
|
|
}
|
2016-04-22 17:52:59 +02:00
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
return (
|
2016-04-22 17:52:59 +02:00
|
|
|
<ListGroup>
|
2016-04-20 19:29:16 +02:00
|
|
|
{playlists}
|
2016-04-22 00:12:57 +02:00
|
|
|
</ListGroup>
|
2016-04-20 19:29:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-04-17 20:37:09 +02:00
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
var DuplicateFinderBox = React.createClass({
|
2016-04-21 01:10:27 +02:00
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
|
|
|
currentId: null,
|
|
|
|
currentUId: null,
|
|
|
|
dups: null,
|
2016-04-21 02:24:22 +02:00
|
|
|
dupsLoading: false,
|
|
|
|
clicked: false
|
2016-04-21 01:10:27 +02:00
|
|
|
};
|
|
|
|
},
|
2016-04-22 00:15:36 +02:00
|
|
|
componentWillReceiveProps: function () {
|
2016-04-21 02:24:22 +02:00
|
|
|
this.setState({
|
|
|
|
clicked: false
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 01:10:27 +02:00
|
|
|
handlePlaylistClick: function (id, uid) {
|
|
|
|
this.setState({
|
|
|
|
currentId: id,
|
|
|
|
currentUId: uid,
|
2016-04-21 02:24:22 +02:00
|
|
|
dupsLoading: true,
|
|
|
|
clicked: true
|
2016-04-21 01:10:27 +02:00
|
|
|
});
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: "/pl/" + uid + "/" + id,
|
|
|
|
data: {
|
|
|
|
'access_token': self.props.auth.access_token
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
var dups = data.data;
|
|
|
|
self.setState({
|
|
|
|
dups: dups,
|
2016-04-21 02:02:16 +02:00
|
|
|
dupsLoading: false
|
2016-04-21 01:10:27 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function (xhr, response, err) {
|
|
|
|
console.error(response, err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-04-20 19:29:16 +02:00
|
|
|
render: function () {
|
2016-04-21 02:24:22 +02:00
|
|
|
var duplicates;
|
2016-04-22 00:12:57 +02:00
|
|
|
var Col = ReactBootstrap.Col,
|
|
|
|
Row = ReactBootstrap.Row;
|
|
|
|
|
2016-04-21 02:24:22 +02:00
|
|
|
if (this.state.clicked) {
|
|
|
|
duplicates = <p>Loading...</p>;
|
|
|
|
if (!this.state.dupsLoading) {
|
|
|
|
duplicates = <DuplicatesBox dups={this.state.dups}/>;
|
|
|
|
}
|
|
|
|
duplicates = (
|
2016-04-22 00:12:57 +02:00
|
|
|
<Col md={9} sm={8}>
|
2016-04-21 02:24:22 +02:00
|
|
|
<h2>Duplicates</h2>
|
|
|
|
{duplicates}
|
2016-04-22 00:12:57 +02:00
|
|
|
</Col>
|
2016-04-21 02:24:22 +02:00
|
|
|
)
|
2016-04-21 01:10:27 +02:00
|
|
|
}
|
2016-04-22 00:12:57 +02:00
|
|
|
|
2016-04-21 02:02:16 +02:00
|
|
|
var playlistBox = <p>Loading...</p>;
|
|
|
|
if (!this.props.playlistsLoading) {
|
|
|
|
playlistBox = <PlaylistBox handleClick={this.handlePlaylistClick} playlists={this.props.playlists}/>;
|
|
|
|
}
|
2016-04-22 00:12:57 +02:00
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
return (
|
2016-04-22 17:52:59 +02:00
|
|
|
<Row>
|
2016-04-22 00:12:57 +02:00
|
|
|
<Col md={3} sm={4}>
|
2016-04-20 19:29:16 +02:00
|
|
|
<h2>Playlists</h2>
|
2016-04-21 02:02:16 +02:00
|
|
|
{playlistBox}
|
2016-04-22 00:12:57 +02:00
|
|
|
</Col>
|
2016-04-21 02:24:22 +02:00
|
|
|
{duplicates}
|
2016-04-22 00:12:57 +02:00
|
|
|
</Row>
|
2016-04-21 02:02:16 +02:00
|
|
|
);
|
2016-04-20 19:29:16 +02:00
|
|
|
}
|
|
|
|
});
|
2016-04-17 20:37:09 +02:00
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
var Authenticate = React.createClass({
|
2016-04-21 01:10:27 +02:00
|
|
|
logout: function () {
|
|
|
|
this.props.refreshAuth(null, null);
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-04-20 19:29:16 +02:00
|
|
|
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>
|
2016-04-20 19:29:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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-20 19:29:16 +02:00
|
|
|
);
|
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-20 19:29:16 +02:00
|
|
|
}
|
|
|
|
});
|
2016-04-17 19:37:16 +02:00
|
|
|
|
2016-04-20 19:29:16 +02:00
|
|
|
ReactDOM.render(
|
2016-04-21 01:10:27 +02:00
|
|
|
<App />,
|
|
|
|
document.getElementById('content')
|
2016-04-20 19:29:16 +02:00
|
|
|
);
|