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.
176 lines (143 loc) • 4.7 kB
JavaScript
import constants from '../constants';
import { getUsers, getPotentialUsers, getCurrentUser, addWidthConfigToUserProfile } from '../data/user';
import { getUserProfile, saveProfile } from '../data/userProfile';
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;
getUserProfile(userWithEntity.entity.id).then((userProfile) => {
userWithEntity.userProfile = userProfile;
dispatch(receiveCurrentUser(userWithEntity));
}).catch(() => {
userWithEntity.userProfile = void 0;
dispatch(receiveCurrentUser(userWithEntity));
});
});
}).catch(unauthorized(dispatch, invalidCurrentUser));
});
export function shouldFetchCurrentUser() {
return (dispatch) => {
dispatch(fetchCurrentUser());
};
}
const requestRemoveOnBoarding = () => ({
type: constants.user.REQUEST_REMOVE_ON_BOADING,
});
const receiveRemoveOnBoarding = () => ({
type: constants.user.RECEIVE_REMOVE_ON_BOARDING,
});
const invalidRemoveOnBoarding = () => ({
type: constants.user.INVALID_REMOVE_ON_BOARDING,
});
export const removeOnBoarding = (userId, userProfile) => {
let userProfileToSave = userProfile;
return (dispatch) => {
dispatch(requestRemoveOnBoarding());
if (!userProfileToSave) {
userProfileToSave = {
isOnBoardingFinished: true,
userId,
};
} else {
userProfileToSave.isOnBoardingFinished = true;
}
saveProfile(userId, userProfileToSave).then(() => {
dispatch(receiveRemoveOnBoarding());
}).catch(unauthorized(dispatch, invalidRemoveOnBoarding));
};
};
export const requestAddWidgetConfigurationToUserProfile = (widgetToAdd) => ({
type: constants.user.REQUEST_ADD_WIDGET_CONFIGURATION_TO_USER_PROFILE,
widgetToAdd,
});
export const receiveAddWidgetConfigurationToUserProfile = (widgetToAdd) => ({
type: constants.user.RECEIVE_ADD_WIDGET_CONFIGURATION_TO_USER_PROFILE
});
export const invalidAddWidgetConfigurationToUserProfile = () => ({
type: constants.user.INVALID_ADD_WIDGET_CONFIGURATION_TO_USER_PROFILE
});
export const addWidgetConfigurationToUserProfile = (userId, widgetToAdd) => {
return (dispatch) => {
dispatch(requestAddWidgetConfigurationToUserProfile(widgetToAdd));
addWidthConfigToUserProfile(userId, widgetToAdd).then(() => {
receiveAddWidgetConfigurationToUserProfile();
}).catch(unauthorized(dispatch, invalidAddWidgetConfigurationToUserProfile));
};
};