application/redux/reducers/channel-reducers.js
import _ from 'lodash';
import Immutable from 'immutable';
import {
CHANNELS_REQUEST,
CHANNELS_RECEIVE,
CHANNELS_RECEIVE_PAGINATION,
CHANNEL_RECEIVE,
CHANNELS_INVALIDATE,
CHANNEL_SELECTED,
CHANNELS_SET_PAGE
} from './../actions/channel-actions';
const PER_PAGE = 20;
export function channels(state = null, action) {
if (state === null) {
state = {
isFetching: false,
didInvalidate: false,
items: Immutable.fromJS({}),
selected: false,
page: 0,
pagination: {
before: 0,
current: 0,
first: 0,
last: 0,
limit: 0,
next: 0,
total_items: 0,
total_pages: 0
},
order: "name",
dir: "asc",
filter: false
};
}
let newState;
switch (action.type) {
case CHANNELS_RECEIVE_PAGINATION:
return Object.assign({}, state, {
pagination: {
'before': action.payload.before,
'current': action.payload.current,
'first': action.payload.first,
'last': action.payload.last,
'limit': action.payload.limit,
'next': action.payload.next,
'total_items': action.payload.total_items,
'total_pages': action.payload.total_pages,
}
});
case CHANNELS_INVALIDATE:
return Object.assign({}, state, {
didInvalidate: true,
});
case CHANNELS_REQUEST:
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false
});
case CHANNEL_SELECTED: {
return Object.assign({}, state, {
selected: action.payload.selected
});
}
case CHANNEL_RECEIVE:
const channel = action.payload.channel;
let found = false;
const allChannels = state.items.map((item, i) => {
if (item.get('uuid') == channel.get('uuid')) {
found = true;
return channel;
}
return item;
});
if (!found) {
let temp = allChannels.toList().asMutable();
temp.push(channel);
action.payload.channels = temp.asMutable();
} else {
action.payload.channels = allChannels;
}
case CHANNELS_RECEIVE:
return Object.assign({}, state, {
isFetching: false,
didInvalidate: false,
items: action.payload.channels
});
}
return state;
};