Home Reference Source

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

import BaseService from './../../services/base-service';
import {parseSrn} from './../../util/strings';
import Immutable from 'immutable';

class AssignmentCommentService extends BaseService {

    fetch(id, opts = {}) {
        return this.get('/v3/assignment-comment/' + id, opts);
    }

    create(id, data) {
        return this.post('/v3/assignment-comment/' + id, data);
    }
}

const service = new AssignmentCommentService;

/**
 * Action types
 */
export const ASSIGNMENT_COMMENTS_REQUEST = 'ASSIGNMENT_COMMENTS_REQUEST';
export const ASSIGNMENT_COMMENTS_RECEIVE = 'ASSIGNMENT_COMMENTS_RECEIVE';
export const ASSIGNMENT_COMMENTS_SELECTED = 'ASSIGNMENT_COMMENTS_SELECTED';
export const ASSIGNMENT_COMMENTS_INVALIDATE = 'ASSIGNMENT_COMMENTS_INVALIDATE';

/**
 * Action creators
 */
export function assignmentCommentsRequest(params = {}) {
    return {
        type: ASSIGNMENT_COMMENTS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function assignmentCommentsReceive(items) {

    return {
        type: ASSIGNMENT_COMMENTS_RECEIVE,
        payload: {
            comments: items ? items : []
        }
    };
}

export function assignmentCommentsInvalidate() {
    return {
        type: ASSIGNMENT_COMMENTS_INVALIDATE,
        payload: {
        }
    };
}

export function assignmentCommentsFetch(id, params = {}) {
    return function(dispatch, getState) {
        dispatch(assignmentCommentsRequest(params));

        return service.fetch(id, params)
            .then(response => {
                return dispatch(assignmentCommentsReceive(response.get('items')));
            });
    };
}

export function assignmentCommentsReceiveWebSocket(comments = {}) {
    return function(dispatch, getState) {
        return Promise.all([
            dispatch(assignmentCommentsReceive(comments.get('items')))
        ]);
    };
}

export function assignmentCommentsCreate(id, comment = {}) {
    return function(dispatch, getState) {
        return service.create(id, comment)
            .then(response => {
                return dispatch(assignmentCommentsReceive(response.get('items')));
            });
    }
}