orchidmap-front/src/redux/user/sagas.js
2018-11-26 15:11:51 +07:00

141 lines
3.3 KiB
JavaScript

import { REHYDRATE } from 'redux-persist';
import { takeLatest, select, call, put, takeEvery } from 'redux-saga/effects';
import { checkUserToken, getGuestToken, getStoredMap } from '$utils/api';
import { setEditing, setMode, setUser } from '$redux/user/actions';
import { getUrlData, pushPath } from '$utils/history';
import { editor } from '$modules/Editor';
import { ACTIONS } from '$redux/user/constants';
import { MODES } from '$constants/modes';
import { DEFAULT_USER } from '$constants/auth';
const getUser = state => (state.user.user);
const getState = 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);
}
function* setModeSaga({ mode }) {
return yield editor.changeMode(mode);
// console.log('change', mode);
}
function* startEditingSaga() {
yield editor.startEditing();
yield put(setEditing(true));
}
function* stopEditingSaga() {
const { changed } = yield select(getState);
if (!changed) {
yield editor.cancelEditing();
yield put(setEditing(false));
yield put(setMode(MODES.NONE));
} else {
// editor.changeMode(MODES.CONFIRM_CANCEL);
// this.props.setMode(MODES.CONFIRM_CANCEL);
yield put(setMode(MODES.CONFIRM_CANCEL));
}
}
function* userLogoutSaga() {
const { id } = yield select(getUser);
if (id === editor.owner) {
editor.owner = null;
}
yield put(setUser(DEFAULT_USER));
yield call(generateGuestSaga);
}
function* setActiveStickerSaga({ activeSticker }) {
yield editor.activeSticker = activeSticker;
return true;
}
function* setLogoSaga({ logo }) {
const { mode } = yield select(getState);
editor.logo = logo;
if (mode === MODES.LOGO) {
yield put(setMode(MODES.NONE));
}
}
export function* userSaga() {
// ASYNCHRONOUS!!! :-)
yield takeLatest(REHYDRATE, authChechSaga);
yield takeEvery(ACTIONS.SET_MODE, setModeSaga);
yield takeEvery(ACTIONS.START_EDITING, startEditingSaga);
yield takeEvery(ACTIONS.STOP_EDITING, stopEditingSaga);
yield takeEvery(ACTIONS.USER_LOGOUT, userLogoutSaga);
yield takeEvery(ACTIONS.SET_ACTIVE_STICKER, setActiveStickerSaga);
yield takeEvery(ACTIONS.SET_LOGO, setLogoSaga);
}