UNPKG

react-giphy-searchbox-stickers

Version:
44 lines (39 loc) 1.05 kB
import { useReducer } from 'react'; import axios from 'axios'; import dataFetchReducer from '../reducers/dataFetchReducer'; var useApi = function useApi() { var _useReducer = useReducer(dataFetchReducer, { loading: false, error: false, data: [], lastPage: false }), state = _useReducer[0], dispatch = _useReducer[1]; var fetch = function fetch(url, isMore) { if (isMore) { dispatch({ type: 'FETCH_MORE_INIT' }); } else { dispatch({ type: 'FETCH_INIT' }); } axios.get(url).then(function (response) { if (isMore) { dispatch({ type: 'FETCH_MORE_SUCCESS', payload: response.data.data, pagination: response.data.pagination }); } else { dispatch({ type: 'FETCH_SUCCESS', payload: response.data.data, pagination: response.data.pagination }); } }).catch(function () { dispatch({ type: 'FETCH_FAILURE' }); }); }; return [state, fetch]; }; export default useApi;