Songs.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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>
  40. <user-id-to-username
  41. :userId="song.requestedBy"
  42. :link="true"
  43. />
  44. </td>
  45. <td>
  46. <button
  47. class="button is-primary"
  48. @click="edit(song)"
  49. >
  50. Edit
  51. </button>
  52. <button
  53. class="button is-danger"
  54. @click="remove(song._id, index)"
  55. >
  56. Remove
  57. </button>
  58. </td>
  59. </tr>
  60. </tbody>
  61. </table>
  62. </div>
  63. <edit-song v-if="modals.editSong" />
  64. </div>
  65. </template>
  66. <script>
  67. import { mapState, mapActions } from "vuex";
  68. import { Toast } from "vue-roaster";
  69. import EditSong from "../Modals/EditSong.vue";
  70. import UserIdToUsername from "../UserIdToUsername.vue";
  71. import io from "../../io";
  72. export default {
  73. components: { EditSong, UserIdToUsername },
  74. data() {
  75. return {
  76. position: 1,
  77. maxPosition: 1,
  78. songs: [],
  79. searchQuery: "",
  80. editing: {
  81. index: 0,
  82. song: {}
  83. }
  84. };
  85. },
  86. computed: {
  87. filteredSongs: function() {
  88. return this.songs;
  89. // return this.songs.filter(song => song.indexOf(song.searchQuery) !== -1);
  90. },
  91. ...mapState("modals", {
  92. modals: state => state.modals.admin
  93. })
  94. },
  95. watch: {
  96. "modals.editSong": function(value) {
  97. if (!value) this.stopVideo();
  98. }
  99. },
  100. methods: {
  101. edit: function(song) {
  102. this.editSong({ song, type: "songs" });
  103. this.openModal({ sector: "admin", modal: "editSong" });
  104. },
  105. remove: function(id) {
  106. this.socket.emit("songs.remove", id, res => {
  107. if (res.status == "success")
  108. Toast.methods.addToast(res.message, 4000);
  109. else Toast.methods.addToast(res.message, 8000);
  110. });
  111. },
  112. getSet: function() {
  113. let _this = this;
  114. _this.socket.emit("songs.getSet", _this.position, data => {
  115. data.forEach(song => {
  116. _this.songs.push(song);
  117. });
  118. _this.position = _this.position + 1;
  119. if (_this.maxPosition > _this.position - 1) _this.getSet();
  120. });
  121. },
  122. init: function() {
  123. let _this = this;
  124. _this.songs = [];
  125. _this.socket.emit("songs.length", length => {
  126. _this.maxPosition = Math.ceil(length / 15);
  127. _this.getSet();
  128. });
  129. _this.socket.emit("apis.joinAdminRoom", "songs", () => {});
  130. },
  131. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  132. ...mapActions("modals", ["openModal", "closeModal"])
  133. },
  134. mounted: function() {
  135. let _this = this;
  136. io.getSocket(socket => {
  137. _this.socket = socket;
  138. if (_this.socket.connected) {
  139. _this.init();
  140. _this.socket.on("event:admin.song.added", song => {
  141. _this.songs.push(song);
  142. });
  143. _this.socket.on("event:admin.song.removed", songId => {
  144. _this.songs = _this.songs.filter(function(song) {
  145. return song._id !== songId;
  146. });
  147. });
  148. _this.socket.on("event:admin.song.updated", updatedSong => {
  149. for (let i = 0; i < _this.songs.length; i++) {
  150. let song = _this.songs[i];
  151. if (song._id === updatedSong._id) {
  152. _this.songs.$set(i, updatedSong);
  153. }
  154. }
  155. });
  156. }
  157. io.onConnect(() => {
  158. _this.init();
  159. });
  160. });
  161. if (this.$route.query.id) {
  162. this.socket.emit("songs.getSong", this.$route.query.id, res => {
  163. if (res.status === "success") {
  164. this.edit(res.data);
  165. this.closeModal({ sector: "admin", modal: "viewReport" });
  166. } else
  167. Toast.methods.addToast("Song with that ID not found", 3000);
  168. });
  169. }
  170. }
  171. };
  172. </script>
  173. <style lang="scss" scoped>
  174. body {
  175. font-family: "Roboto", sans-serif;
  176. }
  177. .song-thumbnail {
  178. display: block;
  179. max-width: 50px;
  180. margin: 0 auto;
  181. }
  182. td {
  183. vertical-align: middle;
  184. }
  185. .is-primary:focus {
  186. background-color: #029ce3 !important;
  187. }
  188. </style>