Songs.vue 5.0 KB

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