index.jsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import React, { Component } from "react";
  2. import { NavLink } from "react-router-dom";
  3. import PropTypes from "prop-types";
  4. import { translate, Trans } from "react-i18next";
  5. import Player from "./Player";
  6. import Seekerbar from "./Seekerbar";
  7. import VolumeSlider from "./VolumeSlider";
  8. import Overlays from "./Views/Overlays";
  9. import { changeVolume } from "actions/volume";
  10. import { changeSong, setTimeElapsed, timePaused, receivedRatings, receivedOwnRatings } from "actions/songPlayer";
  11. import { pauseStation, resumeStation } from "actions/station";
  12. import { openOverlay1 } from "actions/stationOverlay";
  13. import { connect } from "react-redux";
  14. import io from "io";
  15. import config from "config";
  16. import {updateTimePaused} from "../../actions/songPlayer";
  17. const formatTime = (duration) => {
  18. let d = moment.duration(duration, "seconds");
  19. if (duration < 0) return "0:00";
  20. return ((d.hours() > 0) ? (d.hours() < 10 ? ("0" + d.hours() + ":") : (d.hours() + ":")) : "") + (d.minutes() + ":") + (d.seconds() < 10 ? ("0" + d.seconds()) : d.seconds());
  21. };
  22. @connect(state => ({
  23. user: {
  24. userId: state.user.get("userId"),
  25. role: state.user.get("role"),
  26. },
  27. loggedIn: state.user.get("loggedIn"),
  28. songId: state.songPlayer.get("songId"),
  29. songTitle: state.songPlayer.get("title"),
  30. songDuration: state.songPlayer.get("duration"),
  31. songTimeElapsed: state.songPlayer.get("timeElapsed"),
  32. songArtists: state.songPlayer.get("artists"),
  33. songLikes: state.songPlayer.get("likes"),
  34. songLiked: state.songPlayer.get("liked"),
  35. songDislikes: state.songPlayer.get("dislikes"),
  36. songDisliked: state.songPlayer.get("disliked"),
  37. simpleSong: state.songPlayer.get("simple"),
  38. songExists: state.songPlayer.get("exists"),
  39. station: {
  40. stationId: state.station.get("id"),
  41. name: state.station.get("name"),
  42. displayName: state.station.get("displayName"),
  43. paused: state.station.get("paused"),
  44. pausedAt: state.station.get("pausedAt"),
  45. ownerId: state.station.get("ownerId"),
  46. },
  47. }))
  48. @translate(["station"], { wait: true })
  49. export default class Station extends Component {
  50. static propTypes = {
  51. t: PropTypes.func,
  52. };
  53. static defaultProps = {
  54. t: () => {},
  55. };
  56. constructor(props) {
  57. super();
  58. io.getSocket(socket => {
  59. socket.emit("stations.join", props.station.name, res => {
  60. if (res.status === 'success') {
  61. if (res.data.currentSong) {
  62. res.data.currentSong.startedAt = res.data.startedAt;
  63. res.data.currentSong.timePaused = res.data.timePaused;
  64. }
  65. this.props.dispatch(changeSong(res.data.currentSong));
  66. this.getOwnRatings();
  67. }
  68. socket.on('event:songs.next', data => {
  69. if (data.currentSong) {
  70. data.currentSong.startedAt = data.startedAt;
  71. data.currentSong.timePaused = data.timePaused;
  72. }
  73. this.props.dispatch(changeSong(data.currentSong));
  74. this.getOwnRatings();
  75. });
  76. socket.on('event:stations.pause', pausedAt => {
  77. this.props.dispatch(pauseStation(pausedAt));
  78. });
  79. socket.on('event:stations.resume', data => {
  80. this.props.dispatch(updateTimePaused(data.timePaused));
  81. this.props.dispatch(resumeStation());
  82. });
  83. socket.on('event:song.like', data => {
  84. console.log("LIKE");
  85. if (this.props.songExists) {
  86. if (data.songId === this.props.songId) {
  87. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  88. }
  89. }
  90. });
  91. socket.on('event:song.dislike', data => {
  92. console.log("DISLIKE");
  93. if (this.props.songExists) {
  94. if (data.songId === this.props.songId) {
  95. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  96. }
  97. }
  98. });
  99. socket.on('event:song.unlike', data => {
  100. console.log("UNLIKE");
  101. if (this.props.songExists) {
  102. if (data.songId === this.props.songId) {
  103. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  104. }
  105. }
  106. });
  107. socket.on('event:song.undislike', data => {
  108. console.log("UNDISLIKE");
  109. if (this.props.songExists) {
  110. if (data.songId === this.props.songId) {
  111. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  112. }
  113. }
  114. });
  115. socket.on('event:song.newRatings', data => {
  116. if (this.props.songExists) {
  117. if (data.songId === this.props.songId) {
  118. this.props.dispatch(receivedOwnRatings(data.liked, data.disliked));
  119. }
  120. }
  121. });
  122. });
  123. });
  124. setInterval(() => {
  125. if (this.props.songExists) {
  126. this.props.dispatch(setTimeElapsed(this.props.station.paused, this.props.station.pausedAt)); // TODO Fix
  127. }
  128. }, 1000);
  129. }
  130. getOwnRatings = () => {
  131. io.getSocket((socket) => {
  132. if (!this.props.songExists) return;
  133. socket.emit('songs.getOwnSongRatings', this.props.songId, (data) => {
  134. if (this.props.songId === data.songId) this.props.dispatch(receivedOwnRatings(data.liked, data.disliked));
  135. });
  136. });
  137. };
  138. isOwner = () => {
  139. if (this.props.loggedIn) {
  140. if (this.props.user.role === "admin") return true;
  141. if (this.props.user.userId === this.props.station.ownerId) return true;
  142. }
  143. return false;
  144. };
  145. addSongTemp = () => {
  146. io.getSocket(socket => {
  147. socket.emit('stations.addToQueue', this.props.station.stationId, '60ItHLz5WEA', data => {
  148. console.log("ATQ Res", data);
  149. });
  150. });
  151. };
  152. resumeStation = () => {
  153. io.getSocket(socket => {
  154. socket.emit('stations.resume', this.props.station.stationId, data => {
  155. });
  156. });
  157. };
  158. pauseStation = () => {
  159. io.getSocket(socket => {
  160. socket.emit('stations.pause', this.props.station.stationId, data => {
  161. });
  162. });
  163. };
  164. getRatings = () => {
  165. const likes = <span>{ this.props.songLikes }</span>;
  166. const dislikes = <span>{ this.props.songDislikes }</span>;
  167. let likeButton = <i className="material-icons disabled">thumb_up</i>;
  168. let dislikeButton = <i className="material-icons disabled">thumb_down</i>;
  169. if (this.props.loggedIn) {
  170. if (this.props.songLiked) likeButton = <i className="material-icons liked" onClick={ this.unlike }>thumb_up</i>;
  171. else likeButton = <i className="material-icons" onClick={ this.like }>thumb_up</i>;
  172. if (this.props.songDisliked) dislikeButton = <i className="material-icons disliked" onClick={ this.undislike }>thumb_down</i>;
  173. else dislikeButton = <i className="material-icons" onClick={ this.dislike }>thumb_down</i>;
  174. }
  175. return <div>
  176. { likeButton }
  177. { likes }
  178. { dislikeButton }
  179. { dislikes }
  180. </div>;
  181. };
  182. like = () => {
  183. io.getSocket(socket => {
  184. socket.emit('songs.like', this.props.songId, data => {});
  185. });
  186. };
  187. dislike = () => {
  188. io.getSocket(socket => {
  189. socket.emit('songs.dislike', this.props.songId, data => {});
  190. });
  191. };
  192. unlike = () => {
  193. io.getSocket(socket => {
  194. socket.emit('songs.unlike', this.props.songId, data => {});
  195. });
  196. };
  197. undislike = () => {
  198. io.getSocket(socket => {
  199. socket.emit('songs.undislike', this.props.songId, data => {});
  200. });
  201. };
  202. skipStation = () => {
  203. io.getSocket(socket => {
  204. socket.emit('stations.forceSkip', this.props.station.stationId, data => {});
  205. });
  206. }
  207. render() {
  208. const { t } = this.props;
  209. //TODO Make this not re-render a lot
  210. return (
  211. <main id="station">
  212. <Overlays t={ this.props.t } />
  213. <aside>
  214. <h2>Sidebar</h2>
  215. <button onClick={ () => { this.props.dispatch(openOverlay1("users")) } }>Users</button>
  216. <button onClick={ () => { this.props.dispatch(openOverlay1("queueList")) } }>Queue</button>
  217. <button onClick={ () => { this.props.dispatch(openOverlay1("playlists")) } }>Playlists</button>
  218. {
  219. (this.isOwner())
  220. ? (this.props.station.paused)
  221. ? <button onClick={ this.resumeStation }>Resume</button>
  222. : <button onClick={ this.pauseStation }>Pause</button>
  223. : null
  224. }
  225. {
  226. (this.isOwner())
  227. ? <button onClick={ this.skipStation }>Skip</button>
  228. : null
  229. }
  230. {
  231. (this.isOwner())
  232. ? <button onClick={ () => { this.props.dispatch(openOverlay1("settings")) } }>Settings</button>
  233. : null
  234. }
  235. <hr/>
  236. </aside>
  237. <h1>{ this.props.station.displayName }</h1>
  238. <hr/>
  239. <div className={(!this.props.songExists) ? "hidden" : ""}>
  240. <Player onRef={ ref => (this.player = ref) }/>
  241. { (this.props.station.paused) ? <div><span>Paused</span><i className="material-icons">pause</i></div> : null }
  242. </div>
  243. { (this.props.songExists) ? (
  244. [
  245. <div key="content">
  246. <h1>Title: { this.props.songTitle }</h1>
  247. <br/>
  248. <span>Artists: { this.props.songArtists.join(", ") }</span>
  249. <br/>
  250. <span key="time">
  251. { formatTime(this.props.songTimeElapsed) } - { formatTime(this.props.songDuration) }
  252. </span>
  253. <div key="seekerbar" className="seekerbar-container" style={{"width": "100%", "background-color": "yellow", "height": "20px", "display": "block"}}>
  254. <Seekerbar/>
  255. </div>
  256. {
  257. (!this.props.simpleSong) ? this.getRatings() : null
  258. }
  259. <VolumeSlider key="volumeSlider"/>
  260. </div>,
  261. ]) : (
  262. <h1>No song playing</h1>
  263. ) }
  264. </main>
  265. );
  266. }
  267. }