QueueSongs.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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="!getSetsAutomatically"
  18. class="button is-primary"
  19. @click="loadSongsAutomatically()"
  20. >
  21. Load songs automatically
  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 Toast from "toasters";
  100. import EditSong from "../Modals/EditSong.vue";
  101. import UserIdToUsername from "../UserIdToUsername.vue";
  102. import io from "../../io";
  103. export default {
  104. components: { EditSong, UserIdToUsername },
  105. data() {
  106. return {
  107. position: 1,
  108. maxPosition: 1,
  109. searchQuery: "",
  110. songs: [],
  111. gettingSet: false,
  112. getSetsAutomatically: false
  113. };
  114. },
  115. computed: {
  116. filteredSongs() {
  117. return this.songs.filter(
  118. song =>
  119. JSON.stringify(Object.values(song)).indexOf(
  120. this.searchQuery
  121. ) !== -1
  122. );
  123. },
  124. ...mapState("modals", {
  125. modals: state => state.modals.admin
  126. })
  127. },
  128. // watch: {
  129. // "modals.editSong": function(value) {
  130. // console.log(value);
  131. // if (value === false) this.stopVideo();
  132. // }
  133. // },
  134. methods: {
  135. edit(song, index) {
  136. const newSong = {};
  137. Object.keys(song).forEach(n => {
  138. newSong[n] = song[n];
  139. });
  140. this.editSong({ index, song: newSong, type: "queueSongs" });
  141. this.openModal({ sector: "admin", modal: "editSong" });
  142. },
  143. add(song) {
  144. this.socket.emit("songs.add", song, res => {
  145. if (res.status === "success")
  146. new Toast({ content: res.message, timeout: 2000 });
  147. else new Toast({ content: res.message, timeout: 4000 });
  148. });
  149. },
  150. remove(id) {
  151. this.socket.emit("queueSongs.remove", id, 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. getSet() {
  158. if (this.gettingSet) return;
  159. if (this.position > this.maxPosition) return;
  160. this.gettingSet = true;
  161. this.socket.emit("queueSongs.getSet", this.position, data => {
  162. data.forEach(song => {
  163. this.songs.push(song);
  164. });
  165. this.position += 1;
  166. this.gettingSet = false;
  167. if (
  168. this.getSetsAutomatically &&
  169. this.maxPosition > this.position - 1
  170. )
  171. setTimeout(() => {
  172. this.getSet();
  173. }, 500);
  174. });
  175. },
  176. handleScroll() {
  177. if (this.getSetsAutomatically) return false;
  178. if (window.scrollY + 50 >= window.scrollMaxY) this.getSet();
  179. return this.maxPosition === this.position;
  180. },
  181. loadSongsAutomatically() {
  182. this.getSetsAutomatically = true;
  183. this.getSet();
  184. },
  185. init() {
  186. if (this.songs.length > 0)
  187. this.position = Math.ceil(this.songs.length / 15) + 1;
  188. this.socket.emit("queueSongs.length", length => {
  189. this.maxPosition = Math.ceil(length / 15);
  190. this.getSet();
  191. });
  192. this.socket.emit("apis.joinAdminRoom", "queue", () => {});
  193. },
  194. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  195. ...mapActions("modals", ["openModal"])
  196. },
  197. mounted() {
  198. io.getSocket(socket => {
  199. this.socket = socket;
  200. this.socket.on("event:admin.queueSong.added", queueSong => {
  201. this.songs.push(queueSong);
  202. });
  203. this.socket.on("event:admin.queueSong.removed", songId => {
  204. this.songs = this.songs.filter(song => {
  205. return song._id !== songId;
  206. });
  207. });
  208. this.socket.on("event:admin.queueSong.updated", updatedSong => {
  209. for (let i = 0; i < this.songs.length; i += 1) {
  210. const song = this.songs[i];
  211. if (song._id === updatedSong._id) {
  212. this.songs.$set(i, updatedSong);
  213. }
  214. }
  215. });
  216. if (this.socket.connected) {
  217. this.init();
  218. }
  219. io.onConnect(() => {
  220. this.init();
  221. });
  222. });
  223. }
  224. };
  225. </script>
  226. <style lang="scss" scoped>
  227. @import "styles/global.scss";
  228. .optionsColumn {
  229. width: 140px;
  230. button {
  231. width: 35px;
  232. }
  233. }
  234. .song-thumbnail {
  235. display: block;
  236. max-width: 50px;
  237. margin: 0 auto;
  238. }
  239. td {
  240. vertical-align: middle;
  241. }
  242. .is-primary:focus {
  243. background-color: $primary-color !important;
  244. }
  245. </style>