From 9760002fad493506cc95d57190fb0098ade1ac3d Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Fri, 10 Jan 2020 12:26:33 +0700 Subject: [PATCH] fixed map setting on load --- src/redux/editor/sagas.ts | 21 ++---- src/redux/map/sagas.ts | 147 +++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 96 deletions(-) diff --git a/src/redux/editor/sagas.ts b/src/redux/editor/sagas.ts index 7e2c4a6..e934af1 100644 --- a/src/redux/editor/sagas.ts +++ b/src/redux/editor/sagas.ts @@ -39,7 +39,7 @@ import { import { selectMap, selectMapRoute } from '../map/selectors'; import { selectUser } from '../user/selectors'; import { LOGOS } from '~/constants/logos'; -import { loadMapSaga } from '../map/sagas'; +import { loadMapSaga, startEmptyEditorSaga, loadMapFromPath } from '../map/sagas'; import { mapClicked, mapSetRoute } from '../map/actions'; import { MAP_ACTIONS } from '../map/constants'; import { OsrmRouter } from '~/utils/osrm'; @@ -58,7 +58,8 @@ function* stopEditingSaga() { const { address_origin }: ReturnType = yield select(selectMap); const { path } = getUrlData(); - if (!editing) return; + // if (!editing) return; + if (changed && mode !== MODES.CONFIRM_CANCEL) { yield put(editorSetMode(MODES.CONFIRM_CANCEL)); return; @@ -176,21 +177,10 @@ function* cropAShotSaga(params) { function* locationChangeSaga({ location }: ReturnType) { const { ready }: ReturnType = yield select(selectEditor); - const { address }: ReturnType = yield select(selectMap); if (!ready) return; - const { path, mode } = getUrlData(location); - - if (address !== path) { - yield call(loadMapSaga, path); - } - - if (mode !== 'edit') { - yield put(editorSetEditing(false)); - } else { - yield put(editorSetEditing(true)); - } + yield call(loadMapFromPath); } function* keyPressedSaga({ key, target }: ReturnType) { @@ -259,10 +249,11 @@ function* routerSubmit() { } export function* editorSaga() { + yield takeEvery(EDITOR_ACTIONS.LOCATION_CHANGED, locationChangeSaga); + yield takeEvery(EDITOR_ACTIONS.STOP_EDITING, stopEditingSaga); yield takeLatest(EDITOR_ACTIONS.TAKE_A_SHOT, takeAShotSaga); 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.GET_GPX_TRACK, getGPXTrackSaga); yield takeLatest(EDITOR_ACTIONS.ROUTER_CANCEL, routerCancel); diff --git a/src/redux/map/sagas.ts b/src/redux/map/sagas.ts index c5c8906..570b6ac 100644 --- a/src/redux/map/sagas.ts +++ b/src/redux/map/sagas.ts @@ -30,12 +30,12 @@ import { editorSendSaveRequest, editorSetSaveSuccess, editorSetSaveOverwrite, + editorClearAll, } from '~/redux/editor/actions'; import { pushLoaderState, getUrlData, pushPath, replacePath } from '~/utils/history'; import { searchSetSagaWorker } from '~/redux/user/sagas'; import { getStoredMap, postMap } from '~/utils/api'; import { Unwrap } from '~/utils/middleware'; -import { USER_ACTIONS } from '~/redux/user/constants'; import { selectMap, selectMapProvider } from './selectors'; import { TIPS } from '~/constants/tips'; import { delay } from 'redux-saga'; @@ -59,28 +59,6 @@ function* onMapClick({ latlng }: ReturnType) { } } -// function* changeProviderSaga({ provider }: ReturnType) { -// 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) { if (original) { yield put(mapSetAddressOrigin(original)); @@ -95,15 +73,16 @@ export function* loadMapSaga(path) { }: Unwrap = yield call(getStoredMap, { name: path }); if (route && !error) { - // TODO: set initial data - // TODO: fit bounds - yield put( mapSet({ provider: route.provider, route: route.route, stickers: route.stickers, 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; } -function* startEmptyEditorSaga() { +export function* startEmptyEditorSaga() { + yield put(editorSetReady(false)); + const { user: { id, random_url }, }: ReturnType = yield select(selectUser); - const provider: ReturnType = yield select(selectMapProvider); + const { path, mode } = getUrlData(); - // TODO: set owner { id } - pushPath(`/${random_url}/edit`); + if (!path || !mode || mode !== 'edit') { + pushPath(`/${random_url}/edit`); + } - yield put(editorSetChanged(false)); + yield put(editorClearAll()); + yield put(mapSet({ owner: { id } })); 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() { pushLoaderState(90); - const { path, mode, hash } = getUrlData(); + const { hash } = getUrlData(); const { user: { id }, }: ReturnType = yield select(selectUser); @@ -150,34 +152,8 @@ export function* mapInitSaga() { } } - if (path) { - 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)); - + yield call(loadMapFromPath); pushLoaderState(100); - - return true; } function* setActiveStickerSaga() { @@ -195,32 +171,44 @@ function* startEditingSaga() { 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 }) { switch (type) { case EDITOR_ACTIONS.CLEAR_POLY: - yield put( - mapSet({ - route: [], - }) - ); + yield call(clearPolySaga); break; case EDITOR_ACTIONS.CLEAR_STICKERS: - yield put( - mapSet({ - stickers: [], - }) - ); + yield call(clearStickersSaga); break; case EDITOR_ACTIONS.CLEAR_ALL: - yield put(editorSetChanged(false)); - yield put( - mapSet({ - route: [], - stickers: [], - }) - ); + yield call(clearAllSaga); break; default: @@ -248,7 +236,7 @@ function* sendSaveRequestSaga({ const { distance }: ReturnType = yield select(selectEditor); const { token }: ReturnType = yield select(selectUserUser); - yield put(editorSetSaveLoading(true)); + yield put(editorSetSaveLoading(true)); const { result, @@ -325,9 +313,6 @@ function* setSaveSuccessSaga({ yield call(searchSetSagaWorker); } - // yield editor.setInitialData(); - // TODO: set initial data here - return; }