stationInfo.js 5.5 KB

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