import React, { Component } from "react"; import { BrowserRouter as Redirect } from "react-router-dom"; const axios = require('axios'); class NewPlaylist extends Component { constructor(props) { super(props); this.state = { name: '', type: 'normal', error: false, errorText: null } 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) => { var sameName = response.data.playlists.filter((i) => {i.name == this.state.name ? true : false}); if(sameName.length == 0){ axios.put('/api/playlist', { name: this.state.name, parts: [], playlist_references: [], shuffle: false, type: this.state.type, }).catch((error) => { console.log(error); }).finally(() => { window.location.href = "/app/playlists"; }); }else{ this.setState({ error: true, errorText: 'name already exists' }); } }); } render(){ return ( { this.state.error && }

new playlist

{this.state.errorText}

); } } export default NewPlaylist;