cluedin-widget
Version: 
This is the project for creating and managing widgets in CluedIn.
106 lines (85 loc) • 2.62 kB
JavaScript
import constants from '../constants';
import providerApi from '../data/provider';
import { unauthorized } from './generic';
import config from '../config'
var invalidActiveProviders = () => {
    return {
        type: constants.provider.INVALID_PROVIDER
    };
};
var requestActiveProviders = () => {
    return {
        type: constants.provider.REQUEST_ACTIVE_PROVIDERS
    }
};
var receiveActiveProviders = ( providers ) => {
    return {
        type: constants.provider.RECEIVE_ACTIVE_PROVIDERS,
        data: { providers }
    };
};
var fetchActiveProviders = () => {
    return function( dispatch ) {
        dispatch( requestActiveProviders() );
        return providerApi.getCurrentProvider().then( function( resp ) {
            dispatch( receiveActiveProviders( resp.body ) );
        } ).catch( unauthorized( dispatch, invalidActiveProviders ) );
    };
};
function shouldFetchActiveProviders( state ) {
    return !( state.provider.providers && state.provider.providers.length > 0);
}
export function fetchActiveProvidersIfNeeded() {
    return ( dispatch, getState ) => {
        if ( shouldFetchActiveProviders( getState() ) ) {
            return dispatch( fetchActiveProviders() )
        }
    }
}
export function receiveCrawlerStats( crawlerStats ) {
    return {
        type: constants.provider.RECEIVE_CRAWLER_STATS,
        data: { crawlerStats }
    }
}
var requestAllProviders = () => {
    return {
        type: constants.provider.REQUEST_ALL_PROVIDERS
    }
};
var receiveAllProviders = ( allProviders ) => {
    return {
        type: constants.provider.RECEIVE_ALL_PROVIDERS,
        data: {
            allProviders
        }
    };
};
var invalidProviders = () => {
    return {
        type: constants.provider.INVALID_ALL_PROVIDERS
    };
};
var fetchAllProviders = () => {
    return function( dispatch ) {
        dispatch( requestAllProviders() );
        return providerApi.getAllProvider().then( function( resp ) {
            dispatch( receiveAllProviders( resp.body ) );
        } ).catch( unauthorized( dispatch, invalidProviders ) );
    };
};
function shouldFetchProviders( state ) {
    return !( state.provider.allProviders && state.provider.allProviders.length > 0);
}
export function fetchAllProvidersIfNeeded() {
    return ( dispatch, getState ) => {
        if ( shouldFetchProviders( getState() ) ) {
            return dispatch( fetchAllProviders() )
        }
    }
}
export function reAuthProvider( provider ) {
    return providerApi.reAuthProvider( provider ).then( function( url ) {
        config.location.redirectToUrl( url );
    } );
}