Home Reference Source

application/components/common/simple-editor/service.js

import Immutable from 'immutable';

import alt from './../../../alt';
import Alt from 'alt';
import BaseService from './../../../services/base-service';
import {ErrorService} from './../../../services/error-service';

import {info} from './../../../util/console';

/**
 * Core state service for the simple editor. You really
 * shouldn't ever need to call this on your own.
 * 
 * Ever.
 */
class UnwrappedService extends BaseService {

    retainArrow() {
        return true;
    }

    releaseArrow() {
        return true;
    }

    updateUsers(users) {
        return users;
    }

    /**
     * Run a query on the search endpoint. See search docs for query info
     * @param  {object} opts Query has
     */
    searchUsers(val, opts = {}) {
        if (opts && !opts.type) {
            opts.type = 'user';
        }

        opts.keyword = val;
        
        return (dispatch) => {
            return this.get('/v3/search', opts)
                .then((data) => {
                    info('Search returned', data);
                    this.updateUsers(data.get('items'))
                });
        };
    }

    markSelectedUser() {
        return (dispatch) => {
            return new Promise((resolve, reject) => {
                this.selectUser();
                resolve();
            });
        };
    }

    resetUsers() {
        return (dispatch) => {
            return new Promise((resolve, reject) => {
                this.updateUsers(Immutable.fromJS([]));
                resolve();
            });
        }
    }

    incrementSelectedMention() {
        return true;
    }

    decrementSelectedMention() {
        return true;
    }

    setSelectedUser(i) {
        return i;
    }

    selectUser() {
        return true;
    }
}

const service = alt.createActions(UnwrappedService);

/**
 * SimpleEditor Flux store. Tracks
 * <ul>
 *     <li>referenceCount</li>
 * </ul>
 */
class UnwrappedStore {
    constructor() {
        this.state = {
            'arrowReferenceCount': 0,
            'users': Immutable.Map(),
            'mentionSelectedIndex': 0
        };

        this.bindListeners({
            'handleRetainArrow': service.RETAIN_ARROW,
            'handleReleaseArrow': service.RELEASE_ARROW,

            'handleUpdateUsers': service.UPDATE_USERS,

            'handleIncrementSelectedMention': service.INCREMENT_SELECTED_MENTION,
            'handleDecrementSelectedMention': service.DECREMENT_SELECTED_MENTION,

            'handleSetSelectedUser': service.SET_SELECTED_USER,
            'handleSelectUser': service.SELECT_USER
        });
    }

    handleRetainArrow() {
        this.setState({
            'arrowReferenceCount': this.state.arrowReferenceCount + 1
        });
    }

    handleReleaseArrow() {
        this.setState({
            'arrowReferenceCount': this.state.arrowReferenceCount - 1
        });
    }

    handleUpdateUsers(users) {
        this.setState({'users': users});
    }

    handleIncrementSelectedMention() {
        let index = this.state.mentionSelectedIndex;
        index = index + 1;
        if (index > this.state.users.size - 1) {
            index = this.state.users.size -1;
        }
        
        this.setState({'mentionSelectedIndex': index});
    }

    handleDecrementSelectedMention() {
        let index = this.state.mentionSelectedIndex;
        index = index - 1;
        if (index < 0) {
            index = 0;
        }

        this.setState({'mentionSelectedIndex': index});
    }

    handleSetSelectedUser(i) {
        this.setState({
            'mentionSelectedIndex': i,
            'selectedUser': this.state.users.get(i)
        });
    }

    handleSelectUser() {
        this.setState({'selectedUser': this.state.users.get(this.state.mentionSelectedIndex)});
    }
}

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

/**
 * Instace wrapper if you don't or can't use the singleton action and classes.
 *
 * Actions and store are both under the 'references' key.
 */
class SimpleEditorStore extends Alt {
    constructor(config = {}) {
        super(config);

        this.addActions('references', UnwrappedService);
        this.addStore('references', UnwrappedStore);
    }
}

export default SimpleEditorStore;