Songs.vue 3.9 KB

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