QueueSongs.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <div>
  3. <metadata title="Admin | Queue songs" />
  4. <div class="container" v-scroll="handleScroll">
  5. <p>
  6. <span>Sets loaded: {{ position - 1 }} / {{ maxPosition }}</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. ...mapState("modals", {
  126. modals: state => state.modals.admin
  127. })
  128. },
  129. watch: {
  130. // eslint-disable-next-line func-names
  131. "modals.editSong": function(value) {
  132. if (value === false) this.stopVideo();
  133. }
  134. },
  135. methods: {
  136. edit(song, index) {
  137. const newSong = {};
  138. Object.keys(song).forEach(n => {
  139. newSong[n] = song[n];
  140. });
  141. this.editSong({ index, song: newSong, type: "queueSongs" });
  142. this.openModal({ sector: "admin", modal: "editSong" });
  143. },
  144. add(song) {
  145. this.socket.emit("songs.add", song, res => {
  146. if (res.status === "success")
  147. new Toast({ content: res.message, timeout: 2000 });
  148. else new Toast({ content: res.message, timeout: 4000 });
  149. });
  150. },
  151. remove(id) {
  152. this.socket.emit("queueSongs.remove", id, res => {
  153. if (res.status === "success")
  154. new Toast({ content: res.message, timeout: 2000 });
  155. else new Toast({ content: res.message, timeout: 4000 });
  156. });
  157. },
  158. getSet() {
  159. if (this.gettingSet) return;
  160. if (this.position > this.maxPosition) return;
  161. this.gettingSet = true;
  162. this.socket.emit("queueSongs.getSet", this.position, data => {
  163. data.forEach(song => {
  164. this.songs.push(song);
  165. });
  166. this.position += 1;
  167. this.gettingSet = false;
  168. if (this.loadAllSongs && this.maxPosition > this.position - 1)
  169. setTimeout(() => {
  170. this.getSet();
  171. }, 500);
  172. });
  173. },
  174. handleScroll() {
  175. if (this.loadAllSongs) return false;
  176. if (window.scrollY + 50 >= window.scrollMaxY) this.getSet();
  177. return this.maxPosition === this.position;
  178. },
  179. loadAll() {
  180. this.loadAllSongs = true;
  181. this.getSet();
  182. },
  183. init() {
  184. if (this.songs.length > 0)
  185. this.position = Math.ceil(this.songs.length / 15) + 1;
  186. this.socket.emit("queueSongs.length", length => {
  187. this.maxPosition = Math.ceil(length / 15);
  188. this.getSet();
  189. });
  190. this.socket.emit("apis.joinAdminRoom", "queue", () => {});
  191. },
  192. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  193. ...mapActions("modals", ["openModal"])
  194. },
  195. mounted() {
  196. io.getSocket(socket => {
  197. this.socket = socket;
  198. this.socket.on("event:admin.queueSong.added", queueSong => {
  199. this.songs.push(queueSong);
  200. });
  201. this.socket.on("event:admin.queueSong.removed", songId => {
  202. this.songs = this.songs.filter(song => {
  203. return song._id !== songId;
  204. });
  205. });
  206. this.socket.on("event:admin.queueSong.updated", updatedSong => {
  207. for (let i = 0; i < this.songs.length; i += 1) {
  208. const song = this.songs[i];
  209. if (song._id === updatedSong._id) {
  210. Vue.set(this.songs, i, updatedSong);
  211. }
  212. }
  213. });
  214. if (this.socket.connected) {
  215. this.init();
  216. }
  217. io.onConnect(() => {
  218. this.init();
  219. });
  220. });
  221. }
  222. };
  223. </script>
  224. <style lang="scss" scoped>
  225. @import "styles/global.scss";
  226. .night-mode {
  227. .table {
  228. color: #ddd;
  229. background-color: #222;
  230. thead tr {
  231. background: $night-mode-secondary;
  232. td {
  233. color: #fff;
  234. }
  235. }
  236. tbody tr:hover {
  237. background-color: #111 !important;
  238. }
  239. tbody tr:nth-child(even) {
  240. background-color: #444;
  241. }
  242. strong {
  243. color: #ddd;
  244. }
  245. }
  246. }
  247. .optionsColumn {
  248. width: 140px;
  249. button {
  250. width: 35px;
  251. }
  252. }
  253. .song-thumbnail {
  254. display: block;
  255. max-width: 50px;
  256. margin: 0 auto;
  257. }
  258. td {
  259. vertical-align: middle;
  260. }
  261. .is-primary:focus {
  262. background-color: $primary-color !important;
  263. }
  264. </style>