diff --git a/src/redux/map/map.thunks.test.ts b/src/redux/map/map.thunks.test.ts index bdcf388d147f15d8d91dd8976c00bae44e0f04b8..c8bd15b1daf46ccec5383edbd55f11f352cf4548 100644 --- a/src/redux/map/map.thunks.test.ts +++ b/src/redux/map/map.thunks.test.ts @@ -70,6 +70,16 @@ describe('map thunks - utils', () => { expect(backgroundId).toBe(0); }); + + it('should return main map background id if query param background id is invalid', () => { + const store: RootState = { + ...INITIAL_STORE_STATE_MOCK, + backgrounds: { ...BACKGROUND_INITIAL_STATE_MOCK, data: BACKGROUNDS_MOCK }, + }; + const backgroundId = getBackgroundId(store, QUERY_DATA_WITH_BG); + + expect(backgroundId).toBe(13); + }); }); describe('getInitMapPosition', () => { @@ -96,7 +106,7 @@ describe('map thunks - utils', () => { }); describe('getInitMapSizeAndModelId', () => { - it('should return correct mapsize and modelid when modelId is provided in queryData', () => { + it('should return correct map size and modelId when modelId is provided in queryData', () => { const payload = getInitMapSizeAndModelId(STATE_WITH_MODELS, QUERY_DATA_WITH_MODELID); expect(payload).toEqual({ @@ -104,7 +114,7 @@ describe('map thunks - utils', () => { size: { height: 1171.9429798877356, maxZoom: 5, minZoom: 2, tileSize: 256, width: 1652.75 }, }); }); - it('should return correct mapsize and modelId if query params do not include modelId', () => { + it('should return correct map size and modelId if query params do not include modelId', () => { const payload = getInitMapSizeAndModelId(STATE_WITH_MODELS, EMPTY_QUERY_DATA); expect(payload).toEqual({ modelId: 5053, @@ -117,6 +127,22 @@ describe('map thunks - utils', () => { }, }); }); + it('should return correct map size and modelId if query params include invalid modelId', () => { + const payload = getInitMapSizeAndModelId(STATE_WITH_MODELS, { + ...EMPTY_QUERY_DATA, + modelId: 1234567, + }); + expect(payload).toEqual({ + modelId: 5053, + size: { + height: 13503, + maxZoom: 9, + minZoom: 2, + tileSize: 256, + width: 26779.25, + }, + }); + }); }); describe('getOpenedMaps ', () => { diff --git a/src/redux/map/map.thunks.ts b/src/redux/map/map.thunks.ts index 0ea2c2ea5766dcee135c80d56ab3122a3cc6217e..fb0f3dc69a73c3041973f3c97340ff204cc55270 100644 --- a/src/redux/map/map.thunks.ts +++ b/src/redux/map/map.thunks.ts @@ -21,7 +21,10 @@ import { OppenedMap, Position, } from './map.types'; -import { mainBackgroundsDataSelector } from '../backgrounds/background.selectors'; +import { + backgroundsDataSelector, + mainBackgroundsDataSelector, +} from '../backgrounds/background.selectors'; import { currentModelSelector, mainMapModelSelector, @@ -37,11 +40,22 @@ import { INIT_OPENED_MAPS_ERROR_PREFIX, } from './map.constants'; -/** UTILS - in the same file because of dependancy cycle */ +/** UTILS - in the same file because of dependency cycle */ export const getBackgroundId = (state: RootState, queryData: QueryData): number => { const mainMapBackground = mainBackgroundsDataSelector(state); - const backgroundId = queryData?.backgroundId || mainMapBackground?.id || ZERO; + const backgrounds = backgroundsDataSelector(state); + let backgroundId = queryData?.backgroundId || mainMapBackground?.id || ZERO; + + if (backgrounds.length > 0) { + if ( + backgrounds.filter(background => { + return background.id === backgroundId; + }).length === 0 + ) { + backgroundId = backgrounds[ZERO].id; + } + } if (backgroundId !== mainMapBackground?.id) { PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', backgroundId); @@ -50,9 +64,24 @@ export const getBackgroundId = (state: RootState, queryData: QueryData): number return backgroundId; }; -export const getInitMapPosition = (state: RootState, queryData: QueryData): Position => { +export const getModelId = (state: RootState, queryData: QueryData): number => { const mainMapModel = mainMapModelSelector(state); - const modelId = queryData?.modelId || mainMapModel?.idObject || ZERO; + const models = modelsDataSelector(state); + let modelId = queryData?.modelId || mainMapModel?.idObject || ZERO; + if (models.length > 0) { + if ( + models.filter(model => { + return model.idObject === modelId; + }).length === 0 + ) { + modelId = models[ZERO].idObject; + } + } + return modelId; +}; + +export const getInitMapPosition = (state: RootState, queryData: QueryData): Position => { + const modelId = getModelId(state, queryData); const currentModel = modelByIdSelector(state, modelId); const position = queryData?.initialPosition; const HALF = 2; @@ -98,7 +127,7 @@ export const getInitMapSizeAndModelId = ( queryData: QueryData, ): MapSizeAndModelId => { const mainMapModel = mainMapModelSelector(state); - const modelId = queryData?.modelId || mainMapModel?.idObject || ZERO; + const modelId = getModelId(state, queryData); const currentModel = modelByIdSelector(state, modelId); if (modelId !== mainMapModel?.idObject) {