Home Reference Source

application/redux/actions/application-actions.js

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

class SettingService extends BaseService {

    fetch(opts) {
        return this.get('/v3/setting', opts);
    }

    update(slug, value) {
        let data = {};
        data[slug] = value;
        return this.put('/v3/setting', data);
    }
}

const service = new SettingService;

/**
 * Action Types
 */
export const APPLICATION_SET_VERSION = 'APPLICATION_SET_VERSION';
export const APPLICATION_NOTIFY_UPDATES = 'APPLICATION_NOTIFY_UPDATES';
export const APPLICATION_DID_NOTIFY_UPDATES = 'APPLICATION_DID_NOTIFY_UPDATES';
export const APPLICATION_IS_CONNECTED = 'APPLICATION_IS_CONNECTED';
export const APPLICATION_IS_DIRTY = 'APPLICATION_IS_DIRTY';
export const APPLICATION_SET_SETTINGS = 'APPLICATION_SET_SETTINGS';
export const APPLICATION_SET_SETPLUGINS = 'APPLICATION_SET_SETPLUGINS';

/**
 * Action Creators
 */

export function applicationSetVersion(ver) {
    return {
        type: APPLICATION_SET_VERSION,
        payload: {
            version: ver
        }
    };
}

export function applicationIsDirty(mcGurt) {
    return {
        type: APPLICATION_IS_DIRTY,
        payload: {
            isDirty: mcGurt
        }
    };
}

export function applicationNotifyUpdates() {
    return {
        type: APPLICATION_NOTIFY_UPDATES,
        payload: {}
    };
}

export function applicationDidNotifyUpdates() {
    return {
        type: APPLICATION_DID_NOTIFY_UPDATES,
        payload: {}
    };
}

export function applicationIsConnected(flag) {
    return {
        type: APPLICATION_IS_CONNECTED,
        payload: {
            connectedState: flag
        }
    };
}

export function applicationSetSettings(settings) {
    return {
        type: APPLICATION_SET_SETTINGS,
        payload: {
            settings: settings
        }
    };
}

export function applicationSetPlugins(plugins) {
    return {
        type: APPLICATION_SET_SETPLUGINS,
        payload: {
            plugins: plugins
        }
    };
}

export function applicationFetchSettings() {
    return function(dispatch, getState) {

        return service.fetch({})
            .then(response => {
                dispatch(applicationSetPlugins(response.get('plugins')));
                return dispatch(applicationSetSettings(response.get('settings')));
            });
    };
}

export function applicationUpdateSetting(slug, value) {
    return function(dispatch, getState) {

        return service.update(slug, value)
            .then(response => {
                dispatch(applicationSetPlugins(response.get('plugins')));
                return dispatch(applicationSetSettings(response.get('settings')));
            });
   }
}