Home Reference Source

application/services/tag-service.js

import Immutable from 'immutable';

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

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

/**
 * Interact with tag API service
 */
class TagService extends BaseService {

    updateTag(tag) {
        return tag;
    }
    
    updateTags(tags) {
        return tags;
    }
    
    updatePagination(pagination) {
        return pagination;
    }
    
    reset() {
        return (dispatch) => {
            setTimeout(() => {
                this.updateTags(Immutable.fromJS([]));
            }, 1);
        }
    }

    /**
     * Run a query on the search endpoint. See search docs for query info
     * @param  {object} opts Query has
     */
    search(opts) {
        if (opts && !opts.type) {
            opts.type = 'tag';
        }
        
        return (dispatch) => {
            return this.get('/v3/search', opts)
                .then((data) => {
                    info('Search returned', data);
                    this.updateTags(data.get('items'))
                });
        };
    }

    /**
     * Remove (delete) tag
     * @param  {string} id
     */
    remove(id) {
        return (dispatch) => {
            return this.delete('/v3/tag/' + id)
                .then(() => {
                    info(arguments);
                })
                .catch((e) => {
                    ErrorService.add('error', 'Unable to delete tag, usually this means you don\'t have permission to remove items.');
                });
        };
    }

    /**
     * Update existing tag
     * @param  {string}   id       UUID
     * @param  {object}   data     Hash of tag data
     * @param  {Function} callback
     */
    update(id, data, callback) {
        return (dispatch) => {
            return this.put('/v3/tag/' + id, data)
                .then((data) => {
                    info('Update returned', data);
                    this.updateTag(data.first());
                    if (callback) {
                        callback(data.first());
                    }
                })
                .catch((e) => {
                    ErrorService.add('error', 'Unable to save tag, usually this means you don\'t have it checked out.');
                });
        }
    }

    /**
     * Create new tag
     * @param  {object}   data     Hash of content data
     * @param  {Function} callback
     */
    create(data, callback) {
        return (dispatch) => {
            return this.post('/v3/tag/', data)
                .then((data) => {
                    info('Create returned', data);
                    this.updateTag(data.first());
                    if (callback) {
                        callback(data.first());
                    }
                })
                .catch((e) => {
                    ErrorService.add('error', 'Unable to save tag, usually this means you don\'t have it checked out.');
                });
        };
    }

    /**
     * Fetch single tag
     * @param  {string}  id          UUID
     * @param  {object}  opts        Hash of query options
     * @param  {Boolean} forceUpdate If true (default) force update to store, otherwise, don't
     */
    fetchOne(id, opts, forceUpdate = true) {
        if (forceUpdate === true) {
            this.updateTag([]);
        }
        return (dispatch) => {
            return this.get('/v3/tag/' + id, opts)
                .then((data) => {
                    info('FetchOne returned', data);
                    this.updateTag(data.first());
                });
        };
    }

    /**
     * Fetch paginated tag items
     * @param  {object} opts Hash of query options
     */
    fetch(opts) {
        return (dispatch) => {
            return this.get('/v3/tag', opts)
                .then((data) => {
                    info('Fetch returned', data);
                    this.updatePagination(Immutable.fromJS({
                        'first': data.get('first'),
                        'before': data.get('before'),
                        'current': data.get('current'),
                        'last': data.get('last'),
                        'next': data.get('next'),
                        'total_pages': data.get('total_pages'),
                        'total_items': data.get('total_items'),
                        'limit': data.get('limit')
                    }));
                    
                    if (data.get('items').size) {
                        this.updateTags(data.get('items'))
                    } else {
                        this.updateTags(Immutable.fromJS([]));
                    }
                });
        };
    }
}

const service = alt.createActions(TagService);

/**
 * Tag Flux store. Tracks
 * <ul>
 *     <li>tag</li>
 * </ul>
 */
class TagsStore {
    constructor() {
        this.state = {
            'tag': Immutable.Map(),
            'tags': Immutable.Map(),
            'pagination': Immutable.Map()
        };

        this.bindListeners({
            'handleUpdateTag': service.UPDATE_TAG,
            'handleUpdateTags': service.UPDATE_TAGS,
            'handleUpdatePagination': service.UPDATE_PAGINATION
        });
    }

    handleUpdateTag(tag) {
        this.setState({'tag': tag});
    }

    handleUpdateTags(tags) {
        this.setState({'tags': tags});
    }

    handleUpdatePagination(pagination) {
        this.setState({'pagination': pagination});
    }

}

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

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

        this.addActions('tags', TagService);
        this.addStore('tags', TagsStore);
    }
}

export {TagFlux as default, service as TagService, store as TagsStore};