UNPKG

cluedin-widget

Version:

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

130 lines (108 loc) 3.13 kB
import constants from '../constants'; import { getUsers, getPotentialUsers, getCurrentUser } from '../data/user'; import { getEntityByName } from '../data/entity'; import { unauthorized } from './generic'; var invalidUsers = () => { return { type: constants.user.INVALID_USERS }; }; var requestUsers = () => { return { type: constants.user.REQUEST_USERS } }; var receiveUsers = ( users ) => { return { type: constants.user.RECEIVE_USERS, data: { users } }; }; var fetchUsers = () => { return function( dispatch ) { dispatch( requestUsers() ); return getUsers().then( function( resp ) { dispatch( receiveUsers( resp.body ) ); } ).catch( unauthorized( dispatch, invalidUsers ) ); }; }; function shouldFetchUsers( state ) { return !( state.user.users && state.user.users.length > 0); } export function fetchUsersIfNeeded() { return ( dispatch, getState ) => { if ( shouldFetchUsers( getState() ) ) { return dispatch( fetchUsers() ) } }; } var requestPotentialUsers = () => { return { type: constants.user.REQUEST_POTENTIAL_USERS }; }; var receivePotentialUsers = ( potentialUsers ) => { return { type: constants.user.RECEIVE_POTENTIAL_USERS, data: potentialUsers } }; var invalidPotentialUsers = () => { return { type: constants.user.INVALID_POTENTIAL_USERS } }; var fetchPotentialUsers = () => { return function( dispatch ) { dispatch( requestPotentialUsers() ); return getPotentialUsers().then( function( users ) { dispatch( receivePotentialUsers( users ) ); } ).catch( unauthorized( dispatch, invalidPotentialUsers ) ); }; }; var shouldFetchPotentialUsers = ( state ) => { return !( state.user.potentialUsers && state.user.potentialUsers.length > 0); }; export function fetchPotentialUsersIfNeeded() { return ( dispatch, getState ) => { if ( shouldFetchPotentialUsers( getState() ) ) { return dispatch( fetchPotentialUsers() ) } }; } var requestCurrentUser = () => { return { type: constants.user.REQUEST_CURRENT_USER }; }; var receiveCurrentUser = ( user ) => { return { type: constants.user.RECEIVE_CURRENT_USER, data: { user } } }; var invalidCurrentUser = () => { return { type: constants.user.INVALID_CURRENT_USER }; }; var fetchCurrentUser = () => { return function( dispatch ) { dispatch( requestCurrentUser() ); return getCurrentUser().then( function( user ) { return getEntityByName( '/Infrastructure/User%23CluedIn:' + user.client.Id ).then( function( userEntity ) { user.entity = userEntity; dispatch( receiveCurrentUser( user ) ); } ); } ).catch( unauthorized( dispatch, invalidCurrentUser ) ); }; }; export function shouldFetchCurrentUser() { return ( dispatch ) => { return dispatch( fetchCurrentUser() ); }; }