Home Reference Source

application/redux/reducers/assignment-comment-reducers.js

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

import {
    ASSIGNMENT_COMMENTS_REQUEST,
    ASSIGNMENT_COMMENTS_RECEIVE,
    ASSIGNMENT_COMMENTS_SELECTED,
    ASSIGNMENT_COMMENTS_INVALIDATE
} from './../actions/assignment-comment-actions';

export function assignmentComments(state = null, action) {
    if (state === null) {
        state = {
            isFetching: false,
            didInvalidate: false,
            items: Immutable.fromJS({})
        };
    }

    let newState = Object.assign({}, state);

    switch (action.type) {
        case ASSIGNMENT_COMMENTS_INVALIDATE:
            return Object.assign({}, state, {
                didInvalidate: true,
            });
        case ASSIGNMENT_COMMENTS_REQUEST:
            return Object.assign({}, state, {
                isFetching: true,
                didInvalidate: false
            });
        case ASSIGNMENT_COMMENTS_RECEIVE:
            let items = state.items.toJS();

            // we don't want to just replace what's there since
            // that'll remove any existing comments, so we want to
            // feather them in
            action.payload.comments.map((item, i) => {
                items[item.get('uuid')] = item;
            });

            return Object.assign({}, state, {
                isFetching: false,
                didInvalidate: false,
                items: Immutable.fromJS(items)
            });
    }

    return state;
};