SearchMusare.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <script>
  2. import Toast from "toasters";
  3. export default {
  4. data() {
  5. return {
  6. musareSearch: {
  7. query: "",
  8. searchedQuery: "",
  9. page: 0,
  10. count: 0,
  11. resultsLeft: 0,
  12. results: []
  13. }
  14. };
  15. },
  16. computed: {
  17. resultsLeftCount() {
  18. return this.musareSearch.count - this.musareSearch.results.length;
  19. },
  20. nextPageResultsCount() {
  21. return Math.min(this.musareSearch.pageSize, this.resultsLeftCount);
  22. }
  23. },
  24. methods: {
  25. searchForMusareSongs(page) {
  26. if (
  27. this.musareSearch.page >= page ||
  28. this.musareSearch.searchedQuery !== this.musareSearch.query
  29. ) {
  30. this.musareSearch.results = [];
  31. this.musareSearch.page = 0;
  32. this.musareSearch.count = 0;
  33. this.musareSearch.resultsLeft = 0;
  34. this.musareSearch.pageSize = 0;
  35. }
  36. this.musareSearch.searchedQuery = this.musareSearch.query;
  37. this.socket.dispatch(
  38. "songs.searchOfficial",
  39. this.musareSearch.query,
  40. page,
  41. res => {
  42. const { data } = res;
  43. const { count, pageSize, songs } = data;
  44. const newSongs = songs.map(song => ({
  45. isAddedToQueue: false,
  46. ...song
  47. }));
  48. if (res.status === "success") {
  49. this.musareSearch.results = [
  50. ...this.musareSearch.results,
  51. ...newSongs
  52. ];
  53. this.musareSearch.page = page;
  54. this.musareSearch.count = count;
  55. this.musareSearch.resultsLeft =
  56. count - this.musareSearch.results.length;
  57. this.musareSearch.pageSize = pageSize;
  58. } else if (res.status === "error") {
  59. this.musareSearch.results = [];
  60. this.musareSearch.page = 0;
  61. this.musareSearch.count = 0;
  62. this.musareSearch.resultsLeft = 0;
  63. this.musareSearch.pageSize = 0;
  64. new Toast(res.message);
  65. }
  66. }
  67. );
  68. },
  69. addMusareSongToPlaylist(id, index) {
  70. this.socket.dispatch(
  71. "playlists.addSongToPlaylist",
  72. false,
  73. id,
  74. this.playlist._id,
  75. res => {
  76. new Toast(res.message);
  77. if (res.status === "success")
  78. this.musareSearch.results[index].isAddedToQueue = true;
  79. }
  80. );
  81. }
  82. }
  83. };
  84. </script>