2016-09-01 13:21:48 +02:00
|
|
|
import { TIMELINE_SET, TIMELINE_UPDATE } from '../actions/timelines';
|
|
|
|
import { REBLOG_SUCCESS, FAVOURITE_SUCCESS } from '../actions/interactions';
|
|
|
|
import Immutable from 'immutable';
|
2016-08-24 17:56:44 +02:00
|
|
|
|
|
|
|
const initialState = Immutable.Map();
|
|
|
|
|
2016-09-01 13:21:48 +02:00
|
|
|
function updateMatchingStatuses(state, needle, callback) {
|
|
|
|
return state.map(function (list) {
|
|
|
|
return list.map(function (status) {
|
|
|
|
if (status.get('id') === needle.get('id')) {
|
|
|
|
return callback(status);
|
2016-09-03 14:01:10 +02:00
|
|
|
} else if (status.getIn(['reblog', 'id'], null) === needle.get('id')) {
|
|
|
|
return status.set('reblog', callback(status.get('reblog')));
|
2016-09-01 13:21:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-31 16:15:12 +02:00
|
|
|
export default function timelines(state = initialState, action) {
|
2016-08-24 17:56:44 +02:00
|
|
|
switch(action.type) {
|
2016-08-31 16:15:12 +02:00
|
|
|
case TIMELINE_SET:
|
2016-08-24 17:56:44 +02:00
|
|
|
return state.set(action.timeline, Immutable.fromJS(action.statuses));
|
2016-08-31 16:15:12 +02:00
|
|
|
case TIMELINE_UPDATE:
|
2016-09-01 13:21:48 +02:00
|
|
|
return state.update(action.timeline, list => list.unshift(Immutable.fromJS(action.status)));
|
|
|
|
case REBLOG_SUCCESS:
|
|
|
|
case FAVOURITE_SUCCESS:
|
|
|
|
return updateMatchingStatuses(state, action.status, () => Immutable.fromJS(action.response));
|
2016-08-24 17:56:44 +02:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|