editPlaylist.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint no-param-reassign: 0 */
  2. export default {
  3. namespaced: true,
  4. state: {
  5. tab: "settings",
  6. playlist: { songs: [] }
  7. },
  8. getters: {},
  9. actions: {
  10. showTab: ({ commit }, tab) => commit("showTab", tab),
  11. setPlaylist: ({ commit }, playlist) => commit("setPlaylist", playlist),
  12. clearPlaylist: ({ commit }) => commit("clearPlaylist"),
  13. addSong: ({ commit }, song) => commit("addSong", song),
  14. removeSong: ({ commit }, youtubeId) => commit("removeSong", youtubeId),
  15. updatePlaylistSongs: ({ commit }, playlistSongs) =>
  16. commit("updatePlaylistSongs", playlistSongs),
  17. repositionedSong: ({ commit }, song) => commit("repositionedSong", song)
  18. },
  19. mutations: {
  20. showTab(state, tab) {
  21. state.tab = tab;
  22. },
  23. setPlaylist(state, playlist) {
  24. state.playlist = { ...playlist };
  25. state.playlist.songs.sort((a, b) => a.position - b.position);
  26. },
  27. clearPlaylist(state) {
  28. state.playlist = { songs: [] };
  29. },
  30. addSong(state, song) {
  31. state.playlist.songs.push(song);
  32. },
  33. removeSong(state, youtubeId) {
  34. state.playlist.songs = state.playlist.songs.filter(
  35. song => song.youtubeId !== youtubeId
  36. );
  37. },
  38. updatePlaylistSongs(state, playlistSongs) {
  39. state.playlist.songs = playlistSongs;
  40. },
  41. repositionedSong(state, song) {
  42. if (
  43. state.playlist.songs[song.newIndex] &&
  44. state.playlist.songs[song.newIndex].youtubeId === song.youtubeId
  45. )
  46. return;
  47. state.playlist.songs.splice(
  48. song.newIndex,
  49. 0,
  50. state.playlist.songs.splice(song.oldIndex, 1)[0]
  51. );
  52. }
  53. }
  54. };