Songs.vue 4.9 KB

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