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.
244 lines (199 loc) • 5.71 kB
JavaScript
import constants from '../constants';
import { unauthorized } from './generic';
import {
getEntity,
getLatest,
getSuggestedSearch,
getEntityByEmail,
getEntityForSalesForce,
getLatestEntities,
getSchema
} from '../data/entity';
const requestEntity = () => ({
type: constants.entity.REQUEST_ENTITY,
});
const receiveEntity = (entity, addToEntities) => ({
type: constants.entity.RECEIVE_ENTITY,
data: {
entity,
addToEntities,
},
});
const invalidEntity = () => ({
type: constants.entity.INVALID_ENTITY,
});
const shouldFetchEntity = (state, id) => (
!state.entity.selectedEntity || state.entity.selectedEntity.id !== id
);
const fetchEntity = (state, id) => ((dispatch) => {
const entityAlreadyFetch = state.entity.entities[id];
if (entityAlreadyFetch) {
return dispatch(receiveEntity(entityAlreadyFetch));
}
dispatch(requestEntity(id));
return getEntity(id).then((resp) => {
if (!resp) {
return dispatch(invalidEntity());
}
return dispatch(receiveEntity(resp, true));
}).catch(unauthorized(dispatch, invalidEntity));
});
const requestGenericLatest = (type) => ({
type: constants.entity.REQUEST_GENERIC_LATEST,
data: {
type,
},
});
const invalidGenericLatest = () => ({
type: constants.entity.INVALID_GENERIC_LATEST,
});
const receiveGenericLatest = (entities, type) => ({
type: constants.entity.RECEIVE_GENERIC_LATEST,
data: {
entities,
type,
},
});
export function shouldFetchGenericLatest(type, id) {
return (dispatch) => {
dispatch(requestGenericLatest(type));
return getLatest(type, id).then((resp) => {
if (!resp) {
return dispatch(invalidGenericLatest());
}
return dispatch(receiveGenericLatest(resp, type));
}).catch(unauthorized(dispatch, invalidEntity));
};
}
const requestSuggestedSearch = (search, index, pageNumber) => ({
type: constants.entity.REQUEST_SUGGESTED_SEARCH,
data: {
searchQuery: search,
index,
pageNumber: pageNumber,
},
});
const invalidSuggestedSearch = (search, index) => ({
type: constants.entity.INVALID_SUGGESTED_SEARCH,
data: {
searchQuery: search,
index,
},
});
const receiveSuggestedSearch = (entities, search, index, pageNumber) => ({
type: constants.entity.RECEIVE_SUGGESTED_SEARCH,
data: {
searchQuery: search,
entities,
index,
pageNumber: (pageNumber + 1 ),
},
});
export function shouldFetchSuggestedSearch(search, index) {
return (dispatch, getState) => {
const pageNumber = getState().entity.currentSuggestedSearchesPageNumber[index];
dispatch(requestSuggestedSearch(search, index, pageNumber));
getSuggestedSearch(search, pageNumber || 0).then((resp) => {
if (!resp) {
return dispatch(invalidSuggestedSearch());
}
return dispatch(receiveSuggestedSearch(resp, search, index, pageNumber));
}).catch(unauthorized(dispatch, invalidSuggestedSearch));
};
}
export function shouldFetchEntityIfNeeded(id) {
return (dispatch, getState) => {
if (shouldFetchEntity(getState(), id)) {
dispatch(fetchEntity(getState(), id));
}
};
}
const requestEntityByEmail = (email) => ({
type: constants.entity.REQUEST_ENTITY_BY_EMAIL,
data: {
email,
},
});
const receiveByEmail = (entity) => ({
type: constants.entity.RECEIVE_ENTITY_BY_EMAIL,
data: {
entity,
},
});
const invalidEntityByEmail = (email) => ({
type: constants.entity.INVALID_ENTITY_BY_EMAIL,
data: {
email,
},
});
export function fetchEntityByEmail(email) {
return (dispatch) => {
dispatch(requestEntityByEmail());
return getEntityByEmail(email).then((entity) => {
dispatch(receiveByEmail(entity));
}).catch(unauthorized(dispatch, invalidEntityByEmail));
};
}
const requestEntityForSalesForce = () => ({
type: constants.entity.REQUEST_ENTITY_FOR_SALESFORCE,
});
const receiveEntityForSalesForce = (entity) => ({
type: constants.entity.RECEIVE_ENTITY_FOR_SALESFORCE,
data: {
entity,
},
});
const invalidEntitySalesForce = () => ({
type: constants.entity.INVALID_ENTITY_FOR_SALESFORCE,
});
export function fetchEntityForSalesForce(type, origin, value) {
return (dispatch) => {
dispatch(requestEntityForSalesForce());
return getEntityForSalesForce(type, origin, value).then((entity) => {
dispatch(receiveEntityForSalesForce(entity));
}).catch(unauthorized(dispatch, invalidEntitySalesForce));
}
}
const requestGenericLastEntities = (timestamp) => ({
type: constants.entity.REQUEST_GENERIC_LAST_ENTITIES,
data: {
timestamp,
},
});
const receiveGenericLastEntities = (entities, timestamp) => ({
type: constants.entity.RECEIVE_GENERIC_LAST_ENTITIES,
data: {
entities,
timestamp,
},
});
const invalidGenericLastEntities = () => ({
type: constants.entity.INVALID_GENERIC_LAST_ENTITIES,
});
export function fetchGenericLast(entityId, entityType, timestamp) {
return (dispatch) => {
dispatch(requestGenericLastEntities(timestamp));
return getLatestEntities(entityId, entityType).then((entity) => {
dispatch(receiveGenericLastEntities(entity, timestamp));
}).catch(unauthorized(dispatch, invalidGenericLastEntities));
};
};
const requestSchemaForEntities = () => ({
type: constants.entity.REQUEST_ENTITY_SCHEMA,
});
const receiveSchemaForEntities = (schema) => ({
type: constants.entity.RECEIVE_ENTITY_SCHEMA,
data: {
schema,
},
});
export function shouldFetchScema() {
return (dispatch, state) => {
dispatch(requestSchemaForEntities());
if (!state().entity.schema) {
return getSchema().then((schema) => {
dispatch(receiveSchemaForEntities(schema));
});
}
};
}