redux: auth and map init

This commit is contained in:
muerwre 2018-11-26 11:56:19 +07:00
parent dca55a3bc4
commit df6202c32d
17 changed files with 325 additions and 163 deletions

View file

@ -1,15 +0,0 @@
export const ACTIONS = {
};
export const ROLES = {
guest: 'guest',
vk: 'vk',
};
export const EMPTY_USER = {
token: '',
name: '',
role: ROLES.guest,
picture: '',
};

View file

@ -1,12 +0,0 @@
import { createReducer } from 'reduxsauce';
import { ACTIONS, EMPTY_USER } from '$redux/auth/constants';
const HANDLERS = {
};
export const INITIAL_STATE = {
...EMPTY_USER
};
export const authReducer = createReducer(INITIAL_STATE, HANDLERS);

View file

@ -1,4 +0,0 @@
export function* authSaga() {
// Login
// yield takeLatest(AUTH_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
}

View file

@ -3,15 +3,16 @@ import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import createSagaMiddleware from 'redux-saga';
import createHistory from 'history/createBrowserHistory';
// import createHistory from 'history/createBrowserHistory';
// import { routerReducer, routerMiddleware } from 'react-router-redux';
import { authReducer } from '$redux/auth/reducer';
import { authSaga } from '$redux/auth/sagas';
import { userReducer } from '$redux/user/reducer';
import { userSaga } from '$redux/user/sagas';
const authPersistConfig = {
key: 'auth',
blacklist: [],
const userPersistConfig = {
key: 'user',
blacklist: ['editing'],
storage,
};
@ -31,18 +32,15 @@ const composeEnhancers =
export const store = createStore(
combineReducers({
auth: persistReducer(authPersistConfig, authReducer),
user: persistReducer(userPersistConfig, userReducer),
// routing: routerReducer
}),
composeEnhancers(applyMiddleware(
// routerMiddleware(history),
sagaMiddleware
))
composeEnhancers(applyMiddleware(/* routerMiddleware(history), */ sagaMiddleware))
);
export function configureStore() {
// run sagas
sagaMiddleware.run(authSaga);
sagaMiddleware.run(userSaga);
const persistor = persistStore(store);

View file

@ -0,0 +1,4 @@
import { ACTIONS } from '$redux/user/constants';
export const setUser = user => ({ type: ACTIONS.SET_USER, user });
export const setEditing = editing => ({ type: ACTIONS.SET_EDITING, editing });

View file

@ -0,0 +1,4 @@
export const ACTIONS = {
SET_USER: 'SET_USER',
SET_EDITING: 'SET_EDITING',
};

25
src/redux/user/reducer.js Normal file
View file

@ -0,0 +1,25 @@
import { createReducer } from 'reduxsauce';
import { ACTIONS, EMPTY_USER } from '$redux/user/constants';
import { DEFAULT_USER } from '$constants/auth';
const setUser = (state, { user }) => ({
...state,
...user,
});
const setEditing = (state, { editing }) => ({
...state,
editing,
});
const HANDLERS = {
[ACTIONS.SET_USER]: setUser,
[ACTIONS.SET_EDITING]: setEditing,
};
export const INITIAL_STATE = {
...DEFAULT_USER
};
export const userReducer = createReducer(INITIAL_STATE, HANDLERS);

80
src/redux/user/sagas.js Normal file
View file

@ -0,0 +1,80 @@
import { REHYDRATE } from 'redux-persist';
import { takeLatest, select, call, put } from 'redux-saga/effects';
import { checkUserToken, getGuestToken, getStoredMap } from '$utils/api';
import { setUser } from '$redux/user/actions';
import { getUrlData, pushPath } from '$utils/history';
import { editor } from '$modules/Editor';
const getUser = state => (state.user);
const hideLoader = () => {
document.getElementById('loader').style.opacity = 0;
document.getElementById('loader').style.pointerEvents = 'none';
return true;
};
function* generateGuestSaga() {
const user = yield call(getGuestToken);
yield put(setUser(user));
return user;
}
function* startEmptyEditorSaga() {
const { id, random_url } = yield select(getUser);
console.log('RURL', random_url);
pushPath(`/${random_url}/edit`);
editor.owner = id;
editor.startEditing();
return hideLoader();
// todo: this.clearChanged();
}
function* mapInitSaga() {
const { path, mode } = getUrlData();
if (path) {
const map = yield call(getStoredMap, { name: path });
if (map) {
// todo: this.clearChanged();
editor.setData(map);
if (mode && mode === 'edit') {
editor.startEditing();
} else {
editor.stopEditing();
}
return hideLoader();
}
}
return yield call(startEmptyEditorSaga);
}
function* authChechSaga() {
const { id, token } = yield select(getUser);
if (id && token) {
const user = yield call(checkUserToken, { id, token });
if (user && user.success) {
yield put(setUser(user));
return yield call(mapInitSaga);
}
}
yield call(generateGuestSaga);
return yield call(mapInitSaga);
}
export function* userSaga() {
// Login
// yield takeLatest(AUTH_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
yield takeLatest(REHYDRATE, authChechSaga);
}