import React, { Component } from "react"; import PropTypes from "prop-types"; import CustomInput from "components/CustomInput.jsx"; import CustomErrors from "components/CustomMessages.jsx"; import { connect } from "react-redux"; import { closeOverlay1, openOverlay2, closeOverlay2 } from "actions/stationOverlay"; import { selectPlaylist, deselectPlaylists } from "actions/playlistQueue"; import io from "io"; @connect(state => ({ user: { userId: state.user.get("userId"), role: state.user.get("role"), }, loggedIn: state.user.get("loggedIn"), stationId: state.station.get("id"), stationOwner: state.station.get("ownerId"), songTitle: state.songPlayer.get("title"), songArtists: state.songPlayer.get("artists"), songDuration: state.songPlayer.get("duration"), simpleSong: state.songPlayer.get("simple"), songExists: state.songPlayer.get("exists"), playlistSelectedId: state.playlistQueue.get("playlistSelected"), })) export default class Settings extends Component { constructor(props) { super(props); this.state = { queue: [], userIdMap: {}, userIdMapLoading: [], playlists: [], }; io.getSocket((socket) => { socket.emit('stations.getQueue', this.props.stationId, data => { if (data.status === 'success') { this.setState({ queue: data.queue, }); data.queue.forEach((song) => { this.checkUserId(song.requestedBy); }); } }); socket.on('event:queue.update', queue => { this.setState({ queue, }); queue.forEach((song) => { this.checkUserId(song.requestedBy); }); }); socket.emit('playlists.indexForUser', res => { if (res.status === 'success') this.setState({ playlists: res.data, }); }); socket.on('event:playlist.create', () => { socket.emit('playlists.indexForUser', res => { if (res.status === 'success') this.setState({ playlists: res.data, }); }); }); socket.on('event:playlist.delete', () => { socket.emit('playlists.indexForUser', res => { if (res.status === 'success') this.setState({ playlists: res.data, }); }); }); }); } isOwner = () => { if (this.props.loggedIn) { if (this.props.user.role === "admin") return true; if (this.props.user.userId === this.props.stationOwner) return true; } return false; }; deleteSong = (songId) => { io.getSocket((socket) => { socket.emit("stations.removeFromQueue", this.props.stationId, songId, (data) => { if (data.status === "success") this.messages.clearAddSuccess("Successfully removed song."); else this.messages.clearAddError("Failed to remove song.", data.message); }); }); }; checkUserId = (userId) => { if (!this.state.userIdMap[`Z${ userId }`] && !this.state.userIdMapLoading[`Z${ userId }`]) { this.setState({ userIdMapLoading: this.state.userIdMapLoading.concat([`Z${ userId }`]), }); io.getSocket((socket) => { socket.emit("users.getUsernameFromId", userId, (data) => { if (data.status === "success") { this.setState({ userIdMap: { [`Z${ userId }`]: data.data, }, }); } }); }); } }; addSongToQueueCallback = (songId) => { io.getSocket((socket) => { // Add song to queue socket.emit("stations.addToQueue", this.props.stationId, songId, res => { if (res.status === "success") { this.messages.clearAddSuccess("Successfully added song."); } else { this.messages.addError(res.message); } this.props.dispatch(closeOverlay2()); }); }); }; addSongToQueue = () => { this.props.dispatch(openOverlay2("searchYouTube", null, this.addSongToQueueCallback)); }; getPlaylistAction = (playlistId) => { if (playlistId === this.props.playlistSelectedId) { return SELECTED; } else return { this.selectPlaylist(playlistId); } }>SELECT; } selectPlaylist = (playlistId) => { this.props.dispatch(selectPlaylist(playlistId)); } deselectAll = () => { this.props.dispatch(deselectPlaylists()); } close = () => { this.props.dispatch(closeOverlay1()); }; render() { console.log(this.isOwner()); return (

Queue

(this.messages = ref) } /> { (this.state.queue) ? ( ) : null }
{ (this.props.playlistSelectedId) ? : null }
); } }