Songs.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.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. },
  121. watch: {
  122. "modals.editSong": val => {
  123. if (!val) this.stopVideo();
  124. }
  125. },
  126. methods: {
  127. edit(song) {
  128. this.editSong({ song, type: "songs" });
  129. this.openModal({ sector: "admin", modal: "editSong" });
  130. },
  131. remove(id) {
  132. this.socket.emit("songs.remove", id, res => {
  133. if (res.status === "success")
  134. Toast.methods.addToast(res.message, 4000);
  135. else Toast.methods.addToast(res.message, 8000);
  136. });
  137. },
  138. getSet() {
  139. this.socket.emit("songs.getSet", this.position, data => {
  140. data.forEach(song => {
  141. this.songs.push(song);
  142. });
  143. this.position += 1;
  144. if (this.maxPosition > this.position - 1) this.getSet();
  145. });
  146. },
  147. init() {
  148. this.songs = [];
  149. this.socket.emit("songs.length", length => {
  150. this.maxPosition = Math.ceil(length / 15);
  151. this.getSet();
  152. });
  153. this.socket.emit("apis.joinAdminRoom", "songs", () => {});
  154. },
  155. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  156. ...mapActions("modals", ["openModal", "closeModal"])
  157. },
  158. mounted() {
  159. io.getSocket(socket => {
  160. this.socket = socket;
  161. this.socket.on("event:admin.song.added", song => {
  162. this.songs.push(song);
  163. });
  164. this.socket.on("event:admin.song.removed", songId => {
  165. this.songs = this.songs.filter(song => {
  166. return song._id !== songId;
  167. });
  168. });
  169. this.socket.on("event:admin.song.updated", updatedSong => {
  170. for (let i = 0; i < this.songs.length; i += 1) {
  171. const song = this.songs[i];
  172. if (song._id === updatedSong._id) {
  173. this.songs.$set(i, updatedSong);
  174. }
  175. }
  176. });
  177. if (this.socket.connected) {
  178. this.init();
  179. }
  180. io.onConnect(() => {
  181. this.init();
  182. });
  183. });
  184. if (this.$route.query.id) {
  185. this.socket.emit("songs.getSong", this.$route.query.id, res => {
  186. if (res.status === "success") {
  187. this.edit(res.data);
  188. this.closeModal({ sector: "admin", modal: "viewReport" });
  189. } else
  190. Toast.methods.addToast("Song with that ID not found", 3000);
  191. });
  192. }
  193. }
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. @import "styles/global.scss";
  198. body {
  199. font-family: "Roboto", sans-serif;
  200. }
  201. .optionsColumn {
  202. width: 100px;
  203. button {
  204. width: 35px;
  205. }
  206. }
  207. .likesColumn,
  208. .dislikesColumn {
  209. width: 40px;
  210. i {
  211. font-size: 20px;
  212. }
  213. .thumbLike {
  214. color: #87d37c !important;
  215. }
  216. .thumbDislike {
  217. color: #ec644b !important;
  218. }
  219. }
  220. .song-thumbnail {
  221. display: block;
  222. max-width: 50px;
  223. margin: 0 auto;
  224. }
  225. td {
  226. vertical-align: middle;
  227. }
  228. .is-primary:focus {
  229. background-color: #029ce3 !important;
  230. }
  231. </style>