application/redux/reducers/assignment-reducers.js
import _ from 'lodash';
import Immutable from 'immutable';
import moment from 'moment';
import {
ASSIGNMENTS_REQUEST,
ASSIGNMENTS_RECEIVE,
ASSIGNMENTS_RECEIVE_ONE,
ASSIGNMENTS_RECEIVE_ONE_FROM_SOCKET,
ASSIGNMENTS_INVALIDATE,
ASSIGNMENTS_SELECTED,
ASSIGNMENTS_FILTER,
ASSIGNMENTS_SORT,
ASSIGNMENTS_GOTO_PAGE,
ASSIGNMENTS_NEXT_PAGE,
ASSIGNMENTS_PREV_PAGE,
ASSIGNMENTS_CASCADE_FILTERS,
ASSIGNMENTS_RECEIVE_PAGINATION
} from './../actions/assignment-actions';
export function assignments(state = null, action) {
if (state === null) {
state = {
isFetching: false,
didInvalidate: false,
items: Immutable.fromJS({}),
selected: false,
view: 0,
pagination: {
before: 0,
current: 0,
first: 0,
last: 0,
limit: 0,
next: 0,
total_items: 0,
total_pages: 0
}
};
}
let newState = Object.assign({}, state);
let filters;
const per_page = 20;
switch (action.type) {
case ASSIGNMENTS_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 ASSIGNMENTS_INVALIDATE:
return Object.assign({}, state, {
didInvalidate: true,
});
case ASSIGNMENTS_REQUEST:
return Object.assign({}, state, {
isFetching: true,
didInvalidate: false
});
case ASSIGNMENTS_SELECTED: {
return Object.assign({}, state, {
selected: action.payload.selected
});
}
/*
From here, each step waterfalls into the next.
*/
// case ASSIGNMENTS_RECEIVE_ONE:
// let myAssignments = state.items.map((item, i) => {
// if (item.get('uuid') == action.payload.assignment.get('uuid')) {
// return action.payload.assignment;
// }
// return item;
// });
// return Object.assign({}, state, {
// isFetching: false,
// didInvalidate: false,
// items: Immutable.fromJS(myAssignments)
// });
/*
From here, each step waterfalls into the next.
*/
case ASSIGNMENTS_RECEIVE_ONE:
const assignment = action.payload.assignment;
let foundOne = false;
let allAssignments = state.items.map((item, i) => {
if (item.get('uuid') == assignment.get('uuid')) {
foundOne = true;
return assignment;
}
return item;
});
if (!foundOne) {
let col = allAssignments.toJS();
if (col && col.length) {
col.push(assignment);
} else {
col = [assignment];
}
allAssignments = Immutable.fromJS(col);
}
action.payload.assignments = Immutable.fromJS(allAssignments);
case ASSIGNMENTS_RECEIVE:
/*
Just got fresh assignments, so replace everything
*/
return Object.assign({}, state, {
isFetching: false,
didInvalidate: false,
items: action.payload.assignments
});
// case ASSIGNMENTS_FILTER:
// /*
// Apply any pending filters
// */
// let filterVal = state.filters.token ? state.filters.token : false;
// if (action.payload.filters && typeof action.payload.filters.token !== 'undefined') {
// filterVal = action.payload.filters.token;
// }
// let filterWorkflow = state.filters.workflow ? state.filters.workflow : false;
// if (action.payload.filters && typeof action.payload.filters.workflow !== 'undefined') {
// filterWorkflow = action.payload.filters.workflow;
// }
// let filterStatus = state.filters.status ? state.filters.status : false;
// if (action.payload.filters && typeof action.payload.filters.status !== 'undefined') {
// filterStatus = action.payload.filters.status;
// }
// return Object.assign({}, state, {
// filters: Object.assign({}, state.filters, {
// token: filterVal,
// workflow: filterWorkflow,
// status: filterStatus
// })
// });
// case ASSIGNMENTS_GOTO_PAGE:
// /*
// Load the correct page
// */
// let page = state.filters.page ? state.filters.page : false;
// if (action.payload.page) {
// page = action.payload.page;
// }
// return Object.assign({}, state, {
// filters: Object.assign({}, newState.filters, {page: page})
// });
// case ASSIGNMENTS_CASCADE_FILTERS:
// const now = moment().utc();
// let filtered = state.items.filter((item, i) => {
// if (!filterWorkflow) {
// return true;
// }
// if (item.get('export') && item.get('export').get('workflow_id') && item.get('export').get('workflow_id') == filterWorkflow) {
// return true;
// }
// if (item.get('workflow_id') && item.get('workflow_id') == filterWorkflow) {
// return true;
// }
// }).filter((item, i) => {
// if (!filterStatus) {
// return true;
// }
// let status = 'draft';
// if (item.get('published_at')) {
// const pubTime = moment.utc(item.get('published_at'));
// if (now.isBefore(pubTime)) {
// status = 'scheduled';
// }
// if (now.isAfter(pubTime)) {
// status = 'published';
// }
// }
// if (status == filterStatus) {
// return true;
// }
// }).filter((item, i) => {
// if (!filterVal) {
// return true;
// }
// if (item.get('title') && item.get('title').indexOf(filterVal) !== -1) {
// return true;
// }
// if (item.get('slug') && item.get('slug').indexOf(filterVal) !== -1) {
// return true;
// }
// if (item.get('content') && item.get('content').indexOf(filterVal) !== -1) {
// return true;
// }
// if (item.get('abstract') && item.get('abstract').indexOf(filterVal) !== -1) {
// return true;
// }
// });
// const offset = (parseInt(page)-1) * per_page;
// return Object.assign({}, state, {
// filtered: filtered.map((item, i) => item.get('uuid')).skip(offset).take(per_page)
// });
}
return state;
};