homepage.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Map, List, fromJS } 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 = Map({
  63. stations: Map({
  64. official: List([]),
  65. community: List([]),
  66. }),
  67. });
  68. stations.forEach((station) => {
  69. station.stationId = station._id;
  70. delete station._id;
  71. state = state.updateIn(["stations", station.type], (stations) => {
  72. return stations.push(fromJS(station));
  73. });
  74. });
  75. return state;
  76. case STATION_CREATE:
  77. state = state.updateIn(["stations", station.type], (list) => {
  78. return list.push(station);
  79. });
  80. return state;
  81. case STATION_REMOVE:
  82. updateStationById(stationId, (list, index) => {
  83. list.delete(index);
  84. });
  85. return state;
  86. case STATION_SONG_UPDATE:
  87. const { thumbnail } = action;
  88. updateStationById(stationId, (list, index) => {
  89. list.update(index, station => {
  90. return station.set("thumbnail", thumbnail);
  91. });
  92. });
  93. return state;
  94. case STATION_USER_COUNT_UPDATE:
  95. const { userCount } = action;
  96. updateStationById(stationId, (list, index) => {
  97. list.update(index, station => {
  98. return station.set("userCount", userCount);
  99. });
  100. });
  101. return state;
  102. }
  103. return state;
  104. }
  105. const actionCreators = {
  106. stationIndex,
  107. stationCreate,
  108. stationRemove,
  109. stationSongUpdate,
  110. stationUserCountUpdate,
  111. };
  112. const actionTypes = {
  113. STATION_REMOVE,
  114. STATION_SONG_UPDATE,
  115. STATION_USER_COUNT_UPDATE,
  116. };
  117. export {
  118. actionCreators,
  119. actionTypes,
  120. };
  121. export default reducer;