UNPKG

cluedin-widget

Version:

This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.

114 lines (92 loc) 2.67 kB
import constants from '../constants'; import { getUsers, getPotentialUsers, getCurrentUser } from '../data/user'; import { getEntityByName } from '../data/entity'; import { unauthorized } from './generic'; const invalidUsers = () => ({ type: constants.user.INVALID_USERS, }); const requestUsers = () => ({ type: constants.user.REQUEST_USERS, }); const receiveUsers = (users) => ({ type: constants.user.RECEIVE_USERS, data: { users, }, }); const fetchUsers = () => ( (dispatch) => { dispatch(requestUsers()); getUsers().then((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())) { dispatch(fetchUsers()); } }; } const requestPotentialUsers = () => ({ type: constants.user.REQUEST_POTENTIAL_USERS, }); const receivePotentialUsers = (potentialUsers) => ({ type: constants.user.RECEIVE_POTENTIAL_USERS, data: potentialUsers, }); const invalidPotentialUsers = () => ({ type: constants.user.INVALID_POTENTIAL_USERS, }); const fetchPotentialUsers = () => ( (dispatch) => { dispatch(requestPotentialUsers()); return getPotentialUsers().then((users) => { dispatch(receivePotentialUsers(users)); }).catch(unauthorized(dispatch, invalidPotentialUsers)); } ); const shouldFetchPotentialUsers = (state) => ( !(state.user.potentialUsers && state.user.potentialUsers.length > 0) ); export function fetchPotentialUsersIfNeeded() { return (dispatch, getState) => { if (shouldFetchPotentialUsers(getState())) { dispatch(fetchPotentialUsers()); } }; } const requestCurrentUser = () => ({ type: constants.user.REQUEST_CURRENT_USER, }); const receiveCurrentUser = (user) => ({ type: constants.user.RECEIVE_CURRENT_USER, data: { user, }, }); const invalidCurrentUser = () => ({ type: constants.user.INVALID_CURRENT_USER, }); const fetchCurrentUser = () => ((dispatch, getState) => { if (getState().user.currentUser) { return; } dispatch(requestCurrentUser()); getCurrentUser().then((user) => { const userWithEntity = user; getEntityByName(`/Infrastructure/User%23CluedIn:${user.client.Id}`).then((userEntity) => { userWithEntity.entity = userEntity; dispatch(receiveCurrentUser(userWithEntity)); }); }).catch(unauthorized(dispatch, invalidCurrentUser)); }); export function shouldFetchCurrentUser() { return (dispatch) => { dispatch(fetchCurrentUser()); }; }