import React, { Component } from "react"; import { BrowserRouter as Redirect } from "react-router-dom"; const axios = require('axios'); import showMessage from "../Toast.js" class NewPlaylist extends Component { constructor(props) { super(props); this.state = { name: '', type: 'default', description: '' } this.handleInputChange = this.handleInputChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } componentDidMount(){ this.setDescription('default'); } setDescription(value){ switch(value){ case 'default': this.setState({ description: 'Merge playlists as-is with deduplication by spotify id' }) break; case 'recents': this.setState({ description: "Select songs from playlists which have been added since a variable number of days" }) break; } } handleInputChange(event){ this.setState({ [event.target.name]: event.target.value }); this.setDescription(event.target.value); } handleSubmit(event){ var name = this.state.name; this.setState({ name: '' }); if(name.length != 0){ axios.get('/api/playlists') .then((response) => { var names = response.data.playlists.map(entry => entry.name) var sameName = names.includes(this.state.name); if(sameName == false){ axios.put('/api/playlist', { name: name, parts: [], playlist_references: [], shuffle: false, type: this.state.type, }).then((response) => { showMessage(`${this.state.name} Created`); }).catch((error) => { showMessage(`Error Creating Playlist (${error.response.status})`); }); }else{ showMessage('Named Playlist Already Exists'); } }) .catch((error) => { showMessage(`Error Getting Playlists (${error.response.status})`); }); }else{ showMessage('Enter Name'); } } render(){ return (

New Playlist



{this.state.description}
); } } export default NewPlaylist;