summaryrefslogtreecommitdiff
path: root/src/redux/github/actions.js
blob: 7f843c4dcc3954d243b4574a7ca7c64524655702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import axios from 'axios';

import {
  FETCH_TRENDING_FAILED,
  FETCH_TRENDING_SUCCESS,
  PROCESS_FETCH_TRENDING,
  UPDATE_FILTERS
} from './types';

const API_URL = 'https://api.github.com/search/repositories';

/**
 * @param {object} filters
 * @returns {Function}
 */
export const fetchTrending = function (filters) {
  return dispatch => {
    dispatch({ type: PROCESS_FETCH_TRENDING });

    axios.get(API_URL, {
      params: filters
    }).then(response => {
      dispatch({
        type: FETCH_TRENDING_SUCCESS,
        payload: response.data
      });
    }).catch(error => {
      dispatch({
        type: FETCH_TRENDING_FAILED,
        payload: error
      });
    });
  };
};

/**
 * Updates the filters used in queries
 * @param filters
 * @return {Function}
 */
export const updateFilters = function (filters) {
  return dispatch => {
    dispatch({
      type: UPDATE_FILTERS,
      payload: filters
    });
  };
};