Mixonomer/src/js/Playlist/NewPlaylist.js

93 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-07-31 20:31:01 +01:00
import React, { Component } from "react";
import { BrowserRouter as Redirect } from "react-router-dom";
2019-07-31 20:31:01 +01:00
const axios = require('axios');
2019-08-05 22:55:07 +01:00
import showMessage from "../Toast.js"
2019-07-31 20:31:01 +01:00
class NewPlaylist extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
2019-08-05 22:55:07 +01:00
type: 'normal'
2019-07-31 20:31:01 +01:00
}
this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleInputChange(event){
this.setState({
[event.target.name]: event.target.value
});
}
handleSubmit(event){
axios.get('/api/playlists')
.then((response) => {
2019-08-07 16:22:31 +01:00
var names = response.data.playlists.map(entry => entry.name)
var sameName = names.includes(this.state.name);
if(sameName == false){
2019-07-31 20:31:01 +01:00
axios.put('/api/playlist', {
name: this.state.name,
parts: [],
2019-08-04 02:11:33 +01:00
playlist_references: [],
2019-07-31 20:31:01 +01:00
shuffle: false,
type: this.state.type,
2019-08-05 22:55:07 +01:00
}).then((response) => {
showMessage(`${this.state.name} created`);
2019-07-31 20:31:01 +01:00
}).catch((error) => {
2019-08-05 22:55:07 +01:00
showMessage(`error creating playlist (${error.response.status})`);
});
2019-07-31 20:31:01 +01:00
}else{
2019-08-05 22:55:07 +01:00
showMessage('named playlist already exists');
2019-07-31 20:31:01 +01:00
}
2019-08-05 22:55:07 +01:00
})
.catch((error) => {
showMessage(`error getting playlists (${error.response.status})`);
2019-07-31 20:31:01 +01:00
});
}
render(){
return (
<table className="app-table">
<thead>
<tr>
<th colSpan="2">
<h1 className="ui-text center-text">new playlist</h1>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select className="full-width" name="type" onChange={this.handleInputChange}>
<option value="default">normal</option>
2019-07-31 20:31:01 +01:00
<option value="recents">recents</option>
</select>
</td>
<td>
<input
className="full-width"
name="name"
type="text"
value={this.state.name}
onChange={this.handleInputChange}
placeholder="name"/>
</td>
</tr>
<tr>
<td colSpan="2">
<input type="submit" className="button full-width" onClick={this.handleSubmit} value="create" />
2019-07-31 20:31:01 +01:00
</td>
</tr>
</tbody>
</table>
);
}
}
export default NewPlaylist;