import React, { Component } from "react"; import { NavLink } from "react-router-dom"; import PropTypes from "prop-types"; import { translate, Trans } from "react-i18next"; import Player from "./Player"; import Seekerbar from "./Seekerbar"; import VolumeSlider from "./VolumeSlider"; import Overlays from "./Views/Overlays"; import { changeVolume } from "actions/volume"; import { changeSong, setTimeElapsed, timePaused, receivedRatings, receivedOwnRatings } from "actions/songPlayer"; import { pauseStation, resumeStation } from "actions/station"; import { openOverlay1 } from "actions/stationOverlay"; import { addSong } from "actions/playlistQueue"; import { connect } from "react-redux"; import io from "io"; import config from "config"; import {updateTimePaused} from "../../actions/songPlayer"; const formatTime = (duration) => { let d = moment.duration(duration, "seconds"); if (duration < 0) return "0:00"; return ((d.hours() > 0) ? (d.hours() < 10 ? ("0" + d.hours() + ":") : (d.hours() + ":")) : "") + (d.minutes() + ":") + (d.seconds() < 10 ? ("0" + d.seconds()) : d.seconds()); }; @connect(state => ({ user: { userId: state.user.get("userId"), role: state.user.get("role"), }, loggedIn: state.user.get("loggedIn"), songId: state.songPlayer.get("songId"), songTitle: state.songPlayer.get("title"), songDuration: state.songPlayer.get("duration"), songTimeElapsed: state.songPlayer.get("timeElapsed"), songArtists: state.songPlayer.get("artists"), songLikes: state.songPlayer.get("likes"), songLiked: state.songPlayer.get("liked"), songDislikes: state.songPlayer.get("dislikes"), songDisliked: state.songPlayer.get("disliked"), simpleSong: state.songPlayer.get("simple"), songExists: state.songPlayer.get("exists"), queueLocked: state.station.get("locked"), partyEnabled: state.station.get("partyMode"), station: { stationId: state.station.get("id"), name: state.station.get("name"), displayName: state.station.get("displayName"), paused: state.station.get("paused"), pausedAt: state.station.get("pausedAt"), ownerId: state.station.get("ownerId"), }, selectedPlaylistObject: { addedSongId: state.playlistQueue.get("addedSongId"), selectedPlaylistId: state.playlistQueue.get("playlistSelected"), }, })) @translate(["station"], { wait: true }) export default class Station extends Component { static propTypes = { t: PropTypes.func, }; static defaultProps = { t: () => {}, }; constructor(props) { super(); this.state = { mode: this.getModeTemp(props.partyEnabled, props.queueLocked), }; io.getSocket(socket => { socket.emit("stations.join", props.station.name, res => { if (res.status === 'success') { if (res.data.currentSong) { res.data.currentSong.startedAt = res.data.startedAt; res.data.currentSong.timePaused = res.data.timePaused; } this.props.dispatch(changeSong(res.data.currentSong)); this.getOwnRatings(); } socket.on('event:songs.next', data => { this.addTopToQueue(); if (data.currentSong) { data.currentSong.startedAt = data.startedAt; data.currentSong.timePaused = data.timePaused; } this.props.dispatch(changeSong(data.currentSong)); this.getOwnRatings(); }); socket.on('event:stations.pause', pausedAt => { this.props.dispatch(pauseStation(pausedAt)); }); socket.on('event:stations.resume', data => { this.props.dispatch(updateTimePaused(data.timePaused)); this.props.dispatch(resumeStation()); }); socket.on('event:song.like', data => { console.log("LIKE"); if (this.props.songExists) { if (data.songId === this.props.songId) { this.props.dispatch(receivedRatings(data.likes, data.dislikes)); } } }); socket.on('event:song.dislike', data => { console.log("DISLIKE"); if (this.props.songExists) { if (data.songId === this.props.songId) { this.props.dispatch(receivedRatings(data.likes, data.dislikes)); } } }); socket.on('event:song.unlike', data => { console.log("UNLIKE"); if (this.props.songExists) { if (data.songId === this.props.songId) { this.props.dispatch(receivedRatings(data.likes, data.dislikes)); } } }); socket.on('event:song.undislike', data => { console.log("UNDISLIKE"); if (this.props.songExists) { if (data.songId === this.props.songId) { this.props.dispatch(receivedRatings(data.likes, data.dislikes)); } } }); socket.on('event:song.newRatings', data => { if (this.props.songExists) { if (data.songId === this.props.songId) { this.props.dispatch(receivedOwnRatings(data.liked, data.disliked)); } } }); }); }); setInterval(() => { if (this.props.songExists) { this.props.dispatch(setTimeElapsed(this.props.station.paused, this.props.station.pausedAt)); // TODO Fix } }, 1000); } isInQueue = (songId, cb) => { io.getSocket((socket) => { socket.emit('stations.getQueue', this.props.stationId, data => { if (data.status === 'success') { data.queue.forEach((song) => { if (song._id === songId) { return cb(true); } }); } return cb(false); }); }); }; checkIfCanAdd = (cb) => { if (this.state.mode === "normal") return cb(false); let playlistId = this.props.selectedPlaylistObject.selectedPlaylistId; let songId = this.props.selectedPlaylistObject.addedSongId; console.log(playlistId, songId, this.props.songId); if (playlistId) { if (songId === this.props.songId) return cb(true); else if (songId === null) return cb(true); else { this.isInQueue(songId, (res) => { return cb(res); }); } } } addTopToQueue = () => { console.log("ADD TOP TO QUEUE!!!"); this.checkIfCanAdd((can) => { if (!can) return; let playlistId = this.props.selectedPlaylistObject.selectedPlaylistId; console.log(can); io.getSocket((socket) => { socket.emit('playlists.getFirstSong', this.props.selectedPlaylistObject.selectedPlaylistId, data => { if (data.status === 'success') { let songId = data.song.songId; if (data.song.duration < 15 * 60) { this.props.dispatch(addSong(songId)); socket.emit('stations.addToQueue', this.props.station.stationId, songId, data2 => { if (data2.status === 'success') { this.moveToBottom(playlistId, songId, (data3) => { if (data3.status === 'success') { } }); } else { this.messages.clearAddError("Could not automatically add top song of playlist to queue.", data2.message); } }); } else { this.messages.clearAddError("Top song in playlist was too long to be added. Moving to the next song."); this.moveToBottom(playlistId, songId, (data3) => { if (data3.status === 'success') { setTimeout(() => { this.addTopToQueue(); }, 2000); } }); } } }); }); }); }; moveToBottom = (playlistId, songId, cb) => { io.getSocket((socket) => { socket.emit('playlists.moveSongToBottom', playlistId, songId, data => { cb(data); }); }); } getModeTemp = (partyEnabled, queueLocked) => { // If party enabled // If queue locked // Mode is DJ // If queue not locked // Mode party // If party not enabled // Mode is normal if (partyEnabled) { if (queueLocked) return "dj"; else return "party"; } else return "normal"; } getOwnRatings = () => { io.getSocket((socket) => { if (!this.props.songExists) return; socket.emit('songs.getOwnSongRatings', this.props.songId, (data) => { if (this.props.songId === data.songId) this.props.dispatch(receivedOwnRatings(data.liked, data.disliked)); }); }); }; isOwner = () => { if (this.props.loggedIn) { if (this.props.user.role === "admin") return true; if (this.props.user.userId === this.props.station.ownerId) return true; } return false; }; addSongTemp = () => { io.getSocket(socket => { socket.emit('stations.addToQueue', this.props.station.stationId, '60ItHLz5WEA', data => { console.log("ATQ Res", data); }); }); }; resumeStation = () => { io.getSocket(socket => { socket.emit('stations.resume', this.props.station.stationId, data => { }); }); }; pauseStation = () => { io.getSocket(socket => { socket.emit('stations.pause', this.props.station.stationId, data => { }); }); }; getRatings = () => { const likes = { this.props.songLikes }; const dislikes = { this.props.songDislikes }; let likeButton = thumb_up; let dislikeButton = thumb_down; if (this.props.loggedIn) { if (this.props.songLiked) likeButton = thumb_up; else likeButton = thumb_up; if (this.props.songDisliked) dislikeButton = thumb_down; else dislikeButton = thumb_down; } return
{ likeButton } { likes }
{ dislikeButton } { dislikes }
; }; like = () => { io.getSocket(socket => { socket.emit('songs.like', this.props.songId, data => {}); }); }; dislike = () => { io.getSocket(socket => { socket.emit('songs.dislike', this.props.songId, data => {}); }); }; unlike = () => { io.getSocket(socket => { socket.emit('songs.unlike', this.props.songId, data => {}); }); }; undislike = () => { io.getSocket(socket => { socket.emit('songs.undislike', this.props.songId, data => {}); }); }; skipStation = () => { io.getSocket(socket => { socket.emit('stations.forceSkip', this.props.station.stationId, data => {}); }); } render() { const { t } = this.props; //TODO Make this not re-render a lot return (

{ this.props.station.displayName }

(this.player = ref) }/> { (this.props.station.paused) ?
Pausedpause
: null }
{ (this.props.songExists) ? ( [
{ this.props.songTitle } { this.props.songArtists.join(", ") } { formatTime(this.props.songTimeElapsed) } / { formatTime(this.props.songDuration) } { (!this.props.simpleSong) ? this.getRatings() : null }
, ]) : (

No song playing

) }
); } }