Songs.vue 5.0 KB

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