UNPKG

cluedin-widget

Version:

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

89 lines (73 loc) 2.08 kB
import constants from '../constants'; import { getUsers, getPotentialUsers } from '../data/user'; 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() ) } }; }