diff --git a/src/redux/map/map.thunks.test.ts b/src/redux/map/map.thunks.test.ts index 500c7e790041840393162b229dc8f71998e286f0..c8bd15b1daf46ccec5383edbd55f11f352cf4548 100644 --- a/src/redux/map/map.thunks.test.ts +++ b/src/redux/map/map.thunks.test.ts @@ -106,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({ @@ -114,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, @@ -127,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 628996af603427249b44e1cd4dd41faa7da759f4..fb0f3dc69a73c3041973f3c97340ff204cc55270 100644 --- a/src/redux/map/map.thunks.ts +++ b/src/redux/map/map.thunks.ts @@ -64,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; @@ -112,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) {