Home Reference Source

application/services/noteComment-service.js

import Immutable from 'immutable';

import alt from './../alt';
import Alt from 'alt';
import BaseService from './base-service';

/**
 * Note comments are fetched from API and injected into the NoteViewer. Again,
 * you shouldn't need to interactio with this component outside the NoteViewer.
 */
class NoteCommentService extends BaseService {

    updateComments(comments) {
        return comments;
    }

    createAndFetch(id, data, callback) {
        return (dispatch) => {
            return this.post('/v3/note-comment/' + id, data)
                .then((data) => {
                    this.updateComments(data.get('items'));
                    if (callback) {
                        callback();
                    }
                });
        }
    }

    create(id, data, callback) {
        return (dispatch) => {
            return this.post('/v3/note-comment/' + id, data)
                .then((data) => {
                    if (callback) {
                        callback();
                    }
                });
        }
    }

    fetch(id, opts) {
        return (dispatch) => {
            return this.get('/v3/note-comment/' + id, opts)
                .then((data) => {
                    this.updateComments(data.get('items'))
                });
        };
    }

}

const service = alt.createActions(NoteCommentService);

class NoteCommentsStore {
    constructor() {
        this.state = {
            'comments': Immutable.Map()
        };

        this.bindListeners({
            'handleUpdateComments': service.UPDATE_COMMENTS,
        });
    }

    handleUpdateComments(comments) {
        this.setState({'comments': comments});
    }
}

const store = alt.createStore(NoteCommentsStore, 'NoteCommentsStore');

/**
 * Instance flux because multiple parent notes can appear on the screen
 */
class NoteCommentFlux extends Alt {
    constructor(config = {}) {
        super(config);

        this.addActions('noteComments', NoteCommentService);
        this.addStore('noteComments', NoteCommentsStore);
    }
}

export {NoteCommentFlux as default, service as NoteCommentService, store as NoteCommentsStore};