SHARE
TWEET

Untitled

a guest Jul 20th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { IAction } from '../types/redux.types';
  2. import { actionTypes as errorTypes, showError } from './errors.actions';
  3.  
  4. interface IPayloadCreators {
  5.   request(...args: any[]): any;
  6.   success(...args: any[]): any;
  7.   failure?(errorMsg: string): any;
  8. }
  9.  
  10. interface IActionCreator {
  11.   types: { request: string; success: string; failure: string; };
  12.   request(...args: any[]): IAction;
  13.   success(...args: any[]): IAction;
  14.   failure(errorMsg: string): IAction;
  15. }
  16.  
  17. const defaultPayload = (args) => args;
  18.  
  19. export default function createAction(type: string, payloadCreators: IPayloadCreators): IActionCreator {
  20.   const requestType = `${type}_REQUEST`;
  21.   const successType = `${type}_SUCCESS`;
  22.   const failureType = payloadCreators.failure ? `${type}_FAILURE` : errorTypes.SHOW_ERROR;
  23.  
  24.   return {
  25.     types: { request: requestType, success: successType, failure: failureType },
  26.     request(...args) {
  27.       return {
  28.         type: requestType,
  29.         payload: payloadCreators.request ? payloadCreators.request(...args) : defaultPayload(args),
  30.       };
  31.     },
  32.     success(...args) {
  33.       return {
  34.         type: successType,
  35.         payload: payloadCreators.success ? payloadCreators.success(...args) : defaultPayload(args),
  36.       };
  37.     },
  38.     failure(errorMsg: string) {
  39.       if (payloadCreators.failure) {
  40.         return {
  41.           type: failureType,
  42.           payload: payloadCreators.failure(errorMsg),
  43.         };
  44.       }
  45.  
  46.       return showError(errorMsg);
  47.     },
  48.   };
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top