Home Reference Source

application/redux/reducers/content-reducers.js

import _ from 'lodash';
import Immutable from 'immutable';
import moment from 'moment';

import {
    CONTENT_REQUEST,
    CONTENT_RECEIVE,
    CONTENT_RECEIVE_ONE,
    CONTENT_RECEIVE_ONE_FROM_SOCKET,
    CONTENT_INVALIDATE,
    CONTENT_SELECTED,
    CONTENT_REMOVE_ONE,
    CONTENT_RECEIVE_PAGINATION
} from './../actions/content-actions';

export function content(state = null, action) {
    if (state === null) {
        state = {
            isFetching: false,
            didInvalidate: false,
            items: Immutable.fromJS({}),
            selected: false,
            pagination: {
                before: 0,
                current: 0,
                first: 0,
                last: 0,
                limit: 0,
                next: 0,
                total_items: 0,
                total_pages: 0
            },
        };
    }

    switch (action.type) {
        case CONTENT_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 CONTENT_INVALIDATE:
            return Object.assign({}, state, {
                didInvalidate: true,
            });
        case CONTENT_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case CONTENT_SELECTED:
            return Object.assign({}, state, {
                selected: action.payload.selected
            });
        case CONTENT_REMOVE_ONE:
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                items: state.items.filter((item, i) => item.get('uuid') != action.payload.content.get('uuid'))
            });
        case CONTENT_RECEIVE_ONE_FROM_SOCKET:
            let thisContent = state.items.map((item, i) => {
                if (item.get('uuid') == action.payload.content.get('uuid')) {
                    return action.payload.content;
                }
                return item;
            });

            return Object.assign({}, state, {
                items: Immutable.fromJS(thisContent)
            });
        /*
            From here, each step waterfalls into the next.
        */
       case CONTENT_RECEIVE_ONE:
            const content = action.payload.content;
            let foundOne = false;
            let allContent = state.items.map((item, i) => {
                if (item.get('uuid') == content.get('uuid')) {
                    foundOne = true;
                    return content;
                }
                return item;
            });

            if (!foundOne) {
                let col = allContent.toJS();
                if (col && col.length) {
                    col.push(content);
                } else {
                    col = [content];
                }
                allContent = Immutable.fromJS(col);
            }

            action.payload.content = Immutable.fromJS(allContent);
        case CONTENT_RECEIVE:
            /*
                Just got fresh content, so replace everything
            */
            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                items: action.payload.content
            });
        // case CONTENT_FILTER:
        //     /*
        //         Apply any pending filters
        //     */
        //     let filterVal = newState.filters.token ? newState.filters.token : false;
        //     if (action.payload.filters && typeof action.payload.filters.token !== 'undefined') {
        //         filterVal = action.payload.filters.token;
        //     }

        //     let filterWorkflow = newState.filters.workflow ? newState.filters.workflow : false;
        //     if (action.payload.filters && typeof action.payload.filters.workflow !== 'undefined') {
        //         filterWorkflow = action.payload.filters.workflow;
        //     }

        //     let filterStatus = newState.filters.status ? newState.filters.status : false;
        //     if (action.payload.filters && typeof action.payload.filters.status !== 'undefined') {
        //         filterStatus = action.payload.filters.status;
        //     }

        //     let filteredItems = newState.items;
        //     if (filterWorkflow) {
        //         filteredItems = filteredItems.filter((item, i) => {
        //             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;
        //             }
        //         });
        //     }

        //     if (filterStatus) {
        //         const now = moment().utc();
        //         filteredItems = filteredItems.filter((item, i) => {
        //             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;
        //             }
        //         });
        //     }

        //     if (filterVal) {
        //         filteredItems = filteredItems.filter((item, i) => {
        //             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;
        //             }
        //         });
        //     }

        //     filteredItems = filteredItems.sort((a, b) => {
        //         const momentA = moment.utc(a.get('modified_at')).local();
        //         const momentB = moment.utc(b.get('modified_at')).local();

        //         if (momentA.isBefore(momentB)) {
        //             return 1;
        //         } else {
        //             return -1;
        //         }
        //     })

        //     newState = Object.assign({}, newState, {
        //         filteredList: filteredItems.map((item, i) => item.get('uuid')),
        //         filters: Object.assign({}, newState.filters, {
        //             token: filterVal,
        //             workflow: filterWorkflow,
        //             status: filterStatus
        //         })
        //     });
    }

    return state;
};