application/redux/reducers/issue-reducers.js
import _ from 'lodash';
import Immutable from 'immutable';
import {
ISSUES_REQUEST,
ISSUES_RECEIVE,
ISSUE_RECEIVE,
ISSUES_INVALIDATE,
ISSUE_SELECTED,
ISSUES_FILTER_OPEN,
ISSUES_RECEIVE_PAGINATION
} from './../actions/issue-actions';
const PER_PAGE = 20;
export function issues(state = null, action) {
if (state === null) {
state = {
isFetching: false,
didInvalidate: false,
items: Immutable.fromJS({}),
open: Immutable.fromJS({}),
selected: false,
order: "published_at",
dir: "desc",
page: 0,
pagination: {
before: 0,
current: 0,
first: 0,
last: 0,
limit: 0,
next: 0,
total_items: 0,
total_pages: 0
},
};
}
let newState;
switch (action.type) {
case ISSUES_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 ISSUES_INVALIDATE:
return Object.assign({}, state, {
didInvalidate: true,
});
case ISSUES_REQUEST:
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false
});
case ISSUE_SELECTED: {
return Object.assign({}, state, {
selected: action.payload.selected
});
}
case ISSUE_RECEIVE:
let foundOne = false;
const issue = action.payload.issue;
const issues = state.items.map((item, i) => {
if (item.get('uuid') == issue.get('uuid')) {
foundOne = true;
return issue;
}
return item;
});
if (!foundOne) {
if (issues.size) {
action.payload.issues = issues.push(issue);
} else {
action.payload.issues = Immutable.fromJS([issue]);
}
} else {
action.payload.issues = issues;
}
case ISSUES_RECEIVE:
return Object.assign({}, state, {
isFetching: false,
didInvalidate: false,
items: action.payload.issues
});
case ISSUES_FILTER_OPEN:
const iss = state.items.filter((item, i) => {
return parseInt(item.get('status')) == 1;
}).map((item, i) => item.get('uuid'));
return Object.assign({}, state, {
open: iss
});
}
return state;
};