cluedin-widget
Version:
This is the project for creating and managing widgets in CluedIn.
55 lines (44 loc) • 1.54 kB
JavaScript
import constants from '../constants';
import { unauthorized } from './generic';
import { getLastDocuments } from '../data/document';
import fake from '../../iso/entity/fake';
var shouldFetchGenericLastDocuments = ( state ) => {
return (!state.genericLastDocuments || state.genericLastDocuments.length === 0);
};
var requestGenericLastDocuments = () => {
return {
type: constants.document.REQUEST_GENERIC_LAST_DOCUMENTS
};
};
var receiveGenericLastDocuments = ( documents, fake ) => {
return {
type: constants.document.RECEIVE_GENERIC_LAST_DOCUMENTS,
data: {
documents: documents,
isFake: fake
}
};
};
var invalidGenericLastDocuments = () => {
return {
type: constants.document.INVALID_GENERIC_LAST_DOCUMENTS
};
};
var fetchGenericLastDocuments = ( id ) => {
return function( dispatch ) {
dispatch( requestGenericLastDocuments() );
return getLastDocuments( id ).then( function( resp ) {
if ( !resp || resp.length === 0 ) {
return dispatch( receiveGenericLastDocuments( fake.document, true ) );
}
dispatch( receiveGenericLastDocuments( resp ) );
} ).catch( unauthorized( dispatch, invalidGenericLastDocuments ) );
};
};
export function shouldFetchGenericLastDocuments( id ) {
return ( dispatch, getState ) => {
if ( shouldFetchGenericLastDocuments( getState() ) ) {
return dispatch( fetchGenericLastDocuments( id ) );
}
};
}