Mixonomer/src/js/Playlist/PlaylistView.js

190 lines
5.7 KiB
JavaScript
Raw Normal View History

import React, { Component } from "react";
const axios = require('axios');
class PlaylistView extends Component{
constructor(props){
super(props);
this.state = {
name: this.props.match.params.name,
parts: [],
type: null,
error: false,
error_text: null,
day_boundary: '',
2019-07-31 20:31:01 +01:00
newPlaylistName: '',
2019-07-31 20:31:01 +01:00
shuffle: false
}
this.handleAddPart = this.handleAddPart.bind(this);
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);
}
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"
});
});
}
handleInputChange(event){
this.setState({
[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)
}).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);
});
}
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);
this.setState({
parts: parts
});
if(parts.length == 0) {
parts = -1;
}
axios.post('/api/playlist', {
name: this.state.name,
parts: parts
}).catch((error) => {
console.log(error);
});
}
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);
});
}
render(){
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>
</tr>
</thead>
<tbody>
2019-07-31 20:31:01 +01:00
{ this.state.parts.map((part) => <Row part={ part } key={ part } handler={this.handleRemoveRow}/>) }
<tr>
<td>
<input type="text"
name="newPlaylistName"
2019-07-31 20:31:01 +01:00
className="full-width"
value={this.state.newPlaylistName}
onChange={this.handleInputChange}
2019-07-31 20:31:01 +01:00
placeholder="new playlist"></input>
</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>
{ 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"
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>
</tbody>
</table>
);
const error = <p style={{textAlign: "center"}}>{ this.state.error_text }</p>;
return this.state.error ? error : table;
}
}
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>
</tr>
);
}
export default PlaylistView;