homepage.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Map, List } from "immutable";
  2. const STATION_INDEX = "HOMEPAGE::STATION_INDEX";
  3. const STATION_CREATE = "HOMEPAGE::STATION_CREATE";
  4. const STATION_REMOVE = "HOMEPAGE::STATION_REMOVE";
  5. const STATION_SONG_UPDATE = "HOMEPAGE::STATION_SONG_UPDATE";
  6. const STATION_USER_COUNT_UPDATE = "HOMEPAGE::STATION_USER_COUNT_UPDATE";
  7. function stationIndex(stations) {
  8. return {
  9. type: STATION_INDEX,
  10. stations,
  11. }
  12. }
  13. function stationCreate(station) {
  14. return {
  15. type: STATION_CREATE,
  16. station,
  17. }
  18. }
  19. function stationRemove(stationId) {
  20. return {
  21. type: STATION_REMOVE,
  22. stationId,
  23. }
  24. }
  25. function stationSongUpdate(stationId, song) {
  26. return {
  27. type: STATION_SONG_UPDATE,
  28. stationId,
  29. thumbnail: song.thumbnail,
  30. }
  31. }
  32. function stationUserCountUpdate(stationId, userCount) {
  33. return {
  34. type: STATION_USER_COUNT_UPDATE,
  35. stationId,
  36. userCount,
  37. }
  38. }
  39. const initialState = Map({
  40. stations: Map({
  41. official: List([]),
  42. community: List([]),
  43. }),
  44. });
  45. function reducer(state = initialState, action) {
  46. function updateStationById(stationId, cb) {
  47. function byType(type) {
  48. const index = state.getIn(["stations", type]).findIndex(function (station) {
  49. return station.get("stationId") === stationId;
  50. });
  51. if (index === -1) return null;
  52. state = state.updateIn(["stations", type], (list) => {
  53. cb(list, index);
  54. });
  55. }
  56. byType("official");
  57. byType("community");
  58. }
  59. const { stationId, stations, station } = action;
  60. switch (action.type) {
  61. case STATION_INDEX:
  62. state.setIn(["stations", "official"], List([]));
  63. state.setIn(["stations", "community"], List([]));
  64. stations.forEach((station) => {
  65. state = state.updateIn(["stations", station.type], function(list) {
  66. return list.push(station);
  67. });
  68. });
  69. return state;
  70. case STATION_CREATE:
  71. state = state.updateIn(["stations", station.type], (list) => {
  72. return list.push(station);
  73. });
  74. return state;
  75. case STATION_REMOVE:
  76. updateStationById(stationId, (list, index) => {
  77. list.delete(index);
  78. });
  79. return state;
  80. case STATION_SONG_UPDATE:
  81. const { thumbnail } = action;
  82. updateStationById(stationId, (list, index) => {
  83. list.update(index, station => {
  84. return station.set("thumbnail", thumbnail);
  85. });
  86. });
  87. return state;
  88. case STATION_USER_COUNT_UPDATE:
  89. const { userCount } = action;
  90. updateStationById(stationId, (list, index) => {
  91. list.update(index, station => {
  92. return station.set("userCount", userCount);
  93. });
  94. });
  95. return state;
  96. }
  97. return state;
  98. }
  99. const actionCreators = {
  100. stationIndex,
  101. stationCreate,
  102. stationRemove,
  103. stationSongUpdate,
  104. stationUserCountUpdate,
  105. };
  106. const actionTypes = {
  107. STATION_REMOVE,
  108. STATION_SONG_UPDATE,
  109. STATION_USER_COUNT_UPDATE,
  110. };
  111. export {
  112. actionCreators,
  113. actionTypes,
  114. };
  115. export default reducer;