Songs.vue 4.7 KB

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