|
@@ -1,51 +1,66 @@
|
|
-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";
|
|
|
|
|
|
const authReducer = (state, action) => {
|
|
const authReducer = (state, action) => {
|
|
- // if(action.type===)
|
|
|
|
|
|
+ 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 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,
|
|
|
|
+ [action.name]: {
|
|
|
|
+ status: action.status,
|
|
|
|
+ payload: (action.status === "PENDING" && state[action.name] && state[action.name].payload) || action.payload, // зачем эта проверка?
|
|
|
|
+ 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 payload;
|
|
|
|
|
|
+ let result;
|
|
try {
|
|
try {
|
|
- payload = await promise
|
|
|
|
- dispatch(actionResolved(payload))
|
|
|
|
|
|
+ result = await promise
|
|
|
|
+ dispatch(actionResolved(result))
|
|
}
|
|
}
|
|
catch (e) {
|
|
catch (e) {
|
|
dispatch(actionRejected(e))
|
|
dispatch(actionRejected(e))
|
|
}
|
|
}
|
|
- return payload
|
|
|
|
|
|
+ return result
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|