Browse Source

auth_reducer, promise_reducer refactor

pocu46 3 years ago
parent
commit
8b24d7833b
2 changed files with 50 additions and 35 deletions
  1. 35 0
      hipstagram/src/Redux/auth_reducer.js
  2. 15 35
      hipstagram/src/Redux/promise_reducer.js

+ 35 - 0
hipstagram/src/Redux/auth_reducer.js

@@ -0,0 +1,35 @@
+import jwt_decode from "jwt-decode";
+
+const LOGIN = 'LOGIN';
+const LOGOUT = 'LOGOUT';
+
+const authReducer = (state, action) => {
+    if(!state) {
+        if(!localStorage.authToken) {
+            return {}
+        } else {
+            // return {
+            //     login: jwt_decode(localStorage.authToken).sub.login,
+            //     id: jwt_decode(localStorage.authToken).sub.id
+            // }
+            action.type = LOGIN;
+            action.jwt = localStorage.authToken
+        }
+    }
+    if(action.type === LOGIN) {
+        localStorage.authToken = action.jwt;
+        return {
+            id: jwt_decode(action.jwt).sub.id, 
+            login: jwt_decode(action.jwt).sub.login,
+            isLogin: true,
+            token: action.jwt
+        }
+    }
+    if(action.type === LOGOUT) {
+        localStorage.removeItem('authToken');
+        return {};
+    }
+    return state
+}
+
+export default authReducer;

+ 15 - 35
hipstagram/src/Redux/promise_reducer.js

@@ -1,55 +1,35 @@
 import { createStore, combineReducers, applyMiddleware } from 'redux';
 import { createStore, combineReducers, applyMiddleware } from 'redux';
 import thunk from 'redux-thunk';
 import thunk from 'redux-thunk';
-import jwt_decode from "jwt-decode";
+import authReducer from './auth_reducer';
 
 
-const authReducer = (state, action) => {
-    if(!state) {
-        if(!localStorage.authToken) {
-            return {}
-        } else {
-            // return {
-            //     login: jwt_decode(localStorage.authToken).sub.login,
-            //     id: jwt_decode(localStorage.authToken).sub.id
-            // }
-            action.type = 'LOGIN';
-            action.jwt = localStorage.authToken
-        }
-    }
-    if(action.type === 'LOGIN') {
-        localStorage.authToken = action.jwt;
-        return {
-            id: jwt_decode(action.jwt).sub.id, 
-            login: jwt_decode(action.jwt).sub.login,
-            isLogin: true,
-            token: action.jwt
-        }
-    }
-    if(action.type === 'LOGOUT') {
-        localStorage.removeItem('authToken');
-        return {};
-    }
-    return state
-}
+const LOGIN = 'LOGIN';
+const LOGOUT = 'LOGOUT';
+const PROMISE = 'PROMISE';
+const PENDING = 'PENDING';
+const RESOLVED = 'RESOLVED';
+const REJECTED = 'REJECTED';
+ 
 const promiseReducer = (state, action) => {
 const promiseReducer = (state, action) => {
-    if (action.type === 'LOGIN' || action.type === 'LOGOUT') {
+    if (action.type === LOGIN || action.type === LOGOUT) {
         return {}
         return {}
     }
     }
-    if (action.type === 'PROMISE') {
+    if (action.type === PROMISE) {
         return{
         return{
             ...state,
             ...state,
             [action.name]: {
             [action.name]: {
                 status: action.status, 
                 status: action.status, 
-                payload: (action.status === "PENDING" && state[action.name] && state[action.name].payload) || action.payload, // зачем эта проверка?
+                payload: (action.status === PENDING && state[action.name] && state[action.name].payload) || action.payload, // зачем эта проверка?
                 error: action.error
                 error: action.error
             }
             }
         }
         }
     }
     }
 return state
 return state
 }
 }
+
 const actionPromise = (name, promise) => {
 const actionPromise = (name, promise) => {
-    const actionPending = () => ({ type: 'PROMISE', name, status: 'PENDING', payload: null, error: null })
-    const actionResolved = (payload) => ({ type: 'PROMISE', name, status: 'RESOLVED', payload, error: null })
-    const actionRejected = (error) => ({ type: 'PROMISE', name, status: 'REJECTED', payload: null, error })
+    const actionPending = () => ({ type: PROMISE, name, status: PENDING, payload: null, error: null })
+    const actionResolved = (payload) => ({ type: PROMISE, name, status: RESOLVED, payload, error: null })
+    const actionRejected = (error) => ({ type: PROMISE, name, status: REJECTED, payload: null, error })
     return async (dispatch) => {
     return async (dispatch) => {
         dispatch(actionPending())
         dispatch(actionPending())
         let result;
         let result;