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