2019-07-29 11:44:10 +01:00
|
|
|
import React, { Component } from "react";
|
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
class PlaylistView extends Component{
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2019-07-31 12:24:10 +01:00
|
|
|
name: this.props.match.params.name,
|
|
|
|
parts: [],
|
2019-08-03 12:10:24 +01:00
|
|
|
type: null,
|
2019-07-31 12:24:10 +01:00
|
|
|
error: false,
|
|
|
|
error_text: null,
|
2019-08-03 12:10:24 +01:00
|
|
|
|
|
|
|
day_boundary: '',
|
2019-07-31 20:31:01 +01:00
|
|
|
newPlaylistName: '',
|
2019-08-03 12:10:24 +01:00
|
|
|
|
2019-07-31 20:31:01 +01:00
|
|
|
shuffle: false
|
2019-07-29 11:44:10 +01:00
|
|
|
}
|
2019-07-31 12:24:10 +01:00
|
|
|
this.handleAddPart = this.handleAddPart.bind(this);
|
2019-08-03 12:10:24 +01:00
|
|
|
this.handleInputChange = this.handleInputChange.bind(this);
|
2019-07-31 20:31:01 +01:00
|
|
|
this.handleRemoveRow = this.handleRemoveRow.bind(this);
|
|
|
|
|
2019-08-03 21:35:08 +01:00
|
|
|
this.handleRun = this.handleRun.bind(this);
|
|
|
|
|
2019-07-31 20:31:01 +01:00
|
|
|
this.handleShuffleChange = this.handleShuffleChange.bind(this);
|
2019-07-31 12:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
this.getPlaylistInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlaylistInfo(){
|
|
|
|
axios.get(`/api/playlist?name=${ this.state.name }`)
|
|
|
|
.then((response) => {
|
|
|
|
this.setState(response.data);
|
|
|
|
}).catch((error) => {
|
|
|
|
this.setState({
|
|
|
|
error: true,
|
|
|
|
error_text: "error pulling playlist info"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:10:24 +01:00
|
|
|
handleInputChange(event){
|
2019-07-31 12:24:10 +01:00
|
|
|
this.setState({
|
2019-08-03 12:10:24 +01:00
|
|
|
[event.target.name]: event.target.value
|
|
|
|
});
|
|
|
|
|
|
|
|
if(event.target.name == 'day_boundary'){
|
|
|
|
this.handleDayBoundaryChange(event.target.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDayBoundaryChange(boundary) {
|
|
|
|
axios.post('/api/playlist', {
|
|
|
|
name: this.state.name,
|
2019-08-03 21:35:08 +01:00
|
|
|
day_boundary: parseInt(boundary)
|
2019-08-03 12:10:24 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
console.log(error);
|
2019-07-31 20:31:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleShuffleChange(event) {
|
|
|
|
this.setState({
|
|
|
|
shuffle: event.target.checked
|
|
|
|
});
|
|
|
|
axios.post('/api/playlist', {
|
|
|
|
name: this.state.name,
|
|
|
|
shuffle: event.target.checked
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log(error);
|
2019-07-31 12:24:10 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAddPart(event){
|
|
|
|
var parts = this.state.parts;
|
2019-07-31 20:31:01 +01:00
|
|
|
parts.push(this.state.newPlaylistName);
|
|
|
|
this.setState({
|
|
|
|
parts: parts,
|
|
|
|
add_part_value: ''
|
|
|
|
});
|
|
|
|
axios.post('/api/playlist', {
|
|
|
|
name: this.state.name,
|
|
|
|
parts: parts
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRemoveRow(id, event){
|
|
|
|
var parts = this.state.parts;
|
|
|
|
parts = parts.filter(e => e !== id);
|
2019-07-31 12:24:10 +01:00
|
|
|
this.setState({
|
|
|
|
parts: parts
|
|
|
|
});
|
2019-08-03 12:10:24 +01:00
|
|
|
|
|
|
|
if(parts.length == 0) {
|
|
|
|
parts = -1;
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:24:10 +01:00
|
|
|
axios.post('/api/playlist', {
|
|
|
|
name: this.state.name,
|
|
|
|
parts: parts
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2019-07-29 11:44:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-03 21:35:08 +01:00
|
|
|
handleRun(event){
|
|
|
|
axios.get('/api/playlist/run', {params: {name: this.state.name}})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
render(){
|
2019-07-31 12:24:10 +01:00
|
|
|
|
|
|
|
const table = (
|
|
|
|
<table className="app-table max-width">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2019-07-31 20:31:01 +01:00
|
|
|
<th colSpan="2"><h1 className="text-no-select">{ this.state.name }</h1></th>
|
2019-07-31 12:24:10 +01:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2019-07-31 20:31:01 +01:00
|
|
|
{ this.state.parts.map((part) => <Row part={ part } key={ part } handler={this.handleRemoveRow}/>) }
|
2019-07-31 12:24:10 +01:00
|
|
|
<tr>
|
|
|
|
<td>
|
2019-08-03 12:10:24 +01:00
|
|
|
<input type="text"
|
|
|
|
name="newPlaylistName"
|
2019-07-31 20:31:01 +01:00
|
|
|
className="full-width"
|
|
|
|
value={this.state.newPlaylistName}
|
2019-08-03 12:10:24 +01:00
|
|
|
onChange={this.handleInputChange}
|
2019-07-31 20:31:01 +01:00
|
|
|
placeholder="new playlist"></input>
|
2019-07-31 12:24:10 +01:00
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<button className="button full-width" onClick={this.handleAddPart}>add</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2019-07-31 20:31:01 +01:00
|
|
|
<tr>
|
|
|
|
<td className="center-text ui-text text-no-select">
|
|
|
|
shuffle output?
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input type="checkbox"
|
|
|
|
checked={this.state.shuffle}
|
|
|
|
onChange={this.handleShuffleChange}></input>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2019-08-03 12:10:24 +01:00
|
|
|
{ this.state.type == 'recents' &&
|
|
|
|
<tr>
|
|
|
|
<td className="center-text ui-text text-no-select">
|
|
|
|
day boundary
|
|
|
|
</td>
|
|
|
|
<td>
|
2019-08-03 21:35:08 +01:00
|
|
|
<input type="number"
|
2019-08-03 12:10:24 +01:00
|
|
|
name="day_boundary"
|
|
|
|
className="full-width"
|
|
|
|
value={this.state.day_boundary}
|
|
|
|
onChange={this.handleInputChange}></input>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
}
|
2019-08-03 21:35:08 +01:00
|
|
|
<tr>
|
|
|
|
<td colspan="2">
|
|
|
|
<button className="button full-width" onClick={this.handleRun}>run</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2019-07-31 12:24:10 +01:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
|
|
|
|
const error = <p style={{textAlign: "center"}}>{ this.state.error_text }</p>;
|
|
|
|
|
|
|
|
return this.state.error ? error : table;
|
2019-07-29 11:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-31 12:24:10 +01:00
|
|
|
function Row (props) {
|
|
|
|
return (
|
|
|
|
<tr>
|
2019-07-31 20:31:01 +01:00
|
|
|
<td className="ui-text center-text text-no-select">{ props.part }</td>
|
|
|
|
<td><button className="ui-text center-text button full-width" onClick={(e) => props.handler(props.part, e)}>remove</button></td>
|
2019-07-31 12:24:10 +01:00
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PlaylistView;
|