Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import {ActionsObservable} from "redux-observable";
  2. import {AjaxError} from "rxjs/Rx";
  3. import "rxjs/Rx";
  4.  
  5. export const USER_LOAD_REQUEST = "example/USER_LOAD_REQUEST";
  6.  
  7. export const USER_LOAD_RESULT = "example/USER_LOAD_RESULT";
  8.  
  9. export const USER_LOAD_ERROR = "example/USER_LOAD_ERROR";
  10.  
  11. export interface IUserResult {
  12. id: string;
  13. name: string;
  14. }
  15.  
  16. export interface ICustomAjaxError {
  17. type: string;
  18. message: string;
  19. }
  20.  
  21. export const loadUser = (userid: string) => ({
  22. type: USER_LOAD_REQUEST,
  23. userid
  24. });
  25.  
  26. export const loadUserResult = (results: IUserResult) => ({
  27. type: USER_LOAD_RESULT,
  28. results
  29. });
  30.  
  31. export const loadFailure = (message: string): ICustomAjaxError => ({
  32. type: USER_LOAD_ERROR,
  33. message
  34. });
  35.  
  36.  
  37. export const loadUserEpic = (action$: ActionsObservable<any>, store, {getJSON}) => {
  38. return action$.ofType(USER_LOAD_REQUEST)
  39. .do(() => console.log("Locating User ...")) // debugging
  40. .mergeMap(action =>
  41. getJSON(`%PUBLIC_URL%/api/users`)
  42. .map(response => loadUserResult(response as any))
  43. .catch((error: AjaxError): ActionsObservable<ICustomAjaxError> =>
  44. ActionsObservable.of(loadFailure(
  45. `An error occurred: ${error.message}`
  46. )))
  47. );
  48.  
  49. };
  50.  
  51. export default loadUserEpic;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement