AR
export const useGiphyService = limit => {
const [state, dispatch] = useReducer(gifsReducer, initialState);
const fetch = useCallback(
async (query, offset = 0, isIncremental = false) => {
if (!query) {
return dispatch({
type: FETCH_NEW_GIFS_SUCCESS,
data: [],
isLastPage: true
});
}
dispatch({
type: isIncremental ? FETCH_MORE_GIFS_REQUEST : FETCH_NEW_GIFS_REQUEST
});
try {
const {
data,
pagination: { total_count, offset: newOffset, count }
} = await searchGifs({ query, limit, offset });
const isLastPage = total_count - newOffset <= count;
dispatch({
type: isIncremental
? FETCH_MORE_GIFS_SUCCESS
: FETCH_NEW_GIFS_SUCCESS,
data,
isLastPage
});
} catch {
dispatch({
type: isIncremental ? FETCH_MORE_GIFS_FAILURE : FETCH_NEW_GIFS_FAILURE
});
}
},
[limit]
);
return { state, fetch };
};