|
@@ -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;
|