Gogs 7 лет назад
Родитель
Сommit
7f67f038a0

+ 0 - 125
src/reducers/app.js

@@ -1,125 +0,0 @@
-import { isEqual, has, isString } from 'lodash'
-import { setToken } from '../utils/auth'
-import {
-    REQUEST_START,
-    REQUEST_KO, 
-    SHOW_SPINNER,
-    HIDE_SPINNER,
-    SHOW_NOTIFICATION, 
-    HIDE_NOTIFICATION, 
-    REQUEST_OK
-} from '../constants/ActionTypes';
-
-const initialState = {
-    spinner: {
-        isOpen: false,
-        message: null,
-    },
-    notification: {
-        isOpen: false,
-        message: null
-    },
-    dialog: {
-        isOpen: false,
-        message: null
-    },
-    isAuthenticated: true
-}
-
-/**
- * 
- * @param {*} state 
- * @param {*} action 
- */
-export const app = (state = initialState, action) => {
-    if (isEqual(action.type, SHOW_SPINNER) || isEqual(action.type, REQUEST_START)) {
-        state = {
-            ...state,
-            spinner: {
-                isOpen: true,
-                message: action.payload
-            }
-        }
-    }
-
-    if (isEqual(action.type, HIDE_SPINNER) || isEqual(action.type, REQUEST_OK)) {
-        // Response is auth
-        if (has(action.payload, 'auth')) {
-            state = {
-                ...state,
-                isAuthenticated: action.payload.auth
-            }
-        }
-
-        // Response has token
-        if (has(action.payload, 'token')) {
-            setToken(action.payload.token)
-
-            state = {
-                ...state,
-                isAuthenticated: true
-            }
-        }
-
-        // Response is action
-        if (has(action.payload, 'action')) {
-            if (isEqual(action.payload.action.type, 'redirect')) {
-                const params = {
-                    ip: action.payload.action.ip,
-                    port: action.payload.action.port
-                }
-
-                window.open(`${window.location.protocol}//${params.ip}:${params.port}`, '_blank')
-            }
-        }   
-        
-        // Hide spinner
-        state = {
-            ...state,
-            spinner: {
-                isOpen: false,
-                message: isString(action.payload) ? action.payload : null
-            }
-        }
-    }
-
-    // if (isEqual(action.type, REQUEST_KO)) {
-    //     const status = action.payload.status
-        
-    //     if (status === 401) {
-    //         return {
-    //             ...state,
-    //             spinner: {
-    //                 isOpen: false
-    //             },
-    //             isAuthenticated: false
-    //         }
-    //     }
-    // }
-
-    if (isEqual(action.type, SHOW_NOTIFICATION)) {
-        state = {
-            ...state,
-            spinner: {
-                isOpen: false,
-                message: null
-            },
-            notification: {
-                isOpen: true,
-                message: isString(action.payload) ? action.payload : 'No se pudo terminar la petición'
-            }
-        }
-    }
-
-    if (isEqual(action.type, HIDE_NOTIFICATION)) {
-        state = {
-            ...state,
-            notification: {
-                isOpen: false,
-                message: null
-            }
-        }
-    }
-
-    return state
-}

+ 13 - 0
src/reducers/auth.js

@@ -1,5 +1,6 @@
 import { createReducer } from '../utils/reducer'
 import { REQUEST_OK, REQUEST_KO } from '../constants/ActionTypes'
+import { setToken } from '../utils/auth'
 import { has, isEqual } from 'lodash'
 
 const initialState = {
@@ -16,6 +17,18 @@ const checkAuthentication = (isAuthenticated, action) => {
         return isAuthenticated
     }
 
+    // success auth
+    if (isEqual(action.payload.status, 200)) {
+        if (has(action.payload, 'token')) {
+            setToken(action.payload.token)
+
+            return true;
+        }
+
+        return true
+    }
+
+    // error auth
     if (isEqual(action.payload.status, 401)) {
         return false
     }

+ 25 - 8
src/reducers/containers.js

@@ -1,25 +1,42 @@
+import { createReducer } from '../utils/reducer'
 import { REQUEST_OK } from '../constants/ActionTypes'
 import { has, map, isEqual } from 'lodash'
 
+const initialState = {
+    containers: []
+}
+
 /**
  * 
- * @param {*} state 
+ * @param {*} containers 
  * @param {*} action 
  */
-export const containers = (state = [], action) => {
-    if (action.type !== REQUEST_OK) {
-        return state
-    }
-
+const setContainers = (containers, action) => {
+    // single instance
     if (has(action.payload, 'container')) {
-        return map(state, c => {
+        return map(containers, c => {
             return isEqual(c.id, action.payload.container.id) ? action.payload.container : c
         })
     }
 
+    // multiple instances
     if (has(action.payload, 'containers')) {
         return action.payload.containers
     }
 
-    return state
+    return containers
+}
+
+/**
+ * 
+ */
+const containersReducer = createReducer([], {
+    [REQUEST_OK]: setContainers
+})
+
+/**
+ * 
+ */
+export const containers = (state = initialState, action) => {
+    return containersReducer(state.containers, action)
 }

+ 0 - 2
src/reducers/index.js

@@ -7,7 +7,6 @@ import LoggerMiddleware from 'redux-logger'
 import { auth } from './auth'
 import { spinner } from './spinner'
 import { notification } from './notification'
-import { app } from './app'
 import { containers } from './containers'
 import { requests } from './requests'
 
@@ -17,7 +16,6 @@ const rootReducer = combineReducers({
     auth,
     spinner,
     notification,
-    // app,
     containers,
     requests,
     router: routerReducer

+ 4 - 2
src/reducers/notification.js

@@ -1,6 +1,6 @@
 import { createReducer } from '../utils/reducer'
 import { REQUEST_OK, REQUEST_KO, HIDE_NOTIFICATION } from '../constants/ActionTypes'
-import { has, isEqual } from 'lodash'
+import { isEmpty, has, isEqual } from 'lodash'
 
 const initialState = {
     isVisible: false,
@@ -13,13 +13,15 @@ const initialState = {
  * @param {*} action 
  */
 const showNotification = (state, action) => {
+    // common notification
     if (!has(action.payload, 'status')) {
         return {
-            isVisible: true,
+            isVisible: !isEmpty(action.payload.message),
             message: action.payload.message
         }
     }
 
+    // error notification
     if (isEqual(action.payload.status, 401)) {
         if (has(action.payload, 'error_message')) {
             return {

+ 27 - 5
src/reducers/requests.js

@@ -1,14 +1,36 @@
+import { createReducer } from '../utils/reducer'
 import { REQUEST_OK } from '../constants/ActionTypes'
 import { has } from 'lodash'
 
-export const requests = (state = [], action) => {
-    if (action.type !== REQUEST_OK) {
-        return state
-    }
+const initialState = {
+    requests: []
+}
 
+/**
+ * 
+ * @param {*} requests 
+ * @param {*} action 
+ */
+const setRequests = (requests, action) => {
     if (has(action.payload, 'requests')) {
         return action.payload.requests
     }
 
-    return state
+    return requests
+}
+
+/**
+ * 
+ */
+const requestsReducer = createReducer([], {
+    [REQUEST_OK]: setRequests
+})
+
+/**
+ * 
+ * @param {*} state 
+ * @param {*} action 
+ */
+export const requests = (state = initialState, action) => {
+    return requestsReducer(state.requests, action)
 }