stationPlaylists.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { Map, List, fromJS } from "immutable";
  2. const UPDATE = "STATION_PLAYLISTS::UPDATE";
  3. const ADD_SONG = "STATION_PLAYLISTS::ADD_SONG";
  4. const UPDATE_DISPLAY_NAME = "STATION_PLAYLISTS::UPDATE_DISPLAY_NAME";
  5. const MOVE_SONG_TO_BOTTOM = "STATION_PLAYLISTS::MOVE_SONG_TO_BOTTOM";
  6. const MOVE_SONG_TO_TOP = "STATION_PLAYLISTS::MOVE_SONG_TO_TOP";
  7. const REMOVE_SONG = "STATION_PLAYLISTS::REMOVE_SONG";
  8. function update(playlists) {
  9. return {
  10. type: UPDATE,
  11. playlists,
  12. }
  13. }
  14. function addSong(playlistId, song) {
  15. return {
  16. type: ADD_SONG,
  17. playlistId,
  18. song,
  19. }
  20. }
  21. function updateDisplayName(playlistId, displayName) {
  22. return {
  23. type: UPDATE_DISPLAY_NAME,
  24. playlistId,
  25. displayName,
  26. }
  27. }
  28. function moveSongToBottom(playlistId, songId) {
  29. return {
  30. type: MOVE_SONG_TO_BOTTOM,
  31. playlistId,
  32. songId,
  33. }
  34. }
  35. function moveSongToTop(playlistId, songId) {
  36. return {
  37. type: MOVE_SONG_TO_TOP,
  38. playlistId,
  39. songId,
  40. }
  41. }
  42. function removeSong(playlistId, songId) {
  43. return {
  44. type: REMOVE_SONG,
  45. playlistId,
  46. songId,
  47. }
  48. }
  49. const initialState = List([]);
  50. function reducer(state = initialState, action) {
  51. let playlistId, songId;
  52. function updatePlaylist(playlistId, updater) {
  53. return state.update(
  54. state.findIndex(function(playlist) {
  55. return playlist.get("playlistId") === playlistId;
  56. }),
  57. updater
  58. );
  59. }
  60. function updatePlaylistSongs(playlistId, updater) {
  61. return updatePlaylist(playlistId, function (playlist) {
  62. return playlist.update("songs", function (songs) {
  63. return songs.update(updater);
  64. });
  65. })
  66. }
  67. switch (action.type) {
  68. case UPDATE:
  69. let { playlists } = action;
  70. playlists = playlists.map((playlist) => {
  71. return {
  72. playlistId: playlist._id,
  73. createdAt: playlist.createdAt,
  74. createdBy: playlist.createdBy,
  75. displayName: playlist.displayName,
  76. songs: playlist.songs,
  77. };
  78. });
  79. return fromJS(playlists);
  80. case ADD_SONG:
  81. const { song } = action;
  82. playlistId = action.playlistId;
  83. return updatePlaylistSongs(playlistId, function (songs) {
  84. return songs.push(fromJS(song));
  85. });
  86. case UPDATE_DISPLAY_NAME:
  87. const { displayName } = action;
  88. playlistId = action.playlistId;
  89. return updatePlaylist(playlistId, function (playlist) {
  90. return playlist.set("displayName", displayName);
  91. });
  92. case MOVE_SONG_TO_BOTTOM:
  93. playlistId = action.playlistId;
  94. songId = action.songId;
  95. return updatePlaylistSongs(playlistId, function (songs) {
  96. let songIndex = songs.findIndex(function (song) {
  97. return song.get("songId") === songId;
  98. });
  99. let song = songs.get(songIndex);
  100. songs = songs.delete(songIndex);
  101. songs = songs.push(song);
  102. return songs;
  103. });
  104. case MOVE_SONG_TO_TOP:
  105. playlistId = action.playlistId;
  106. songId = action.songId;
  107. return updatePlaylistSongs(playlistId, function (songs) {
  108. let songIndex = songs.findIndex(function (song) {
  109. return song.get("songId") === songId;
  110. });
  111. let song = songs.get(songIndex);
  112. songs = songs.delete(songIndex);
  113. songs = songs.unshift(song);
  114. return songs;
  115. });
  116. case REMOVE_SONG:
  117. playlistId = action.playlistId;
  118. songId = action.songId;
  119. return updatePlaylistSongs(playlistId, function (songs) {
  120. let songIndex = songs.findIndex(function (song) {
  121. return song.get("songId") === songId;
  122. });
  123. return songs.delete(songIndex);
  124. });
  125. }
  126. return state;
  127. }
  128. const actionCreators = {
  129. update,
  130. addSong,
  131. updateDisplayName,
  132. moveSongToBottom,
  133. moveSongToTop,
  134. removeSong,
  135. };
  136. const actionTypes = {
  137. ADD_SONG,
  138. UPDATE_DISPLAY_NAME,
  139. MOVE_SONG_TO_BOTTOM,
  140. MOVE_SONG_TO_TOP,
  141. REMOVE_SONG,
  142. };
  143. export {
  144. actionCreators,
  145. actionTypes,
  146. };
  147. export default reducer;