From 02614f5ee9c6ad3389a7d43bdbeba6ec6e88659b Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 19 Aug 2018 21:58:30 +0400 Subject: Add axios and reducer for github --- src/redux/github/actions.js | 33 +++++++++++++++++++++++++++++++++ src/redux/github/reducer.js | 33 +++++++++++++++++++++++++++++++++ src/redux/github/types.js | 3 +++ src/redux/preference/types.js | 6 +++--- 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/redux/github/actions.js create mode 100644 src/redux/github/reducer.js create mode 100644 src/redux/github/types.js (limited to 'src/redux') diff --git a/src/redux/github/actions.js b/src/redux/github/actions.js new file mode 100644 index 0000000..2db93ce --- /dev/null +++ b/src/redux/github/actions.js @@ -0,0 +1,33 @@ +import axios from 'axios'; + +import { + FETCH_TRENDING_FAILED, + FETCH_TRENDING_SUCCESS, + PROCESS_FETCH_TRENDING +} 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 + }); + }); + }; +}; diff --git a/src/redux/github/reducer.js b/src/redux/github/reducer.js new file mode 100644 index 0000000..c752e36 --- /dev/null +++ b/src/redux/github/reducer.js @@ -0,0 +1,33 @@ +import { FETCH_TRENDING_FAILED, FETCH_TRENDING_SUCCESS, PROCESS_FETCH_TRENDING } from './types'; + +export const initialState = { + processing: false, + error: null, + data: [], +}; + +export default function reducer(state = initialState, action) { + switch (action.type) { + case PROCESS_FETCH_TRENDING: + return { + ...state, + processing: true, + error: null + }; + case FETCH_TRENDING_SUCCESS: + return { + ...state, + data: action.payload, + processing: false, + error: null + }; + case FETCH_TRENDING_FAILED: + return { + ...state, + process: false, + error: action.payload + }; + default: + return state; + } +} diff --git a/src/redux/github/types.js b/src/redux/github/types.js new file mode 100644 index 0000000..a345496 --- /dev/null +++ b/src/redux/github/types.js @@ -0,0 +1,3 @@ +export const PROCESS_FETCH_TRENDING = 'PROCESS_FETCH_TRENDING'; +export const FETCH_TRENDING_SUCCESS = 'FETCH_TRENDING_SUCCESS'; +export const FETCH_TRENDING_FAILED = 'FETCH_TRENDING_FAILED'; diff --git a/src/redux/preference/types.js b/src/redux/preference/types.js index ac613c6..84e1f8b 100644 --- a/src/redux/preference/types.js +++ b/src/redux/preference/types.js @@ -1,3 +1,3 @@ -export const UPDATE_OPTIONS = 'update_options'; -export const RESET_OPTIONS = 'reset_options'; -export const UPDATE_VIEW_TYPE = 'update_view_type'; +export const UPDATE_OPTIONS = 'UPDATE_OPTIONS'; +export const RESET_OPTIONS = 'RESET_OPTIONS'; +export const UPDATE_VIEW_TYPE = 'UPDATE_VIEW_TYPE'; -- cgit v1.3