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.
83 lines (66 loc) • 2.32 kB
JavaScript
import constants from '../constants';
import { unauthorized } from './generic';
import { getLastOrganizations, current, getById } from '../data/organization';
import entityFake from '../../iso/entity/fake';
const requestGenericLastOrganizations = (timeStamp) => ({
type: constants.organization.REQUEST_GENERIC_LAST_ORGANIZATIONS,
data: {
timeStamp,
},
});
const receiveGenericLastOrganizations = (orgs, page, fake, timeStamp) => {
const pageNumber = page || 0;
return {
type: constants.organization.RECEIVE_GENERIC_LAST_ORGANIZATIONS,
data: {
organizations: orgs,
fake,
pageNumber: (pageNumber + 1),
timeStamp,
},
};
};
const invalidGenericLastOrganizations = () => ({
type: constants.organization.INVALID_GENERIC_LAST_ORGANIZATIONS,
});
const requestCurrentOrganization = () => ({
type: constants.organization.REQUEST_CURRENT_ORGANIZATION,
});
const receiveCurrentOrganization = (org) => ({
type: constants.organization.RECEIVE_CURRENT_ORGANIZATION,
data: {
org,
},
});
const invalidCurrentOrganization = () => ({
type: constants.organization.INVALID_CURRENT_ORGANIZATION,
});
export function fetchCurrentOrganization() {
return (dispatch, getState) => {
if (getState().organization.currentOrganization) {
return;
}
dispatch(requestCurrentOrganization());
current().then((org) => {
getById(org.Id).then((orgEntity) => {
const orgReceived = org;
orgReceived.entity = orgEntity;
dispatch(receiveCurrentOrganization(orgReceived));
});
}).catch(unauthorized(dispatch, invalidCurrentOrganization));
};
}
export function fetchGenericLastOrganizations(id, timeStamp) {
return (dispatch, getState) => {
dispatch(requestGenericLastOrganizations(timeStamp));
const pageNumber = getState().organization.pageNumber;
return getLastOrganizations(pageNumber || 0, id).then((resp) => {
if (!resp || resp.length === 0 && (!pageNumber || pageNumber === 0)) {
return dispatch(
receiveGenericLastOrganizations(entityFake.organization, pageNumber, true, timeStamp)
);
}
return dispatch(receiveGenericLastOrganizations(resp, pageNumber, false, timeStamp));
}).catch(unauthorized(dispatch, invalidGenericLastOrganizations));
};
}