stationInfo.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. song.songId = song._id;
  138. delete song._id;
  139. return song;
  140. }));
  141. return state.merge({
  142. stationId,
  143. name: action.station.name,
  144. displayName,
  145. description,
  146. privacy,
  147. type,
  148. ownerId,
  149. paused,
  150. mode: (getModeTemp(action.station.partyMode, action.station.locked)),
  151. userList: fromJS(action.station.userList),
  152. userCount,
  153. songList: fromJS(songList),
  154. privatePlaylist: action.station.privatePlaylist,
  155. });
  156. case LEAVE:
  157. return initialState;
  158. case USER_LIST_UPDATE:
  159. return state.merge({
  160. userList: fromJS(action.userList),
  161. });
  162. case USER_COUNT_UPDATE:
  163. return state.merge({
  164. userCount: action.userCount,
  165. });
  166. case NAME_UPDATE:
  167. return state.merge({
  168. name: action.name,
  169. });
  170. case DISPLAY_NAME_UPDATE:
  171. return state.merge({
  172. displayName: action.displayName,
  173. });
  174. case DESCRIPTION_UPDATE:
  175. return state.merge({
  176. description: action.description,
  177. });
  178. case MODE_UPDATE:
  179. return state.merge({
  180. mode: getModeTemp(action.partyMode, action.locked),
  181. });
  182. case QUEUE_INDEX:
  183. songList = fromJS(action.songList.map((song) => {
  184. song.songId = song._id;
  185. delete song._id;
  186. return song;
  187. }));
  188. return state.merge({
  189. songList,
  190. });
  191. case QUEUE_UPDATE:
  192. songList = fromJS(action.songList.map((song) => {
  193. song.songId = song._id;
  194. delete song._id;
  195. return song;
  196. }));
  197. return state.merge({
  198. songList,
  199. });
  200. case PAUSE:
  201. return state.merge({
  202. paused: true,
  203. });
  204. case RESUME:
  205. return state.merge({
  206. paused: false,
  207. });
  208. case SELECT_PLAYLIST:
  209. return state.merge({
  210. privatePlaylist: action.playlistId,
  211. });
  212. case SELECT_PLAYLIST_QUEUE:
  213. return state.merge({
  214. privatePlaylistQueue: action.playlistId,
  215. });
  216. }
  217. return state;
  218. }
  219. const actionCreators = {
  220. joinStation,
  221. leaveStation,
  222. userListUpdate,
  223. userCountUpdate,
  224. nameUpdate,
  225. displayNameUpdate,
  226. descriptionUpdate,
  227. modeUpdate,
  228. queueIndex,
  229. queueUpdate,
  230. pause,
  231. resume,
  232. selectPlaylist,
  233. selectPlaylistQueue,
  234. };
  235. const actionTypes = {
  236. JOIN_STATION: JOIN,
  237. LEAVE_STATION: LEAVE,
  238. USER_LIST_UPDATE,
  239. USER_COUNT_UPDATE,
  240. NAME_UPDATE,
  241. DISPLAY_NAME_UPDATE,
  242. DESCRIPTION_UPDATE,
  243. MODE_UPDATE,
  244. QUEUE_INDEX,
  245. QUEUE_UPDATE,
  246. PAUSE,
  247. RESUME,
  248. SELECT_PLAYLIST,
  249. SELECT_PLAYLIST_QUEUE,
  250. };
  251. export {
  252. actionCreators,
  253. actionTypes,
  254. };
  255. export default reducer;