ソースを参照

auth_reducer, promise_reducer refactor

pocu46 3 年 前
コミット
8b24d7833b
2 ファイル変更50 行追加35 行削除
  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 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) => {
-    if (action.type === 'LOGIN' || action.type === 'LOGOUT') {
+    if (action.type === LOGIN || action.type === LOGOUT) {
         return {}
     }
-    if (action.type === 'PROMISE') {
+    if (action.type === PROMISE) {
         return{
             ...state,
             [action.name]: {
                 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
             }
         }
     }
 return state
 }
+
 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) => {
         dispatch(actionPending())
         let result;