Browse Source

[IMP] reducer engine

Gogs 7 years ago
parent
commit
aedbbf6ebe

+ 4 - 0
src/reducers/index.js

@@ -4,6 +4,8 @@ import createBrowserHistory from 'history/createBrowserHistory'
 import ThunkMiddleware from  'redux-thunk'
 import LoggerMiddleware from 'redux-logger'
 
+import { spinner } from './spinner'
+import { notification } from './notification'
 import { app } from './app'
 import { containers } from './containers'
 import { requests } from './requests'
@@ -11,6 +13,8 @@ import { requests } from './requests'
 const history = createBrowserHistory()
 
 const rootReducer = combineReducers({
+    spinner,
+    notification,
     app,
     containers,
     requests,

+ 33 - 0
src/reducers/notification.js

@@ -0,0 +1,33 @@
+import { createReducer } from '../utils/reducer'
+import { REQUEST_KO } from '../constants/ActionTypes'
+
+const initialState = {
+    isVisible: false,
+    message: null
+}
+
+/**
+ * 
+ * @param {*} state 
+ * @param {*} action 
+ */
+const setNotificationState = (state, action) => {
+
+    return false
+}
+
+/**
+ * 
+ */
+const notificationReducer = createReducer(initialState, {
+    [REQUEST_KO]: setNotificationState
+})
+
+/**
+ * 
+ * @param {*} state 
+ * @param {*} action 
+ */
+export const notification = (state = initialState, action) => {
+    return notificationReducer(state, action)
+}

+ 36 - 0
src/reducers/spinner.js

@@ -0,0 +1,36 @@
+import { createReducer } from '../utils/reducer'
+import { REQUEST_START, REQUEST_OK, REQUEST_KO } from '../constants/ActionTypes'
+
+const initialState = {
+    isVisible: false
+}
+
+/**
+ * 
+ * @param {*} visibilityState 
+ * @param {*} action 
+ */
+const setSpinnerVisibility = (visibilityState, action) => {
+    return true
+}
+
+/**
+ * 
+ */
+const spinnerVisibilityReducer = createReducer(false, {
+    [REQUEST_START]: setSpinnerVisibility,
+    [REQUEST_OK]: setSpinnerVisibility,
+    [REQUEST_KO]: setSpinnerVisibility
+})
+
+
+/**
+ * 
+ * @param {*} state 
+ * @param {*} payload 
+ */
+export const spinner = (state = initialState, action) => {
+    return {
+        isVisible: spinnerVisibilityReducer(state.isVisible, action)
+    }
+}

+ 12 - 0
src/reducers/user.js

@@ -0,0 +1,12 @@
+const initialState = {
+    username: null
+}
+
+/**
+ * 
+ * @param {*} state 
+ * @param {*} payload 
+ */
+export const user = (state = initialState, payload) => {
+
+}

+ 0 - 20
src/registerAuthEngine.js

@@ -1,20 +0,0 @@
-import axios from 'axios'
-
-const TOKEN_KEY = 'eiruAutomation'
-
-/**
- * 
- */
-const init = () => {
-    const token = localStorage.getItem(TOKEN_KEY)
-
-    if (!token) {
-        return
-    }
-
-    axios.defaults.headers.common['Accept'] = 'application/json'
-    axios.defaults.headers.common['Content-Type'] = 'application/json'
-    axios.defaults.headers.common['Authorization'] = `JWT ${token}`
-} 
-
-export default init

+ 1 - 2
src/utils/http.js

@@ -9,8 +9,7 @@ const instance = axios.create({
     headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
-    },
-    timeout: 1000
+    }
 })
 
 // Define interceptor

+ 12 - 0
src/utils/reducer.js

@@ -0,0 +1,12 @@
+import { has } from 'lodash'
+
+/**
+ * 
+ * @param {*} initialState 
+ * @param {*} payload 
+ */
+export const createReducer = (initialState, handlers) => {
+    return (state = initialState, action) => {
+        return has(handlers, action.type) ? handlers[action.type](state, action) : state
+    }
+}