Songs.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div>
  3. <div class="container">
  4. <input type="text" class="input" v-model="searchQuery" placeholder="Search for Songs" />
  5. <br />
  6. <br />
  7. <table class="table is-striped">
  8. <thead>
  9. <tr>
  10. <td>Thumbnail</td>
  11. <td>Title</td>
  12. <td>YouTube ID</td>
  13. <td>Artists</td>
  14. <td>Genres</td>
  15. <td>Requested By</td>
  16. <td>Options</td>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr v-for="(song, index) in filteredSongs" :key="index">
  21. <td>
  22. <img
  23. class="song-thumbnail"
  24. :src="song.thumbnail"
  25. onerror="this.src='/assets/notes-transparent.png'"
  26. />
  27. </td>
  28. <td>
  29. <strong>{{ song.title }}</strong>
  30. </td>
  31. <td>{{ song.songId }}</td>
  32. <td>{{ song.artists.join(', ') }}</td>
  33. <td>{{ song.genres.join(', ') }}</td>
  34. <td>{{ song.requestedBy }}</td>
  35. <td>
  36. <button class="button is-primary" v-on:click="edit(song, index)">Edit</button>
  37. <button class="button is-danger" v-on:click="remove(song._id, index)">Remove</button>
  38. </td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. </div>
  43. <edit-song v-if="modals.editSong"></edit-song>
  44. </div>
  45. </template>
  46. <script>
  47. import { mapState, mapActions } from "vuex";
  48. import { Toast } from "vue-roaster";
  49. import EditSong from "../Modals/EditSong.vue";
  50. import io from "../../io";
  51. export default {
  52. components: { EditSong },
  53. data() {
  54. return {
  55. position: 1,
  56. maxPosition: 1,
  57. songs: [],
  58. searchQuery: "",
  59. editing: {
  60. index: 0,
  61. song: {}
  62. }
  63. };
  64. },
  65. computed: {
  66. filteredSongs: function() {
  67. return this.songs;
  68. // return this.songs.filter(song => song.indexOf(song.searchQuery) !== -1);
  69. },
  70. ...mapState("modals", {
  71. modals: state => state.modals.admin
  72. })
  73. },
  74. watch: {
  75. "modals.editSong": function(value) {
  76. if (!value) this.stopVideo();
  77. }
  78. },
  79. methods: {
  80. edit: function(song, index) {
  81. this.editSong({ song, index, type: "songs" });
  82. },
  83. remove: function(id) {
  84. this.socket.emit("songs.remove", id, res => {
  85. if (res.status == "success") Toast.methods.addToast(res.message, 4000);
  86. else Toast.methods.addToast(res.message, 8000);
  87. });
  88. },
  89. getSet: function() {
  90. let _this = this;
  91. _this.socket.emit("songs.getSet", _this.position, data => {
  92. data.forEach(song => {
  93. _this.songs.push(song);
  94. });
  95. _this.position = _this.position + 1;
  96. if (_this.maxPosition > _this.position - 1) _this.getSet();
  97. });
  98. },
  99. init: function() {
  100. let _this = this;
  101. _this.songs = [];
  102. _this.socket.emit("songs.length", length => {
  103. _this.maxPosition = Math.ceil(length / 15);
  104. _this.getSet();
  105. });
  106. _this.socket.emit("apis.joinAdminRoom", "songs", () => {});
  107. },
  108. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  109. ...mapActions("modals", ["toggleModal"])
  110. },
  111. mounted: function() {
  112. let _this = this;
  113. io.getSocket(socket => {
  114. _this.socket = socket;
  115. if (_this.socket.connected) {
  116. _this.init();
  117. _this.socket.on("event:admin.song.added", song => {
  118. _this.songs.push(song);
  119. });
  120. _this.socket.on("event:admin.song.removed", songId => {
  121. _this.songs = _this.songs.filter(function(song) {
  122. return song._id !== songId;
  123. });
  124. });
  125. _this.socket.on("event:admin.song.updated", updatedSong => {
  126. for (let i = 0; i < _this.songs.length; i++) {
  127. let song = _this.songs[i];
  128. if (song._id === updatedSong._id) {
  129. _this.songs.$set(i, updatedSong);
  130. }
  131. }
  132. });
  133. }
  134. io.onConnect(() => {
  135. _this.init();
  136. });
  137. });
  138. }
  139. };
  140. </script>
  141. <style lang='scss' scoped>
  142. body {
  143. font-family: "Roboto", sans-serif;
  144. }
  145. .song-thumbnail {
  146. display: block;
  147. max-width: 50px;
  148. margin: 0 auto;
  149. }
  150. td {
  151. vertical-align: middle;
  152. }
  153. .is-primary:focus {
  154. background-color: #029ce3 !important;
  155. }
  156. </style>