import {fromJS, Record} from 'immutable';

import {
  STORE_ACTIVITIES,
  UPDATE_ACTIVITY
} from './actions';

const Activity = Record({
  id: null,
  actor: null,
  type: '',
  object: {},
  status: {}
});

export const initialState = fromJS({
  byId: {}
});


export default function reducer(state = initialState, action) {
  switch (action.type) {
    case STORE_ACTIVITIES: {
      const activities = {};
      action.payload.activities.forEach((a) => {
        activities[a.id] = new Activity(a);
      });
      return state.mergeIn(['byId'], activities);
    }
    case UPDATE_ACTIVITY: {
      const {activity} = action.payload;
      return state.mergeDeepIn(['byId', activity.id], activity);
    }
    default:
      return state;
  }
}

import {Record} from 'immutable';

import {
  STORE_ACTIVITIES,
  UPDATE_ACTIVITY
} from './actions';

const Activity = Record({
  id: null,
  actor: null,
  type: '',
  object: {},
  status: {}
});

const InitialState = Record({
  byId: {}
});

export const initialState = new InitialState();

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case STORE_ACTIVITIES: {
      const activities = {};
      action.payload.activities.forEach((a) => {
        activities[a.id] = new Activity(a);
      });
      return state.mergeIn(['byId'], activities);
    }
    case UPDATE_ACTIVITY: {
      const {activity} = action.payload;
      return state.mergeDeepIn(['byId', activity.id], activity);
    }
    default:
      return state;
  }
}

Update to use Records for initialState
feat(redux-module-activities): Update to use Records for initialState
