index.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. },
  46. }))
  47. @translate(["station"], { wait: true })
  48. export default class Station extends Component {
  49. static propTypes = {
  50. t: PropTypes.func,
  51. };
  52. static defaultProps = {
  53. t: () => {},
  54. };
  55. constructor(props) {
  56. super();
  57. io.getSocket(socket => {
  58. socket.emit("stations.join", props.station.name, res => {
  59. if (res.status === 'success') {
  60. if (res.data.currentSong) {
  61. res.data.currentSong.startedAt = res.data.startedAt;
  62. res.data.currentSong.timePaused = res.data.timePaused;
  63. }
  64. this.props.dispatch(changeSong(res.data.currentSong));
  65. this.getOwnRatings();
  66. }
  67. socket.on('event:songs.next', data => {
  68. if (data.currentSong) {
  69. data.currentSong.startedAt = data.startedAt;
  70. data.currentSong.timePaused = data.timePaused;
  71. }
  72. this.props.dispatch(changeSong(data.currentSong));
  73. this.getOwnRatings();
  74. });
  75. socket.on('event:stations.pause', pausedAt => {
  76. this.props.dispatch(pauseStation(pausedAt));
  77. });
  78. socket.on('event:stations.resume', data => {
  79. this.props.dispatch(updateTimePaused(data.timePaused));
  80. this.props.dispatch(resumeStation());
  81. });
  82. socket.on('event:song.like', data => {
  83. console.log("LIKE");
  84. if (this.props.songExists) {
  85. if (data.songId === this.props.songId) {
  86. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  87. }
  88. }
  89. });
  90. socket.on('event:song.dislike', data => {
  91. console.log("DISLIKE");
  92. if (this.props.songExists) {
  93. if (data.songId === this.props.songId) {
  94. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  95. }
  96. }
  97. });
  98. socket.on('event:song.unlike', data => {
  99. console.log("UNLIKE");
  100. if (this.props.songExists) {
  101. if (data.songId === this.props.songId) {
  102. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  103. }
  104. }
  105. });
  106. socket.on('event:song.undislike', data => {
  107. console.log("UNDISLIKE");
  108. if (this.props.songExists) {
  109. if (data.songId === this.props.songId) {
  110. this.props.dispatch(receivedRatings(data.likes, data.dislikes));
  111. }
  112. }
  113. });
  114. socket.on('event:song.newRatings', data => {
  115. if (this.props.songExists) {
  116. if (data.songId === this.props.songId) {
  117. this.props.dispatch(receivedOwnRatings(data.liked, data.disliked));
  118. }
  119. }
  120. });
  121. });
  122. });
  123. setInterval(() => {
  124. if (this.props.songExists) {
  125. this.props.dispatch(setTimeElapsed(this.props.station.paused, this.props.station.pausedAt)); // TODO Fix
  126. }
  127. }, 1000);
  128. }
  129. getOwnRatings = () => {
  130. io.getSocket((socket) => {
  131. if (!this.props.songExists) return;
  132. socket.emit('songs.getOwnSongRatings', this.props.songId, (data) => {
  133. if (this.props.songId === data.songId) this.props.dispatch(receivedOwnRatings(data.liked, data.disliked));
  134. });
  135. });
  136. };
  137. isOwner = (ownerId) => {
  138. if (this.props.loggedIn) {
  139. if (this.props.user.role === "admin") return true;
  140. if (this.props.user.userId === ownerId) return true;
  141. }
  142. return false;
  143. };
  144. addSongTemp = () => {
  145. io.getSocket(socket => {
  146. socket.emit('stations.addToQueue', this.props.station.stationId, '60ItHLz5WEA', data => {
  147. console.log("ATQ Res", data);
  148. });
  149. });
  150. };
  151. resumeStation = () => {
  152. io.getSocket(socket => {
  153. socket.emit('stations.resume', this.props.station.stationId, data => {
  154. });
  155. });
  156. };
  157. pauseStation = () => {
  158. io.getSocket(socket => {
  159. socket.emit('stations.pause', this.props.station.stationId, data => {
  160. });
  161. });
  162. };
  163. getRatings = () => {
  164. const likes = <span>{ this.props.songLikes }</span>;
  165. const dislikes = <span>{ this.props.songDislikes }</span>;
  166. let likeButton = <i className="material-icons disabled">thumb_up</i>;
  167. let dislikeButton = <i className="material-icons disabled">thumb_down</i>;
  168. if (this.props.loggedIn) {
  169. if (this.props.songLiked) likeButton = <i className="material-icons liked" onClick={ this.unlike }>thumb_up</i>;
  170. else likeButton = <i className="material-icons" onClick={ this.like }>thumb_up</i>;
  171. if (this.props.songDisliked) dislikeButton = <i className="material-icons disliked" onClick={ this.undislike }>thumb_down</i>;
  172. else dislikeButton = <i className="material-icons" onClick={ this.dislike }>thumb_down</i>;
  173. }
  174. return <div>
  175. { likeButton }
  176. { likes }
  177. { dislikeButton }
  178. { dislikes }
  179. </div>;
  180. };
  181. like = () => {
  182. io.getSocket(socket => {
  183. socket.emit('songs.like', this.props.songId, data => {});
  184. });
  185. };
  186. dislike = () => {
  187. io.getSocket(socket => {
  188. socket.emit('songs.dislike', this.props.songId, data => {});
  189. });
  190. };
  191. unlike = () => {
  192. io.getSocket(socket => {
  193. socket.emit('songs.unlike', this.props.songId, data => {});
  194. });
  195. };
  196. undislike = () => {
  197. io.getSocket(socket => {
  198. socket.emit('songs.undislike', this.props.songId, data => {});
  199. });
  200. };
  201. skip = () => {
  202. io.getSocket(socket => {
  203. socket.emit('stations.forceSkip', this.props.station.stationId, data => {});
  204. });
  205. }
  206. render() {
  207. const { t } = this.props;
  208. //TODO Make this not re-render a lot
  209. return (
  210. <main id="station">
  211. <Overlays t={ this.props.t } />
  212. <button onClick={ () => { this.props.dispatch(openOverlay1("settings")) } }>Open settings</button>
  213. <button onClick={ () => { this.props.dispatch(openOverlay1("playlists")) } }>Open playlists</button>
  214. <h1>{ this.props.station.displayName }</h1>
  215. { (this.props.station.paused) ? <button onClick={ this.resumeStation }>Resume</button> : <button onClick={ this.pauseStation }>Pause</button>}
  216. <button onClick={ this.addSongTemp }>Add song to queue TEMP</button>
  217. <button onClick={ this.skip }>Skip</button>
  218. <hr/>
  219. <div className={(!this.props.songExists) ? "hidden" : ""}>
  220. <Player onRef={ ref => (this.player = ref) }/>
  221. { (this.props.station.paused) ? <div><span>Paused</span><i className="material-icons">pause</i></div> : null }
  222. </div>
  223. { (this.props.songExists) ? (
  224. [
  225. <div key="content">
  226. <h1>Title: { this.props.songTitle }</h1>
  227. <br/>
  228. <span>Artists: { this.props.songArtists.join(", ") }</span>
  229. <br/>
  230. <span key="time">
  231. { formatTime(this.props.songTimeElapsed) } - { formatTime(this.props.songDuration) }
  232. </span>
  233. <div key="seekerbar" className="seekerbar-container" style={{"width": "100%", "background-color": "yellow", "height": "20px", "display": "block"}}>
  234. <Seekerbar/>
  235. </div>
  236. {
  237. (!this.props.simpleSong) ? this.getRatings() : null
  238. }
  239. <VolumeSlider key="volumeSlider"/>
  240. </div>,
  241. ]) : (
  242. <h1>No song playing</h1>
  243. ) }
  244. </main>
  245. );
  246. }
  247. }