Songs.vue 6.4 KB

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