Home Reference Source

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

import BaseService from './../../services/base-service';

class NoteCommentService extends BaseService {

    fetch(ref, opts) {
        return this.get('/v3/note-comment/' + ref, opts);
    }

    fetchOne(id) {
        return this.get('/v3/note-comment/' + id);
    }

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

    update(id, data) {
        return this.put('/v3/note-comment/' + id, data);
    }

    remove(uuid) {
        return this.delete('/v3/note-comment/' + uuid);
    }
}

const service = new NoteCommentService;

/**
 * Action types
 */
export const NOTE_COMMENTS_REQUEST = 'NOTE_COMMENTS_REQUEST';
export const NOTE_COMMENT_RECEIVE = 'NOTE_COMMENT_RECEIVE';
export const NOTE_COMMENTS_RECEIVE = 'NOTE_COMMENTS_RECEIVE';
export const NOTE_COMMENT_SELECTED = 'NOTE_COMMENT_SELECTED';
export const NOTE_COMMENT_INVALIDATE = 'NOTE_COMMENT_INVALIDATE';
export const NOTE_COMMENTS_IS_FETCHING = 'NOTE_COMMENTS_IS_FETCHING';

export function noteCommentsIsFetching(isFetching) {
    return {
        type: NOTE_COMMENTS_IS_FETCHING,
        payload: {
            isFetching: isFetching
        }
    };
}

export function noteCommentsRequest(params = {}) {
    return {
        type: NOTE_COMMENTS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function noteCommentsReceive(items) {

    return {
        type: NOTE_COMMENTS_RECEIVE,
        payload: {
            noteComments: items ? items : []
        }
    };
}

export function noteCommentReceive(noteComment) {
    return {
        type: NOTE_COMMENT_RECEIVE,
        payload: {
            noteComment: noteComment ? noteComment : {}
        }
    };
}

export function noteCommentSetSelected(noteComment) {
    return {
        type: NOTE_COMMENT_SELECTED,
        payload: {
            selected: noteComment ? noteComment : false
        }
    };
}

export function noteCommentsInvalidate() {
    return {
        type: NOTE_COMMENTS_INVALIDATE,
        payload: {
        }
    };
}

export function noteCommentUpdate(id, data) {
    return function(dispatch, getState) {
        dispatch(noteCommentsRequest({}));

        return service.update(id, data)
            .then(response => {
                return dispatch(noteCommentReceive(response.first()));
            });
   }
}

export function noteCommentCreate(ref, data) {
    return function(dispatch, getState) {
        dispatch(noteCommentsIsFetching(true));
        dispatch(noteCommentsRequest({}));

        return service.create(ref, data)
            .then(response => {
                return dispatch(noteCommentsIsFetching(false));
                // return dispatch(noteCommentsReceive(response.get('items')));
            });
   }
}

export function noteCommentsMaybeFetch(params = {}) {
    return function(dispatch, getState) {
        const noteComments = getState().noteComments.items;

        if (getState().noteComments.isFetching) {
            return Promise.resolve();
        }

        if (!noteComments.items || !noteComments.items.size) {
            return dispatch(noteCommentsFetch(params));
        }

        return Promise.resolve();
    }
};

export function noteCommentsFetch(ref, params = {}) {
    return function(dispatch, getState) {
        dispatch(noteCommentsIsFetching(true));
        dispatch(noteCommentsRequest(params));

        params.per_page = params.per_page ? params.per_page : 20;
        params.order = params.order ? params.order : "name";
        params.dir = params.dir ? params.dir : "asc";

        return service.fetch(ref, params)
            .then(response => {
                dispatch(noteCommentsIsFetching(false));
                return dispatch(noteCommentsReceive(response.get('items')));
            });
    };
}

export function noteCommentsFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(noteCommentsRequest({}));

        return service.fetchOne(id)
            .then(response => {
                return dispatch(noteCommentReceive(response.first()));
            });
    }
}

export function noteCommentsRemove(uuid) {
    return function (dispatch, getState) {
        dispatch(noteCommentsInvalidate());

        return service.remove(uuid);
    }
}