summaryrefslogtreecommitdiff
path: root/src/redux
diff options
context:
space:
mode:
authorKamran Ahmed <kamranahmed.se@gmail.com>2018-08-19 21:58:30 +0400
committerKamran Ahmed <kamranahmed.se@gmail.com>2018-08-19 21:58:30 +0400
commit02614f5ee9c6ad3389a7d43bdbeba6ec6e88659b (patch)
treee9df96aa2e47f070b2f5c13143adcc9da14bdf6a /src/redux
parentSeparate loading component (diff)
downloadgithunt-02614f5ee9c6ad3389a7d43bdbeba6ec6e88659b.tar.gz
Add axios and reducer for github
Diffstat (limited to 'src/redux')
-rw-r--r--src/redux/github/actions.js33
-rw-r--r--src/redux/github/reducer.js33
-rw-r--r--src/redux/github/types.js3
-rw-r--r--src/redux/preference/types.js6
4 files changed, 72 insertions, 3 deletions
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';