Songs.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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="!loadAllSongs"
  18. class="button is-primary"
  19. @click="loadAll()"
  20. >
  21. Load all
  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. loadAllSongs: 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. // eslint-disable-next-line func-names
  140. "modals.editSong": function(val) {
  141. if (!val) this.stopVideo();
  142. }
  143. },
  144. methods: {
  145. edit(song) {
  146. this.editSong({ song, type: "songs" });
  147. this.openModal({ sector: "admin", modal: "editSong" });
  148. },
  149. remove(id) {
  150. this.socket.emit("songs.remove", id, res => {
  151. if (res.status === "success")
  152. new Toast({ content: res.message, timeout: 4000 });
  153. else new Toast({ content: res.message, timeout: 8000 });
  154. });
  155. },
  156. getSet() {
  157. if (this.gettingSet) return;
  158. if (this.position > this.maxPosition) return;
  159. this.gettingSet = true;
  160. this.socket.emit("songs.getSet", this.position, data => {
  161. data.forEach(song => {
  162. this.addSong(song);
  163. });
  164. this.position += 1;
  165. this.gettingSet = false;
  166. if (this.loadAllSongs && this.maxPosition > this.position - 1)
  167. setTimeout(() => {
  168. this.getSet();
  169. }, 500);
  170. });
  171. },
  172. handleScroll() {
  173. if (this.loadAllSongs) return false;
  174. if (window.scrollY + 50 >= window.scrollMaxY) this.getSet();
  175. return this.maxPosition === this.position;
  176. },
  177. loadAll() {
  178. this.loadAllSongs = true;
  179. this.getSet();
  180. },
  181. init() {
  182. if (this.songs.length > 0)
  183. this.position = Math.ceil(this.songs.length / 15) + 1;
  184. this.socket.emit("songs.length", length => {
  185. this.maxPosition = Math.ceil(length / 15);
  186. this.getSet();
  187. });
  188. this.socket.emit("apis.joinAdminRoom", "songs", () => {});
  189. },
  190. ...mapActions("admin/songs", [
  191. "stopVideo",
  192. "editSong",
  193. "addSong",
  194. "removeSong",
  195. "updateSong"
  196. ]),
  197. ...mapActions("modals", ["openModal", "closeModal"])
  198. },
  199. mounted() {
  200. io.getSocket(socket => {
  201. this.socket = socket;
  202. this.socket.on("event:admin.song.added", song => {
  203. this.addSong(song);
  204. });
  205. this.socket.on("event:admin.song.removed", songId => {
  206. this.removeSong(songId);
  207. });
  208. this.socket.on("event:admin.song.updated", updatedSong => {
  209. this.updateSong(updatedSong);
  210. });
  211. if (this.socket.connected) {
  212. this.init();
  213. }
  214. io.onConnect(() => {
  215. this.init();
  216. });
  217. });
  218. if (this.$route.query.songId) {
  219. this.socket.emit("songs.getSong", this.$route.query.songId, res => {
  220. if (res.status === "success") {
  221. this.edit(res.data);
  222. this.closeModal({ sector: "admin", modal: "viewReport" });
  223. } else
  224. new Toast({
  225. content: "Song with that ID not found",
  226. timeout: 3000
  227. });
  228. });
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss" scoped>
  234. @import "styles/global.scss";
  235. .night-mode {
  236. .table {
  237. color: #ddd;
  238. background-color: #222;
  239. thead tr {
  240. background: $night-mode-secondary;
  241. td {
  242. color: #fff;
  243. }
  244. }
  245. tbody tr:hover {
  246. background-color: #111 !important;
  247. }
  248. tbody tr:nth-child(even) {
  249. background-color: #444;
  250. }
  251. strong {
  252. color: #ddd;
  253. }
  254. }
  255. }
  256. body {
  257. font-family: "Roboto", sans-serif;
  258. }
  259. .optionsColumn {
  260. width: 100px;
  261. button {
  262. width: 35px;
  263. }
  264. }
  265. .likesColumn,
  266. .dislikesColumn {
  267. width: 40px;
  268. i {
  269. font-size: 20px;
  270. }
  271. .thumbLike {
  272. color: $green !important;
  273. }
  274. .thumbDislike {
  275. color: $red !important;
  276. }
  277. }
  278. .song-thumbnail {
  279. display: block;
  280. max-width: 50px;
  281. margin: 0 auto;
  282. }
  283. td {
  284. vertical-align: middle;
  285. }
  286. .is-primary:focus {
  287. background-color: $primary-color !important;
  288. }
  289. </style>