stationInfo.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { Map, List, fromJS } from "immutable";
  2. const JOIN = "STATION_INFO::JOIN";
  3. const LEAVE = "STATION_INFO::LEAVE";
  4. const USER_LIST_UPDATE = "STATION_INFO::USER_LIST_UPDATE";
  5. const USER_COUNT_UPDATE = "STATION_INFO::USER_COUNT_UPDATE";
  6. const NAME_UPDATE = "STATION_INFO::NAME_UPDATE";
  7. const DISPLAY_NAME_UPDATE = "STATION_INFO::DISPLAY_NAME_UPDATE";
  8. const DESCRIPTION_UPDATE = "STATION_INFO::DESCRIPTION_UPDATE";
  9. const MODE_UPDATE = "STATION_INFO::MODE_UPDATE";
  10. const QUEUE_INDEX = "STATION_INFO::QUEUE_INDEX";
  11. const QUEUE_UPDATE = "STATION_INFO::QUEUE_UPDATE";
  12. const PAUSE = "STATION_INFO::PAUSE";
  13. const RESUME = "STATION_INFO::RESUME";
  14. const SELECT_PLAYLIST = "STATION_INFO::SELECT_PLAYLIST";
  15. const SELECT_PLAYLIST_QUEUE = "STATION_INFO::SELECT_PLAYLIST_QUEUE";
  16. function joinStation(station) {
  17. return {
  18. type: JOIN,
  19. station,
  20. }
  21. }
  22. function leaveStation() {
  23. return {
  24. type: LEAVE,
  25. }
  26. }
  27. function userListUpdate(userList) {
  28. return {
  29. type: USER_LIST_UPDATE,
  30. userList,
  31. }
  32. }
  33. function userCountUpdate(userCount) {
  34. return {
  35. type: USER_COUNT_UPDATE,
  36. userCount,
  37. }
  38. }
  39. function nameUpdate(name) {
  40. return {
  41. type: NAME_UPDATE,
  42. name,
  43. }
  44. }
  45. function displayNameUpdate(displayName) {
  46. return {
  47. type: DISPLAY_NAME_UPDATE,
  48. displayName,
  49. }
  50. }
  51. function descriptionUpdate(description) {
  52. return {
  53. type: DESCRIPTION_UPDATE,
  54. description,
  55. }
  56. }
  57. function modeUpdate(partyMode, locked) {
  58. return {
  59. type: MODE_UPDATE,
  60. partyMode,
  61. locked,
  62. }
  63. }
  64. function queueIndex(songList) {
  65. return {
  66. type: QUEUE_INDEX,
  67. songList,
  68. }
  69. }
  70. function queueUpdate(songList) {
  71. return {
  72. type: QUEUE_UPDATE,
  73. songList,
  74. }
  75. }
  76. function pause() {
  77. return {
  78. type: PAUSE,
  79. }
  80. }
  81. function resume() {
  82. return {
  83. type: RESUME,
  84. }
  85. }
  86. function selectPlaylist(playlistId) {
  87. return {
  88. type: SELECT_PLAYLIST,
  89. playlistId,
  90. }
  91. }
  92. function selectPlaylistQueue(playlistId) {
  93. return {
  94. type: SELECT_PLAYLIST_QUEUE,
  95. playlistId,
  96. }
  97. }
  98. const initialState = Map({
  99. "stationId": "",
  100. "name": "",
  101. "displayName": "",
  102. "description": "",
  103. "privacy": "private",
  104. "type": "community",
  105. "ownerId": "",
  106. "paused": true,
  107. "mode": "",
  108. "userList": List([]),
  109. "userCount": 0,
  110. "songList": List([]),
  111. "privatePlaylist": "",
  112. "privatePlaylistQueue": "",
  113. });
  114. function reducer(state = initialState, action) {
  115. let name, displayName, description, mode, userList, userCount, songList;
  116. function getModeTemp(partyEnabled, queueLocked) {
  117. // If party enabled
  118. // If queue locked
  119. // Mode is DJ
  120. // If queue not locked
  121. // Mode party
  122. // If party not enabled
  123. // Mode is normal
  124. if (partyEnabled) {
  125. if (queueLocked) return "dj";
  126. else return "party";
  127. } else return "normal";
  128. }
  129. switch (action.type) {
  130. case JOIN:
  131. const { stationId, privacy, type, ownerId, paused } = action.station;
  132. name = action.station.name;
  133. displayName = action.station.displayName;
  134. description = action.station.description;
  135. userCount = action.station.userCount;
  136. songList = fromJS(action.station.songList.map((song) => {
  137. if (!song.songId) {
  138. song.songId = song._id;
  139. delete song._id;
  140. }
  141. return song;
  142. }));
  143. return state.merge({
  144. stationId,
  145. name: action.station.name,
  146. displayName,
  147. description,
  148. privacy,
  149. type,
  150. ownerId,
  151. paused,
  152. mode: (getModeTemp(action.station.partyMode, action.station.locked)),
  153. userList: fromJS(action.station.userList),
  154. userCount,
  155. songList: fromJS(songList),
  156. privatePlaylist: action.station.privatePlaylist,
  157. });
  158. case LEAVE:
  159. return initialState;
  160. case USER_LIST_UPDATE:
  161. return state.merge({
  162. userList: fromJS(action.userList),
  163. });
  164. case USER_COUNT_UPDATE:
  165. return state.merge({
  166. userCount: action.userCount,
  167. });
  168. case NAME_UPDATE:
  169. return state.merge({
  170. name: action.name,
  171. });
  172. case DISPLAY_NAME_UPDATE:
  173. return state.merge({
  174. displayName: action.displayName,
  175. });
  176. case DESCRIPTION_UPDATE:
  177. return state.merge({
  178. description: action.description,
  179. });
  180. case MODE_UPDATE:
  181. return state.merge({
  182. mode: getModeTemp(action.partyMode, action.locked),
  183. });
  184. case QUEUE_INDEX:
  185. songList = fromJS(action.songList.map((song) => {
  186. if (!song.songId) {
  187. song.songId = song._id;
  188. delete song._id;
  189. }
  190. return song;
  191. }));
  192. return state.merge({
  193. songList,
  194. });
  195. case QUEUE_UPDATE:
  196. songList = fromJS(action.songList.map((song) => {
  197. if (!song.songId) {
  198. song.songId = song._id;
  199. delete song._id;
  200. }
  201. return song;
  202. }));
  203. return state.merge({
  204. songList,
  205. });
  206. case PAUSE:
  207. return state.merge({
  208. paused: true,
  209. });
  210. case RESUME:
  211. return state.merge({
  212. paused: false,
  213. });
  214. case SELECT_PLAYLIST:
  215. return state.merge({
  216. privatePlaylist: action.playlistId,
  217. });
  218. case SELECT_PLAYLIST_QUEUE:
  219. return state.merge({
  220. privatePlaylistQueue: action.playlistId,
  221. });
  222. }
  223. return state;
  224. }
  225. const actionCreators = {
  226. joinStation,
  227. leaveStation,
  228. userListUpdate,
  229. userCountUpdate,
  230. nameUpdate,
  231. displayNameUpdate,
  232. descriptionUpdate,
  233. modeUpdate,
  234. queueIndex,
  235. queueUpdate,
  236. pause,
  237. resume,
  238. selectPlaylist,
  239. selectPlaylistQueue,
  240. };
  241. const actionTypes = {
  242. JOIN_STATION: JOIN,
  243. LEAVE_STATION: LEAVE,
  244. USER_LIST_UPDATE,
  245. USER_COUNT_UPDATE,
  246. NAME_UPDATE,
  247. DISPLAY_NAME_UPDATE,
  248. DESCRIPTION_UPDATE,
  249. MODE_UPDATE,
  250. QUEUE_INDEX,
  251. QUEUE_UPDATE,
  252. PAUSE,
  253. RESUME,
  254. SELECT_PLAYLIST,
  255. SELECT_PLAYLIST_QUEUE,
  256. };
  257. export {
  258. actionCreators,
  259. actionTypes,
  260. };
  261. export default reducer;