Skip to content

Commit

Permalink
Merge pull request #10 from wylkon/fix/api-fetch
Browse files Browse the repository at this point in the history
Fix/api fetch
  • Loading branch information
ashokdey authored Oct 4, 2019
2 parents e27505f + 3da7307 commit 5fa1faa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
14 changes: 8 additions & 6 deletions src/HandleAPICalls/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { takeEvery, call } from 'redux-saga/effects';
import { toastr } from 'react-redux-toastr';
import { HANDLE_API_CALLS } from './constants';


// function that makes the api request and returns a Promise for response
function callToAPI(method, url, data) {
// console.log(params);
return axios({
method,
url,
Expand All @@ -17,11 +15,17 @@ function callToAPI(method, url, data) {
// worker saga: makes the api call when watcher saga sees the action
function* handleAPICalls(action) {
try {
const response = yield call(callToAPI, action.method, action.url, action.body);
const response = yield call(
callToAPI,
action.method,
action.url,
action.body
);
if (action.handleSuccess) {
yield call(action.handleSuccess, response.data);
}
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
if (!action.showToast) {
toastr.error('ERROR', 'Failed to request');
Expand All @@ -38,8 +42,6 @@ function* callToAPIWatcher() {
}

/* eslint-disable */
export {
callToAPIWatcher,
};
export { callToAPIWatcher };

/* eslint-enable */
29 changes: 7 additions & 22 deletions src/RandomQuote/container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,20 @@ class RamdomQuoteContainer extends Component {
this.props.clearQuote();

this.props.getDataFromAPI(
'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=',
'https://api.quotable.io/random',
'GET',
undefined,
(response) => {
// set the quote in state
const dataObject = response[0];
/**
* refer here for more about this regex:
* https://stackoverflow.com/questions/1499889/remove-html-tags-in-javascript-with-regex
*/
const filterHTMLRegex = /(<([^>]+)>)/ig;
const author = dataObject.title;
const quote = unescape(dataObject.content.replace(filterHTMLRegex, '')).replace(/\r?\n|\r/g, '');

this.props.setQuote(quote, author);
{ per_page: 1, orderby: 'rand' },
({ author, content }) => {
this.props.setQuote(content, author);
this.props.disableButton(false);
},
err => console.log(err),
}
// err => console.log(err),
);
}

render() {
const quote = this.props.quote;
return (
<RamdomQuote
getQuote={this.getQuote}
quote={quote}
/>
);
return <RamdomQuote getQuote={this.getQuote} quote={quote} />;
}
}

Expand Down

0 comments on commit 5fa1faa

Please sign in to comment.