cluedin-widget
Version:
This is the project for creating and managing widgets in CluedIn.
55 lines (44 loc) • 1.43 kB
JavaScript
import constants from '../constants';
import { unauthorized } from './generic';
import { getLastDeals } from '../data/deals';
import fake from '../../iso/entity/fake';
var shouldFetchGenericLastDeals = ( state ) => {
return (!state.genericLastDeals || state.genericLastDeals.length === 0);
};
var requestGenericLastDeals = () => {
return {
type: constants.deal.REQUEST_GENERIC_LAST_DEALS
};
};
var receiveGenericLastDeals = ( deals, fake ) => {
return {
type: constants.deal.RECEIVE_GENERIC_LAST_DEALS,
data: {
deals: deals,
isFake: fake
}
};
};
var invalidGenericLastDeals = () => {
return {
type: constants.deal.INVALID_GENERIC_LAST_DEALS
};
};
var fetchGenericLastDeals = () => {
return function( dispatch ) {
dispatch( requestGenericLastDeals() );
return getLastDeals().then( function( resp ) {
if ( !resp || resp.length === 0 ) {
return dispatch( receiveGenericLastDeals( fake.deal || [], true ) );
}
dispatch( receiveGenericLastDeals( resp ) );
} ).catch( unauthorized( dispatch, invalidGenericLastDeals ) );
};
};
export function shouldFetchGenericLastDeals() {
return ( dispatch, getState ) => {
if ( shouldFetchGenericLastDeals( getState() ) ) {
return dispatch( fetchGenericLastDeals() );
}
};
}