123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import { Map, List, fromJS } from "immutable";
- const STATION_INDEX = "HOMEPAGE::STATION_INDEX";
- const STATION_CREATE = "HOMEPAGE::STATION_CREATE";
- const STATION_REMOVE = "HOMEPAGE::STATION_REMOVE";
- const STATION_SONG_UPDATE = "HOMEPAGE::STATION_SONG_UPDATE";
- const STATION_USER_COUNT_UPDATE = "HOMEPAGE::STATION_USER_COUNT_UPDATE";
- function stationIndex(stations) {
- return {
- type: STATION_INDEX,
- stations,
- }
- }
- function stationCreate(station) {
- return {
- type: STATION_CREATE,
- station,
- }
- }
- function stationRemove(stationId) {
- return {
- type: STATION_REMOVE,
- stationId,
- }
- }
- function stationSongUpdate(stationId, song) {
- return {
- type: STATION_SONG_UPDATE,
- stationId,
- thumbnail: song.thumbnail,
- }
- }
- function stationUserCountUpdate(stationId, userCount) {
- return {
- type: STATION_USER_COUNT_UPDATE,
- stationId,
- userCount,
- }
- }
- const initialState = Map({
- stations: Map({
- official: List([]),
- community: List([]),
- }),
- });
- function reducer(state = initialState, action) {
- function updateStationById(stationId, cb) {
- function byType(type) {
- const index = state.getIn(["stations", type]).findIndex(function (station) {
- return station.get("stationId") === stationId;
- });
- if (index === -1) return null;
- state = state.updateIn(["stations", type], (list) => {
- cb(list, index);
- });
- }
- byType("official");
- byType("community");
- }
- const { stationId, stations, station } = action;
- switch (action.type) {
- case STATION_INDEX:
- state = Map({
- stations: Map({
- official: List([]),
- community: List([]),
- }),
- });
- stations.forEach((station) => {
- station.stationId = station._id;
- delete station._id;
- state = state.updateIn(["stations", station.type], (stations) => {
- return stations.push(fromJS(station));
- });
- });
- return state;
- case STATION_CREATE:
- state = state.updateIn(["stations", station.type], (list) => {
- return list.push(station);
- });
- return state;
- case STATION_REMOVE:
- updateStationById(stationId, (list, index) => {
- list.delete(index);
- });
- return state;
- case STATION_SONG_UPDATE:
- const { thumbnail } = action;
- updateStationById(stationId, (list, index) => {
- list.update(index, station => {
- return station.set("thumbnail", thumbnail);
- });
- });
- return state;
- case STATION_USER_COUNT_UPDATE:
- const { userCount } = action;
- updateStationById(stationId, (list, index) => {
- list.update(index, station => {
- return station.set("userCount", userCount);
- });
- });
- return state;
- }
- return state;
- }
- const actionCreators = {
- stationIndex,
- stationCreate,
- stationRemove,
- stationSongUpdate,
- stationUserCountUpdate,
- };
- const actionTypes = {
- STATION_REMOVE,
- STATION_SONG_UPDATE,
- STATION_USER_COUNT_UPDATE,
- };
- export {
- actionCreators,
- actionTypes,
- };
- export default reducer;
|