Skip to content

Instantly share code, notes, and snippets.

@lakshaygupta21
Created August 31, 2020 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshaygupta21/367265237f6d0d1842fa65e9938c330c to your computer and use it in GitHub Desktop.
Save lakshaygupta21/367265237f6d0d1842fa65e9938c330c to your computer and use it in GitHub Desktop.
import axios from 'axios'
import * as RNLocalize from "react-native-localize";
// const domain = "http://localhost:3001"
const domain = "https:apigoogletrends.herokuapp.com"
export const trendData = (trend1, trend2) => async (dispatch) => {
try {
dispatch({
type: 'SENDING'
})
console.log('dispatched')
const country = RNLocalize.getCountry();
const result = await axios.get(`${domain}/${trend1}/${trend2}`)
// const result2 = await axios.get(`${domain}/${trend2}`)
// console.log(result.data)
dispatch({
type: 'TREND_RECEIVED',
payload: result.data
});
} catch (error) {
if (error.response) {
console.log(error.response.data.errors);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
/*
* The request was made but no response was received, `error.request`
* is an instance of XMLHttpRequest in the browser and an instance
* of http.ClientRequest in Node.js
*/
console.log(error.request);
} else {
console.log("Error", error.message);
}
console.log(error);
dispatch({
type: 'TREND_ERROR',
});
}
};
export const getTrending = () => async (dispatch) => {
try {
dispatch({
type: 'TRENDING'
})
console.log('dispatched')
const country = RNLocalize.getCountry();
const result = await axios.get(`${domain}/${country}`)
// const result2 = await axios.get(`${domain}/${trend2}`)
// console.log(JSON.parse(result.data).default.trendingSearchesDays[0].trendingSearches)
dispatch({
type: 'TRENDING_RECEIVED',
payload: result.data
});
} catch (error) {
if (error.response) {
console.log(error.response.data.errors);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
/*
* The request was made but no response was received, `error.request`
* is an instance of XMLHttpRequest in the browser and an instance
* of http.ClientRequest in Node.js
*/
console.log(error.request);
} else {
console.log("Error", error.message);
}
console.log(error);
dispatch({
type: 'TRENDING_ERROR',
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment