QueueSongs.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <div v-scroll="handleScroll">
  3. <metadata title="Admin | Queue songs" />
  4. <div class="container">
  5. <p>
  6. <span>Sets loaded: {{ setsLoaded }} / {{ maxSets }}</span>
  7. <br />
  8. <span>Loaded songs: {{ this.songs.length }}</span>
  9. </p>
  10. <input
  11. v-model="searchQuery"
  12. type="text"
  13. class="input"
  14. placeholder="Search for Songs"
  15. />
  16. <button
  17. v-if="!loadAllSongs"
  18. class="button is-primary"
  19. @click="loadAll()"
  20. >
  21. Load all
  22. </button>
  23. <br />
  24. <br />
  25. <table class="table is-striped">
  26. <thead>
  27. <tr>
  28. <td>Thumbnail</td>
  29. <td>Title</td>
  30. <td>Artists</td>
  31. <td>Genres</td>
  32. <td>ID / YouTube ID</td>
  33. <td>Requested By</td>
  34. <td>Options</td>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <tr v-for="(song, index) in filteredSongs" :key="index">
  39. <td>
  40. <img
  41. class="song-thumbnail"
  42. :src="song.thumbnail"
  43. onerror="this.src='/assets/notes-transparent.png'"
  44. />
  45. </td>
  46. <td>
  47. <strong>{{ song.title }}</strong>
  48. </td>
  49. <td>{{ song.artists.join(", ") }}</td>
  50. <td>{{ song.genres.join(", ") }}</td>
  51. <td>
  52. {{ song._id }}
  53. <br />
  54. <a
  55. :href="
  56. 'https://www.youtube.com/watch?v=' +
  57. `${song.songId}`
  58. "
  59. target="_blank"
  60. >
  61. {{ song.songId }}</a
  62. >
  63. </td>
  64. <td>
  65. <user-id-to-username
  66. :userId="song.requestedBy"
  67. :link="true"
  68. />
  69. </td>
  70. <td class="optionsColumn">
  71. <button
  72. class="button is-primary"
  73. @click="edit(song, index)"
  74. >
  75. <i class="material-icons">edit</i>
  76. </button>
  77. <button
  78. class="button is-success"
  79. @click="add(song)"
  80. >
  81. <i class="material-icons">add</i>
  82. </button>
  83. <button
  84. class="button is-danger"
  85. @click="remove(song._id, index)"
  86. >
  87. <i class="material-icons">cancel</i>
  88. </button>
  89. </td>
  90. </tr>
  91. </tbody>
  92. </table>
  93. </div>
  94. <edit-song v-if="modals.editSong" />
  95. </div>
  96. </template>
  97. <script>
  98. import { mapState, mapActions } from "vuex";
  99. import Vue from "vue";
  100. import Toast from "toasters";
  101. import EditSong from "../Modals/EditSong.vue";
  102. import UserIdToUsername from "../UserIdToUsername.vue";
  103. import io from "../../io";
  104. export default {
  105. components: { EditSong, UserIdToUsername },
  106. data() {
  107. return {
  108. position: 1,
  109. maxPosition: 1,
  110. searchQuery: "",
  111. songs: [],
  112. gettingSet: false,
  113. loadAllSongs: false
  114. };
  115. },
  116. computed: {
  117. filteredSongs() {
  118. return this.songs.filter(
  119. song =>
  120. JSON.stringify(Object.values(song)).indexOf(
  121. this.searchQuery
  122. ) !== -1
  123. );
  124. },
  125. setsLoaded() {
  126. return this.position - 1;
  127. },
  128. maxSets() {
  129. return this.maxPosition - 1;
  130. },
  131. ...mapState("modals", {
  132. modals: state => state.modals.admin
  133. })
  134. },
  135. watch: {
  136. // eslint-disable-next-line func-names
  137. "modals.editSong": function(value) {
  138. if (value === false) this.stopVideo();
  139. }
  140. },
  141. methods: {
  142. edit(song, index) {
  143. const newSong = {};
  144. Object.keys(song).forEach(n => {
  145. newSong[n] = song[n];
  146. });
  147. this.editSong({ index, song: newSong, type: "queueSongs" });
  148. this.openModal({ sector: "admin", modal: "editSong" });
  149. },
  150. add(song) {
  151. this.socket.emit("songs.add", song, res => {
  152. if (res.status === "success")
  153. new Toast({ content: res.message, timeout: 2000 });
  154. else new Toast({ content: res.message, timeout: 4000 });
  155. });
  156. },
  157. remove(id) {
  158. this.socket.emit("queueSongs.remove", id, res => {
  159. if (res.status === "success")
  160. new Toast({ content: res.message, timeout: 2000 });
  161. else new Toast({ content: res.message, timeout: 4000 });
  162. });
  163. },
  164. getSet() {
  165. if (this.gettingSet) return;
  166. if (this.position >= this.maxPosition) return;
  167. this.gettingSet = true;
  168. this.socket.emit("queueSongs.getSet", this.position, data => {
  169. data.forEach(song => {
  170. this.songs.push(song);
  171. });
  172. this.position += 1;
  173. this.gettingSet = false;
  174. if (this.loadAllSongs && this.maxPosition > this.position - 1)
  175. setTimeout(() => {
  176. this.getSet();
  177. }, 500);
  178. });
  179. },
  180. handleScroll() {
  181. const scrollPosition = document.body.clientHeight + window.scrollY;
  182. const bottomPosition = document.body.scrollHeight;
  183. if (this.loadAllSongs) return false;
  184. if (scrollPosition + 50 >= bottomPosition) this.getSet();
  185. return this.maxPosition === this.position;
  186. },
  187. loadAll() {
  188. this.loadAllSongs = true;
  189. this.getSet();
  190. },
  191. init() {
  192. if (this.songs.length > 0)
  193. this.position = Math.ceil(this.songs.length / 15) + 1;
  194. this.socket.emit("queueSongs.length", length => {
  195. this.maxPosition = Math.ceil(length / 15) + 1;
  196. this.getSet();
  197. });
  198. this.socket.emit("apis.joinAdminRoom", "queue", () => {});
  199. },
  200. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  201. ...mapActions("modals", ["openModal"])
  202. },
  203. mounted() {
  204. io.getSocket(socket => {
  205. this.socket = socket;
  206. this.socket.on("event:admin.queueSong.added", queueSong => {
  207. this.songs.push(queueSong);
  208. });
  209. this.socket.on("event:admin.queueSong.removed", songId => {
  210. this.songs = this.songs.filter(song => {
  211. return song._id !== songId;
  212. });
  213. });
  214. this.socket.on("event:admin.queueSong.updated", updatedSong => {
  215. for (let i = 0; i < this.songs.length; i += 1) {
  216. const song = this.songs[i];
  217. if (song._id === updatedSong._id) {
  218. Vue.set(this.songs, i, updatedSong);
  219. }
  220. }
  221. });
  222. if (this.socket.connected) {
  223. this.init();
  224. }
  225. io.onConnect(() => {
  226. this.init();
  227. });
  228. });
  229. }
  230. };
  231. </script>
  232. <style lang="scss" scoped>
  233. @import "styles/global.scss";
  234. .night-mode {
  235. .table {
  236. color: #ddd;
  237. background-color: #222;
  238. thead tr {
  239. background: $night-mode-secondary;
  240. td {
  241. color: #fff;
  242. }
  243. }
  244. tbody tr:hover {
  245. background-color: #111 !important;
  246. }
  247. tbody tr:nth-child(even) {
  248. background-color: #444;
  249. }
  250. strong {
  251. color: #ddd;
  252. }
  253. }
  254. }
  255. .optionsColumn {
  256. width: 140px;
  257. button {
  258. width: 35px;
  259. }
  260. }
  261. .song-thumbnail {
  262. display: block;
  263. max-width: 50px;
  264. margin: 0 auto;
  265. }
  266. td {
  267. vertical-align: middle;
  268. }
  269. .is-primary:focus {
  270. background-color: $primary-color !important;
  271. }
  272. </style>