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: [], playlists: [], playlist_references: [], type: null, error: false, error_text: null, day_boundary: '', recommendation_sample: '', newPlaylistName: '', newPlaylistReference: '', shuffle: false, include_recommendations: false } this.handleAddPart = this.handleAddPart.bind(this); this.handleAddReference = this.handleAddReference.bind(this); this.handleInputChange = this.handleInputChange.bind(this); this.handleRemoveRow = this.handleRemoveRow.bind(this); this.handleRemoveRefRow = this.handleRemoveRefRow.bind(this); this.handleRun = this.handleRun.bind(this); this.handleShuffleChange = this.handleShuffleChange.bind(this); this.handleRecChange = this.handleRecChange.bind(this); } componentDidMount(){ axios.all([this.getPlaylistInfo(), this.getPlaylists()]) .then(axios.spread((info, playlists) => { this.setState(info.data); this.setState({playlists: playlists.data.playlists}); })) .catch((error) => { this.setState({ error: true, error_text: "error pulling playlist info" }); }); } getPlaylistInfo(){ return axios.get(`/api/playlist?name=${ this.state.name }`); } getPlaylists(){ return axios.get(`/api/playlists`); } handleInputChange(event){ console.log(event.target.name + event.target.value); this.setState({ [event.target.name]: event.target.value }); if(event.target.name == 'day_boundary'){ this.handleDayBoundaryChange(event.target.value); } if(event.target.name == 'recommendation_sample'){ this.handleRecSampleChange(event.target.value); } } handleDayBoundaryChange(boundary) { axios.post('/api/playlist', { name: this.state.name, day_boundary: parseInt(boundary) }).catch((error) => { console.log(error); }); } handleRecSampleChange(sample){ axios.post('/api/playlist', { name: this.state.name, recommendation_sample: parseInt(sample) }).catch((error) => { console.log(error); }); } 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); }); } handleRecChange(event) { this.setState({ include_recommendations: event.target.checked }); axios.post('/api/playlist', { name: this.state.name, include_recommendations: event.target.checked }).catch((error) => { console.log(error); }); } handleAddPart(event){ var check = this.state.parts.filter((e) => { return e == event.target.value; }); if(check.length == 0) { var parts = this.state.parts.slice(); parts.push(this.state.newPlaylistName); parts.sort(function(a, b){ if(a < b) { return -1; } if(a > b) { return 1; } return 0; }); this.setState({ parts: parts, newPlaylistName: '' }); axios.post('/api/playlist', { name: this.state.name, parts: parts }).catch((error) => { console.log(error); }); } } handleAddReference(event){ var check = this.state.playlist_references.filter((e) => { return e == event.target.value; }); if(check.length == 0) { var playlist_references = this.state.playlist_references.slice(); playlist_references.push(this.state.newPlaylistReference); playlist_references.sort(function(a, b){ if(a < b) { return -1; } if(a > b) { return 1; } return 0; }); this.setState({ playlist_references: playlist_references, newPlaylistReference: '' }); axios.post('/api/playlist', { name: this.state.name, playlist_references: playlist_references }).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); }); } handleRemoveRefRow(id, event){ var playlist_references = this.state.playlist_references; playlist_references = playlist_references.filter(e => e !== id); this.setState({ playlist_references: playlist_references }); if(playlist_references.length == 0) { playlist_references = -1; } axios.post('/api/playlist', { name: this.state.name, playlist_references: playlist_references }).catch((error) => { console.log(error); }); } handleRun(event){ axios.get('/api/playlist/run', {params: {name: this.state.name}}) .catch((error) => { console.log(error); }); } render(){ const table = (
{ this.state.name } |
|
---|---|
shuffle output? | |
include recommendations? | |
recommendations sample size | |
added since (days) | |
{ this.state.error_text }
; return this.state.error ? error : table; } } function ReferenceEntry(props) { return ( ); } function ListBlock(props) { return (