Last active
February 16, 2018 13:17
-
-
Save mohandere/8fb42d9aee070df71826a04677aa5357 to your computer and use it in GitHub Desktop.
Epic getProducts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ajax | |
} from 'rxjs/observable/dom/ajax'; | |
import 'rxjs/add/operator/catch' | |
import 'rxjs/add/operator/debounceTime' | |
import queryString from 'query-string' | |
import { | |
Observable | |
} from 'rxjs/Observable'; | |
import { | |
REQUEST_PRODUCTS, | |
CANCEL_PRODUCTS_REQUEST, | |
PRODUCTS_REQUEST_FAILED | |
} from '../actions/actionTypes'; | |
import { | |
productsRequestFulfilled, | |
productsRequestFailed | |
} from '../actions/doFetchProducts'; | |
export default function epicGetProducts(action$, store) { | |
return action$.ofType(REQUEST_PRODUCTS) | |
.debounceTime(500) | |
.map(function(action) { | |
let search = store.getState().catalog.search | |
let searchString = queryString.stringify(search) | |
return Object.assign(action, { | |
searchString | |
}) | |
}) | |
.mergeMap(action => | |
ajax.get(`/products?${action.searchString}`) | |
.map(response => productsRequestFulfilled(response)) | |
.catch(error => productsRequestFailed(error)) | |
.takeUntil(action$.ofType(CANCEL_PRODUCTS_REQUEST)) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment