From 63679a8a813b699bc22d26e4adf86167ccdde429 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <p.gawron@atcomp.pl> Date: Thu, 22 Aug 2024 12:13:22 +0200 Subject: [PATCH] use valid model id if the one provided in the url is invalid --- src/redux/map/map.thunks.test.ts | 20 ++++++++++++++++++-- src/redux/map/map.thunks.ts | 21 ++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/redux/map/map.thunks.test.ts b/src/redux/map/map.thunks.test.ts index 500c7e79..c8bd15b1 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 628996af..fb0f3dc6 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) { -- GitLab