Songs.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. :user-id="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 ScrollAndFetchHandler from "../../../mixins/ScrollAndFetchHandler.vue";
  107. import io from "../../../io";
  108. export default {
  109. mixins: [ScrollAndFetchHandler],
  110. components: { EditSong, UserIdToUsername },
  111. data() {
  112. return {
  113. searchQuery: "",
  114. editing: {
  115. index: 0,
  116. song: {}
  117. }
  118. };
  119. },
  120. computed: {
  121. filteredSongs() {
  122. return this.songs.filter(
  123. song =>
  124. JSON.stringify(Object.values(song)).indexOf(
  125. this.searchQuery
  126. ) !== -1
  127. );
  128. },
  129. ...mapState("modals", {
  130. modals: state => state.modals.admin
  131. }),
  132. ...mapState("admin/songs", {
  133. songs: state => state.songs
  134. })
  135. },
  136. watch: {
  137. // eslint-disable-next-line func-names
  138. "modals.editSong": function(val) {
  139. if (!val) this.stopVideo();
  140. }
  141. },
  142. methods: {
  143. edit(song) {
  144. this.editSong({ song, type: "songs" });
  145. this.openModal({ sector: "admin", modal: "editSong" });
  146. },
  147. remove(id) {
  148. this.socket.emit("songs.remove", id, res => {
  149. if (res.status === "success")
  150. new Toast({ content: res.message, timeout: 4000 });
  151. else new Toast({ content: res.message, timeout: 8000 });
  152. });
  153. },
  154. getSet() {
  155. if (this.gettingSet) return;
  156. if (this.position >= this.maxPosition) return;
  157. this.gettingSet = true;
  158. this.socket.emit("songs.getSet", this.position, data => {
  159. data.forEach(song => {
  160. this.addSong(song);
  161. });
  162. this.position += 1;
  163. this.gettingSet = false;
  164. });
  165. },
  166. init() {
  167. if (this.songs.length > 0)
  168. this.position = Math.ceil(this.songs.length / 15) + 1;
  169. this.socket.emit("songs.length", length => {
  170. this.maxPosition = Math.ceil(length / 15) + 1;
  171. this.getSet();
  172. setTimeout(() => {
  173. if (
  174. !this.loadAllSongs &&
  175. this.maxPosition > this.position - 1
  176. )
  177. this.getSet();
  178. }, 1000);
  179. });
  180. this.socket.emit("apis.joinAdminRoom", "songs", () => {});
  181. },
  182. ...mapActions("admin/songs", [
  183. "stopVideo",
  184. "editSong",
  185. "addSong",
  186. "removeSong",
  187. "updateSong"
  188. ]),
  189. ...mapActions("modals", ["openModal", "closeModal"])
  190. },
  191. mounted() {
  192. io.getSocket(socket => {
  193. this.socket = socket;
  194. this.socket.on("event:admin.song.added", song => {
  195. this.addSong(song);
  196. });
  197. this.socket.on("event:admin.song.removed", songId => {
  198. this.removeSong(songId);
  199. });
  200. this.socket.on("event:admin.song.updated", updatedSong => {
  201. this.updateSong(updatedSong);
  202. });
  203. if (this.socket.connected) this.init();
  204. io.onConnect(() => {
  205. this.init();
  206. });
  207. });
  208. if (this.$route.query.songId) {
  209. this.socket.emit("songs.getSong", this.$route.query.songId, res => {
  210. if (res.status === "success") {
  211. this.edit(res.data);
  212. this.closeModal({
  213. sector: "admin",
  214. modal: "viewReport"
  215. });
  216. } else
  217. new Toast({
  218. content: "Song with that ID not found",
  219. timeout: 3000
  220. });
  221. });
  222. }
  223. }
  224. };
  225. </script>
  226. <style lang="scss" scoped>
  227. @import "../../../styles/global.scss";
  228. .night-mode {
  229. .table {
  230. color: #ddd;
  231. background-color: #222;
  232. thead tr {
  233. background: $night-mode-secondary;
  234. td {
  235. color: #fff;
  236. }
  237. }
  238. tbody tr:hover {
  239. background-color: #111 !important;
  240. }
  241. tbody tr:nth-child(even) {
  242. background-color: #444;
  243. }
  244. strong {
  245. color: #ddd;
  246. }
  247. }
  248. }
  249. body {
  250. font-family: "Hind", sans-serif;
  251. }
  252. .optionsColumn {
  253. width: 100px;
  254. button {
  255. width: 35px;
  256. }
  257. }
  258. .likesColumn,
  259. .dislikesColumn {
  260. width: 40px;
  261. i {
  262. font-size: 20px;
  263. }
  264. .thumbLike {
  265. color: $green !important;
  266. }
  267. .thumbDislike {
  268. color: $red !important;
  269. }
  270. }
  271. .song-thumbnail {
  272. display: block;
  273. max-width: 50px;
  274. margin: 0 auto;
  275. }
  276. td {
  277. vertical-align: middle;
  278. }
  279. .is-primary:focus {
  280. background-color: $primary-color !important;
  281. }
  282. </style>