UNPKG

cluedin-widget

Version:

This is the project for creating and managing widgets in CluedIn.

139 lines (113 loc) 3.35 kB
import constants from '../constants' import { getLayout, getMostConnectedData } from '../data/index' import config from '../config' import { unauthorized } from './generic' import userProfile from '../userProfile/index' export function goToLocation( location, param ) { if ( location === "addProvider" ) { window.location = config.location.goToAppProvider(); } if ( location === "addProviderById" ) { window.location = config.location.gotToAppProvider( param ); } if ( location === "goToSearch" ) { window.location = config.location.goToSearch( param ); } if ( location === "entity" ) { window.location = config.location.goToEntity( param ); } if ( location === "goToInviteUser" ) { window.location = config.location.goToInviteUser( param ); } if ( location === "allConnectedProvider" ) { window.location = config.location.goToConnectedProvider(); } return { type: constants.core.GOTOLOCATION } } var requestLayout = () => { return { type: constants.core.REQUEST_LAYOUT }; }; var receiveLayout = ( layout, index ) => { return { type: constants.core.RECEIVE_LAYOUT, data: { layout, index } }; }; var invalidLayout = () => { return { type: constants.core.INVALID_LAYOUT }; }; export function removeWidget( name, option ) { userProfile.removeWidget( option.type, name ); return { type: constants.core.REMOVE_WIDGET, data: { name: name, layoutIndex: option.layoutIndex, tabIndex: option.tabIndex, type: option.type } }; } export function fetchLayout( type, index ) { return function( dispatch ) { dispatch( requestLayout() ); return getLayout( type ).then( function( layout ) { dispatch( receiveLayout( layout, index ) ); } ).catch( unauthorized( dispatch, invalidLayout ) ); }; } export function fetchLayoutIfNeeded( type, index ) { return ( dispatch ) => { return dispatch( fetchLayout( type, index ) ); } } var invalidMostConnected = () => { return ( dispatch, getState ) => { getState() }; }; var requestMostConnected = () => { return { type: constants.core.REQUEST_MOST_CONNECTED }; }; var shouldFetchMostConnected = ( state )=> { return !(state.connectedData && state.connectedData.length > 0); }; var receiveMostConnected = ( connectedData ) => { return { type: constants.core.RECEIVE_MOST_CONNECTED, data: connectedData }; }; var fetchMostConnected = () => { return function( dispatch ) { dispatch( requestMostConnected() ); return getMostConnectedData().then( function( entities ) { dispatch( receiveMostConnected( entities ) ); } ).catch( unauthorized( dispatch, invalidMostConnected ) ); } }; export function fetchMostConnectedIfNeeded() { return ( dispatch, getState ) => { if ( shouldFetchMostConnected( getState() ) ) { return dispatch( fetchMostConnected() ); } }; } export function shouldResetLayout() { return ( dispatch ) => { return dispatch( { type: constants.core.RESET_LAYOUT } ); }; }