Songs.vue 5.9 KB

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