2
0

stationInfo.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { Map, List } from "immutable";
  2. const JOIN_STATION = "STATION_INFO::JOIN_STATION";
  3. const LEAVE_STATION = "STATION_INFO::LEAVE_STATION";
  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. function joinStation(station) {
  13. return {
  14. type: JOIN_STATION,
  15. station,
  16. }
  17. }
  18. function leaveStation() {
  19. return {
  20. type: LEAVE_STATION,
  21. }
  22. }
  23. function userListUpdate(userList) {
  24. return {
  25. type: USER_LIST_UPDATE,
  26. userList,
  27. }
  28. }
  29. function userCountUpdate(userCount) {
  30. return {
  31. type: USER_COUNT_UPDATE,
  32. userCount,
  33. }
  34. }
  35. function nameUpdate(name) {
  36. return {
  37. type: NAME_UPDATE,
  38. name,
  39. }
  40. }
  41. function displayNameUpdate(displayName) {
  42. return {
  43. type: DISPLAY_NAME_UPDATE,
  44. displayName,
  45. }
  46. }
  47. function descriptionUpdate(description) {
  48. return {
  49. type: DESCRIPTION_UPDATE,
  50. description,
  51. }
  52. }
  53. function modeUpdate(mode) {
  54. return {
  55. type: MODE_UPDATE,
  56. mode,
  57. }
  58. }
  59. function queueIndex(songList) {
  60. return {
  61. type: QUEUE_INDEX,
  62. songList,
  63. }
  64. }
  65. function queueUpdate(songList) {
  66. return {
  67. type: QUEUE_UPDATE,
  68. songList,
  69. }
  70. }
  71. const initialState = Map({
  72. "stationId": "",
  73. "name": "",
  74. "displayName": "",
  75. "description": "",
  76. "privacy": "private",
  77. "type": "community",
  78. "ownerId": "",
  79. "paused": true,
  80. "mode": "",
  81. "userList": List([]),
  82. "userCount": 0,
  83. "songList": List([]),
  84. });
  85. function reducer(state = initialState, action) {
  86. let name, displayName, description, mode, userList, userCount, songList;
  87. function getModeTemp(partyEnabled, queueLocked) {
  88. // If party enabled
  89. // If queue locked
  90. // Mode is DJ
  91. // If queue not locked
  92. // Mode party
  93. // If party not enabled
  94. // Mode is normal
  95. if (partyEnabled) {
  96. if (queueLocked) return "dj";
  97. else return "party";
  98. } else return "normal";
  99. }
  100. switch (action.type) {
  101. case JOIN_STATION:
  102. const { stationId, privacy, type, ownerId, paused } = action.station;
  103. name = action.station.name;
  104. displayName = action.station.displayName;
  105. description = action.station.description;
  106. userCount = action.station.userCount;
  107. mode = (getModeTemp(action.station.partyMode, action.station.locked));
  108. userList = List([]);
  109. action.station.userList.forEach((user) => {
  110. userList.push(user);
  111. });
  112. songList = List([]);
  113. action.station.songList.forEach((song) => {
  114. songList.push(song);
  115. });
  116. return state.merge({
  117. stationId,
  118. name: action.station.name,
  119. displayName,
  120. description,
  121. privacy,
  122. type,
  123. ownerId,
  124. paused,
  125. mode,
  126. userList,
  127. userCount,
  128. songList,
  129. });
  130. case LEAVE_STATION:
  131. return initialState;
  132. case USER_LIST_UPDATE:
  133. userList = List([]);
  134. action.userList.forEach((user) => {
  135. userList.push(user);
  136. });
  137. state.set("userList", userList);
  138. return state;
  139. case USER_COUNT_UPDATE:
  140. userCount = action.userCount;
  141. state.set("userCount", userCount);
  142. return state;
  143. case NAME_UPDATE:
  144. name = action.name;
  145. state.set("name", name);
  146. return state;
  147. case DISPLAY_NAME_UPDATE:
  148. displayName = action.displayName;
  149. state.set("displayName", displayName);
  150. return state;
  151. case DESCRIPTION_UPDATE:
  152. description = action.description;
  153. state.set("description", description);
  154. return state;
  155. case MODE_UPDATE:
  156. mode = action.mode;
  157. state.set("mode", mode);
  158. return state;
  159. case QUEUE_INDEX:
  160. songList = List([]);
  161. action.songList.forEach((song) => {
  162. songList.push(song);
  163. });
  164. return state;
  165. case QUEUE_UPDATE:
  166. songList = List([]);
  167. action.songList.forEach((song) => {
  168. songList.push(song);
  169. });
  170. return state;
  171. }
  172. return state;
  173. }
  174. const actionCreators = {
  175. joinStation,
  176. leaveStation,
  177. userListUpdate,
  178. userCountUpdate,
  179. nameUpdate,
  180. displayNameUpdate,
  181. descriptionUpdate,
  182. modeUpdate,
  183. queueIndex,
  184. queueUpdate,
  185. };
  186. const actionTypes = {
  187. JOIN_STATION,
  188. LEAVE_STATION,
  189. USER_LIST_UPDATE,
  190. USER_COUNT_UPDATE,
  191. NAME_UPDATE,
  192. DISPLAY_NAME_UPDATE,
  193. DESCRIPTION_UPDATE,
  194. MODE_UPDATE,
  195. QUEUE_INDEX,
  196. QUEUE_UPDATE,
  197. };
  198. export {
  199. actionCreators,
  200. actionTypes,
  201. };
  202. export default reducer;