const commentsFetchEpic: Epic<IPayloadAction> = (actions$) => actions$.pipe( ofType(commentsActions.fetch.started.type), mergeMap((action: IPayloadAction) => concat( of(commentsActions.loading({ isLoading: true })), ajax.getJSON(action.payload.url).pipe( map((comments: IComments[]) => commentsActions.fetch.done({ params: action.payload.url, result: { comments } })), catchError(_ => of(commentsActions.fetch.failed({ params: action.payload.url, error: { hasError: true } }))), ), of(commentsActions.loading({ isLoading: false })), )), )
// match all actions of type SEARCH_USERS action$.ofType(SEARCH_USERS) // we've matched - we'll use RxJs 'debounce' to ensure we don't spam Github with excessive requests .debounceTime(800) // we can now reach out to the Github API .mergeMap(action => ajax.getJSON(`https://api.github.com/search/users?q=${action.payload}`) // we can now return our response to be actioned by our reducers .map(payload => ({ type: SEARCH_USERS_FULFILLED, payload })) )
const loadInitDataPhones = () => { return ajax.getJSON(`${apiContextPath}/assets/json/phones/phones.json`).pipe( catchError(error => { return of({ error: true, payload: error.xhr, }); }) ); }
export function fetchUserEpic(action$) { return action$.ofType(FETCH_USER) .switchMap(({payload}) => { return Observable.ajax.getJSON(`https://api.github.com/users/${payload}`) .map(user => { return fetchUserFulfilledAction(user); }) }) }
const fetchUserEpic = action$ => ( action$.ofType(types.FETCH_USER) .mergeMap(({ value }) => ( ajax.getJSON(`https://api.github.com/users/${value}`) .map(creators.fetchUserFullfilled) .takeUntil(action$.ofType(types.FETCH_USER_CANCELLED)) .catch(err => Observable.of(creators.fetchRejected(err))) )) )
const callSearchWxAPI = (errorCase) => (action$) => { return action$.pipe( switchMap( (action) => ajax.getJSON(`${WX_API_DOMAIN}${WX_PATH}${API_KEY}&zip=${action.payload},${countryCode}${tempUnits}`) .pipe( map( response => fetchWxFulfilled(response)), catchError( payload => handleError(errorCase, payload)) ) )); }
const listCommitsEpic = action$ => ( action$.ofType(types.LIST_COMMITS) .mergeMap(({ apiUrl }) => ( ajax.getJSON(`${apiUrl}?per_page=100`) .map((response: any[]) => response.map(({ commit, comments_url }) => ({ message: commit.message, timeStamp: new Date(commit.author.date).toLocaleDateString(), dateStamp: new Date(commit.author.date).toLocaleTimeString(), url: comments_url, }))) .map(creators.listCommitsFullfilled) )) )
const pingEpic = action$ => ( action$.ofType(types.PING) .mergeMap(() => ( ajax.getJSON('http://localhost:3500/api/ping') .mapTo({ type: types.PONG }) )) )
const fetchUserEpic = action$ => ( action$.ofType(FETCH_USER) .debounceTime(500) .mergeMap(action => ajax .getJSON(`https://api.github.com/users/${action.payload}`) .map(fetchUserFulfilled) .takeUntil(action$.ofType(FETCH_USER)) .catch(error => Observable.of(fetchUserError(error))) ) )
const userEpic = action$ => action$.pipe( ofType(FETCH_USER), mergeMap(action => ajax.getJSON(`https://api.github.com/users/${action.payload}`).pipe( map(response => fetchUserSuccess(response)) )) )
//epic const loadUserEpic = action$ => action$.ofType(LOAD_USER) .mergeMap(action => ajax.getJSON(`https://api.github.com/users/${action.payload}`) .map(response => { console.log('Network output' + response); return loadUserSuccess(response) }) )
const beepEpic = action$ => ( action$.ofType(types.BEEP) .mergeMap(() => ( ajax.getJSON('http://localhost:3500/api/ping') .mapTo({ type: types.BOOP }) )) )
const fetchUserEpic: Epic<Action, LifeStore> = (action$: ActionsObservable<Action>) => action$.ofType('GET_SOME_DATA') .mergeMap((action: Action) => ajax.getJSON(`https://api.github.com/users/${action.name}`) .map(response => getSuccess(response)) )
const getHourlyWxAPI = (errorCase) => (action$) => { return action$.pipe( switchMap( (action) => ajax.getJSON(`${WX_API_DOMAIN}${FORCAST_PATH}${API_KEY}&zip=${action.payload},${countryCode}${tempUnits}`) .pipe( map( response => fetchHourlyWxFulfilled(response)), catchError( payload => handleError(errorCase, payload)) ) )); }
const loadInitDataSelectedPhone = () => { return ajax .getJSON( `${apiContextPath}/assets/json/phones/motorola-xoom-with-wi-fi.json` ) .pipe( catchError(error => { return of({ error: true, payload: error.xhr, }); }) ); }