fixed map setting on load

This commit is contained in:
Fedor Katurov 2020-01-10 12:26:33 +07:00
parent 8adf64acb9
commit 9760002fad
2 changed files with 72 additions and 96 deletions

View file

@ -39,7 +39,7 @@ import {
import { selectMap, selectMapRoute } from '../map/selectors'; import { selectMap, selectMapRoute } from '../map/selectors';
import { selectUser } from '../user/selectors'; import { selectUser } from '../user/selectors';
import { LOGOS } from '~/constants/logos'; import { LOGOS } from '~/constants/logos';
import { loadMapSaga } from '../map/sagas'; import { loadMapSaga, startEmptyEditorSaga, loadMapFromPath } from '../map/sagas';
import { mapClicked, mapSetRoute } from '../map/actions'; import { mapClicked, mapSetRoute } from '../map/actions';
import { MAP_ACTIONS } from '../map/constants'; import { MAP_ACTIONS } from '../map/constants';
import { OsrmRouter } from '~/utils/osrm'; import { OsrmRouter } from '~/utils/osrm';
@ -58,7 +58,8 @@ function* stopEditingSaga() {
const { address_origin }: ReturnType<typeof selectMap> = yield select(selectMap); const { address_origin }: ReturnType<typeof selectMap> = yield select(selectMap);
const { path } = getUrlData(); const { path } = getUrlData();
if (!editing) return; // if (!editing) return;
if (changed && mode !== MODES.CONFIRM_CANCEL) { if (changed && mode !== MODES.CONFIRM_CANCEL) {
yield put(editorSetMode(MODES.CONFIRM_CANCEL)); yield put(editorSetMode(MODES.CONFIRM_CANCEL));
return; return;
@ -176,21 +177,10 @@ function* cropAShotSaga(params) {
function* locationChangeSaga({ location }: ReturnType<typeof editorLocationChanged>) { function* locationChangeSaga({ location }: ReturnType<typeof editorLocationChanged>) {
const { ready }: ReturnType<typeof selectEditor> = yield select(selectEditor); const { ready }: ReturnType<typeof selectEditor> = yield select(selectEditor);
const { address }: ReturnType<typeof selectMap> = yield select(selectMap);
if (!ready) return; if (!ready) return;
const { path, mode } = getUrlData(location); yield call(loadMapFromPath);
if (address !== path) {
yield call(loadMapSaga, path);
}
if (mode !== 'edit') {
yield put(editorSetEditing(false));
} else {
yield put(editorSetEditing(true));
}
} }
function* keyPressedSaga({ key, target }: ReturnType<typeof editorKeyPressed>) { function* keyPressedSaga({ key, target }: ReturnType<typeof editorKeyPressed>) {
@ -259,10 +249,11 @@ function* routerSubmit() {
} }
export function* editorSaga() { export function* editorSaga() {
yield takeEvery(EDITOR_ACTIONS.LOCATION_CHANGED, locationChangeSaga);
yield takeEvery(EDITOR_ACTIONS.STOP_EDITING, stopEditingSaga); yield takeEvery(EDITOR_ACTIONS.STOP_EDITING, stopEditingSaga);
yield takeLatest(EDITOR_ACTIONS.TAKE_A_SHOT, takeAShotSaga); yield takeLatest(EDITOR_ACTIONS.TAKE_A_SHOT, takeAShotSaga);
yield takeLatest(EDITOR_ACTIONS.CROP_A_SHOT, cropAShotSaga); yield takeLatest(EDITOR_ACTIONS.CROP_A_SHOT, cropAShotSaga);
yield takeLatest(EDITOR_ACTIONS.LOCATION_CHANGED, locationChangeSaga);
yield takeLatest(EDITOR_ACTIONS.KEY_PRESSED, keyPressedSaga); yield takeLatest(EDITOR_ACTIONS.KEY_PRESSED, keyPressedSaga);
yield takeLatest(EDITOR_ACTIONS.GET_GPX_TRACK, getGPXTrackSaga); yield takeLatest(EDITOR_ACTIONS.GET_GPX_TRACK, getGPXTrackSaga);
yield takeLatest(EDITOR_ACTIONS.ROUTER_CANCEL, routerCancel); yield takeLatest(EDITOR_ACTIONS.ROUTER_CANCEL, routerCancel);

View file

@ -30,12 +30,12 @@ import {
editorSendSaveRequest, editorSendSaveRequest,
editorSetSaveSuccess, editorSetSaveSuccess,
editorSetSaveOverwrite, editorSetSaveOverwrite,
editorClearAll,
} from '~/redux/editor/actions'; } from '~/redux/editor/actions';
import { pushLoaderState, getUrlData, pushPath, replacePath } from '~/utils/history'; import { pushLoaderState, getUrlData, pushPath, replacePath } from '~/utils/history';
import { searchSetSagaWorker } from '~/redux/user/sagas'; import { searchSetSagaWorker } from '~/redux/user/sagas';
import { getStoredMap, postMap } from '~/utils/api'; import { getStoredMap, postMap } from '~/utils/api';
import { Unwrap } from '~/utils/middleware'; import { Unwrap } from '~/utils/middleware';
import { USER_ACTIONS } from '~/redux/user/constants';
import { selectMap, selectMapProvider } from './selectors'; import { selectMap, selectMapProvider } from './selectors';
import { TIPS } from '~/constants/tips'; import { TIPS } from '~/constants/tips';
import { delay } from 'redux-saga'; import { delay } from 'redux-saga';
@ -59,28 +59,6 @@ function* onMapClick({ latlng }: ReturnType<typeof mapClicked>) {
} }
} }
// function* changeProviderSaga({ provider }: ReturnType<typeof changeProvider>) {
// const { provider: current_provider } = yield select(selectUser);
// yield put(mapSetProvider(provider));
// if (current_provider === provider) return;
// yield put(setChanged(true));
// return put(setMode(MODES.NONE));
// }
// function* setLogoSaga({ logo }: { type: string; logo: string }) {
// const { mode } = yield select(selectUser);
// yield put(setChanged(true));
// if (mode === MODES.LOGO) {
// yield put(setMode(MODES.NONE));
// }
// }
export function* replaceAddressIfItsBusy(destination, original) { export function* replaceAddressIfItsBusy(destination, original) {
if (original) { if (original) {
yield put(mapSetAddressOrigin(original)); yield put(mapSetAddressOrigin(original));
@ -95,15 +73,16 @@ export function* loadMapSaga(path) {
}: Unwrap<typeof getStoredMap> = yield call(getStoredMap, { name: path }); }: Unwrap<typeof getStoredMap> = yield call(getStoredMap, { name: path });
if (route && !error) { if (route && !error) {
// TODO: set initial data
// TODO: fit bounds
yield put( yield put(
mapSet({ mapSet({
provider: route.provider, provider: route.provider,
route: route.route, route: route.route,
stickers: route.stickers, stickers: route.stickers,
title: route.title, title: route.title,
address: route.address,
description: route.description,
is_public: route.is_public,
logo: route.logo,
}) })
); );
@ -113,26 +92,49 @@ export function* loadMapSaga(path) {
return null; return null;
} }
function* startEmptyEditorSaga() { export function* startEmptyEditorSaga() {
yield put(editorSetReady(false));
const { const {
user: { id, random_url }, user: { id, random_url },
}: ReturnType<typeof selectUser> = yield select(selectUser); }: ReturnType<typeof selectUser> = yield select(selectUser);
const provider: ReturnType<typeof selectMapProvider> = yield select(selectMapProvider); const { path, mode } = getUrlData();
// TODO: set owner { id } if (!path || !mode || mode !== 'edit') {
pushPath(`/${random_url}/edit`); pushPath(`/${random_url}/edit`);
}
yield put(editorSetChanged(false)); yield put(editorClearAll());
yield put(mapSet({ owner: { id } }));
yield put(editorSetEditing(true)); yield put(editorSetEditing(true));
yield put(editorSetReady(true));
}
return yield call(setReadySaga); export function* loadMapFromPath() {
const { path, mode } = getUrlData();
if (path) {
const map = yield call(loadMapSaga, path);
if (!map) {
yield call(setReadySaga);
return yield call(startEmptyEditorSaga);
}
yield put(editorSetEditing(mode && mode === 'edit'));
yield call(setReadySaga);
return;
}
yield call(setReadySaga);
yield call(startEmptyEditorSaga);
} }
export function* mapInitSaga() { export function* mapInitSaga() {
pushLoaderState(90); pushLoaderState(90);
const { path, mode, hash } = getUrlData(); const { hash } = getUrlData();
const { const {
user: { id }, user: { id },
}: ReturnType<typeof selectUser> = yield select(selectUser); }: ReturnType<typeof selectUser> = yield select(selectUser);
@ -150,34 +152,8 @@ export function* mapInitSaga() {
} }
} }
if (path) { yield call(loadMapFromPath);
const map = yield call(loadMapSaga, path);
if (map && map.route) {
if (mode && mode === 'edit') {
if (map && map.route && map.route.owner && mode === 'edit' && map.route.owner !== id) {
yield call(setReadySaga);
yield call(replaceAddressIfItsBusy, map.random_url, map.address);
} else {
yield put(mapSetAddressOrigin(''));
}
yield put(editorSetEditing(true));
} else {
yield put(editorSetEditing(false));
}
yield call(setReadySaga);
return true;
}
}
yield call(startEmptyEditorSaga);
yield put(editorSetReady(true));
pushLoaderState(100); pushLoaderState(100);
return true;
} }
function* setActiveStickerSaga() { function* setActiveStickerSaga() {
@ -195,32 +171,44 @@ function* startEditingSaga() {
yield pushPath(`/${path}/edit`); yield pushPath(`/${path}/edit`);
} }
function* clearPolySaga() {
yield put(
mapSet({
route: [],
})
);
}
function* clearStickersSaga() {
yield put(
mapSet({
stickers: [],
})
);
}
function* clearAllSaga() {
yield put(editorSetChanged(false));
yield put(
mapSet({
route: [],
stickers: [],
})
);
}
function* clearSaga({ type }) { function* clearSaga({ type }) {
switch (type) { switch (type) {
case EDITOR_ACTIONS.CLEAR_POLY: case EDITOR_ACTIONS.CLEAR_POLY:
yield put( yield call(clearPolySaga);
mapSet({
route: [],
})
);
break; break;
case EDITOR_ACTIONS.CLEAR_STICKERS: case EDITOR_ACTIONS.CLEAR_STICKERS:
yield put( yield call(clearStickersSaga);
mapSet({
stickers: [],
})
);
break; break;
case EDITOR_ACTIONS.CLEAR_ALL: case EDITOR_ACTIONS.CLEAR_ALL:
yield put(editorSetChanged(false)); yield call(clearAllSaga);
yield put(
mapSet({
route: [],
stickers: [],
})
);
break; break;
default: default:
@ -248,7 +236,7 @@ function* sendSaveRequestSaga({
const { distance }: ReturnType<typeof selectEditor> = yield select(selectEditor); const { distance }: ReturnType<typeof selectEditor> = yield select(selectEditor);
const { token }: ReturnType<typeof selectUserUser> = yield select(selectUserUser); const { token }: ReturnType<typeof selectUserUser> = yield select(selectUserUser);
yield put(editorSetSaveLoading(true)); yield put(editorSetSaveLoading(true));
const { const {
result, result,
@ -325,9 +313,6 @@ function* setSaveSuccessSaga({
yield call(searchSetSagaWorker); yield call(searchSetSagaWorker);
} }
// yield editor.setInitialData();
// TODO: set initial data here
return; return;
} }