UNPKG

cluedin-widget

Version:

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

132 lines (107 loc) 3.06 kB
import constants from '../constants'; import { getLayout, getMostConnectedData } from '../data/index'; import config from '../config'; import { unauthorized } from './generic'; import localStorage from 'store'; export function goToLocation( location, param ) { if ( location === "addProvider" ) { window.location = config.location.goToAppProvider(); } 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(); } return { type: constants.core.GOTOLOCATION } } var requestLayout = () => { return { type: constants.core.REQUEST_LAYOUT }; }; var receiveLayout = ( layout ) => { return { type: constants.core.RECEIVE_LAYOUT, data: { layout: layout } }; }; var invalidLayout = () => { return { type: constants.core.INVALID_LAYOUT }; }; export function removeWidget( name ) { var excludes = localStorage.get( 'excludes' ) || []; if ( excludes.indexOf( name ) === -1 ) { excludes.push( name ); localStorage.set( 'excludes', excludes ); } return { type: constants.core.REMOVE_WIDGET, data: { name: name } }; } export function fetchLayout( type ) { return function( dispatch ) { dispatch( requestLayout() ); return getLayout( type ).then( function( layout ) { dispatch( receiveLayout( layout ) ); } ).catch( unauthorized( dispatch, invalidLayout ) ); }; } export function fetchLayoutIfNeeded( type ) { return ( dispatch ) => { return dispatch( fetchLayout( type ) ); } } 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 } ); }; }