From d6c72ede3b13d1d49fe53d82afc3c080f984bcc6 Mon Sep 17 00:00:00 2001 From: Mateusz Winiarczyk <mateusz.winiarczyk@appunite.com> Date: Wed, 20 Mar 2024 03:43:48 +0100 Subject: [PATCH] feat(overlays): add possibility to set empty background by optional function argument --- docs/plugins/overlays.md | 7 +++++- .../hooks/useEmptyBackground.ts | 3 --- .../BackgroundsSelector.component.tsx | 2 -- src/redux/map/map.reducers.ts | 4 ++++ .../overlayBioEntity.thunk.ts | 1 - .../addDataOverlay/addDataOverlay.test.ts | 8 +++++-- .../setBackgroundtoEmptyIfAvailable.test.ts | 18 --------------- .../setBackgroundtoEmptyIfAvailable.ts | 3 --- .../showDataOverlay/showDataOverlay.test.ts | 22 ++++++++++++++++++- .../showDataOverlay/showDataOverlay.ts | 6 +++-- 10 files changed, 41 insertions(+), 33 deletions(-) diff --git a/docs/plugins/overlays.md b/docs/plugins/overlays.md index ac86b9c1..b03b1ea1 100644 --- a/docs/plugins/overlays.md +++ b/docs/plugins/overlays.md @@ -22,12 +22,17 @@ window.minerva.overlays.data.getVisibleDataOverlays(); #### Show an overlay -To show an overlay, plugins can use the `showDataOverlay` method defined in `window.minerva.overlays`. This method takes one argument: the ID of the overlay that the plugin wants to show. +To show an overlay, plugins can use the `showDataOverlay` method defined in `window.minerva.overlays`. This method takes following arguments: + +- overlayId - the ID of the overlay that the plugin wants to show. +- setBackgroundEmpty (optional) - whether `showDataOverlay` should set the background to empty if available when it shows overlay. Its value should be a boolean type. ##### Example of showDataOverlay usage: ```javascript window.minerva.overlays.showDataOverlay(109); + +window.minerva.overlays.showDataOverlay(112, true); ``` #### Hide an overlay diff --git a/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts b/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts index fb2eae6c..2536fa84 100644 --- a/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts +++ b/src/components/Map/Drawer/OverlaysDrawer/hooks/useEmptyBackground.ts @@ -3,7 +3,6 @@ import { emptyBackgroundIdSelector } from '@/redux/backgrounds/background.select import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { useAppSelector } from '@/redux/hooks/useAppSelector'; import { setMapBackground } from '@/redux/map/map.slice'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; type UseEmptyBackgroundReturn = { setBackgroundtoEmptyIfAvailable: () => void; @@ -16,8 +15,6 @@ export const useEmptyBackground = (): UseEmptyBackgroundReturn => { const setBackgroundtoEmptyIfAvailable = useCallback(() => { if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } }, [dispatch, emptyBackgroundId]); diff --git a/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx b/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx index 7b31ff35..ca0dd733 100644 --- a/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx +++ b/src/components/Map/MapAdditionalOptions/BackgroundsSelector/BackgroundsSelector.component.tsx @@ -10,7 +10,6 @@ import { Icon } from '@/shared/Icon'; import { MapBackground } from '@/types/models'; import { useAppDispatch } from '@/redux/hooks/useAppDispatch'; import { setMapBackground } from '@/redux/map/map.slice'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; const DEFAULT_TOGGLE_BUTTON_TEXT = 'Background'; @@ -23,7 +22,6 @@ export const BackgroundSelector = (): JSX.Element => { const onItemSelect = (background: MapBackground | undefined | null): void => { if (background) { dispatch(setMapBackground(background.id)); - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', background.id); } }; diff --git a/src/redux/map/map.reducers.ts b/src/redux/map/map.reducers.ts index b2fbff2d..d1562cad 100644 --- a/src/redux/map/map.reducers.ts +++ b/src/redux/map/map.reducers.ts @@ -144,6 +144,10 @@ export const closeMapAndSetMainMapActiveReducer = ( }; export const setMapBackgroundReducer = (state: MapState, action: SetBackgroundAction): void => { + if (action.payload !== state.data.backgroundId) { + PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', action.payload); + } + state.data.backgroundId = action.payload; }; diff --git a/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts b/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts index 30222f30..956b6751 100644 --- a/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts +++ b/src/redux/overlayBioEntity/overlayBioEntity.thunk.ts @@ -100,7 +100,6 @@ export const getInitOverlays = createAsyncThunk< const emptyBackgroundId = emptyBackgroundIdSelector(state); if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } overlaysId.forEach(id => { diff --git a/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts b/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts index a99220a5..729d53cb 100644 --- a/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts +++ b/src/services/pluginsManager/map/overlays/addDataOverlay/addDataOverlay.test.ts @@ -9,6 +9,10 @@ import { RootState, store } from '@/redux/store'; import { mockNetworkResponse } from '@/utils/mockNetworkResponse'; import { HttpStatusCode } from 'axios'; import { addOverlay } from '@/redux/overlays/overlays.thunks'; +import { + ERROR_OVERLAY_NAME_NOT_PROVIDED, + ERROR_PROJECT_ID_NOT_FOUND, +} from '@/services/pluginsManager/errorMessages'; import { addDataOverlay } from './addDataOverlay'; import { DEFAULT_FILE_NAME, DEFAULT_TYPE } from './addDataOverlay.constants'; @@ -82,7 +86,7 @@ describe('addDataOverlay', () => { }) as RootState, ); - await expect(() => addDataOverlay(overlay)).rejects.toThrow('Project id does not exist'); + await expect(() => addDataOverlay(overlay)).rejects.toThrow(ERROR_PROJECT_ID_NOT_FOUND); }); it('should throw error when overlay name is not provided', async () => { @@ -94,7 +98,7 @@ describe('addDataOverlay', () => { }; await expect(addDataOverlay(overlayWithoutName)).rejects.toThrow( - 'Overlay name is not provided', + ERROR_OVERLAY_NAME_NOT_PROVIDED, ); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts index f8c875ee..15b32c94 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.test.ts @@ -4,7 +4,6 @@ import { } from '@/redux/backgrounds/background.mock'; import { initialMapStateFixture } from '@/redux/map/map.fixtures'; import { RootState, store } from '@/redux/store'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; import { setBackgroundtoEmptyIfAvailable } from './setBackgroundtoEmptyIfAvailable'; const DEFAULT_BACKGROUND_ID = 0; @@ -50,21 +49,4 @@ describe('setEmptyBackground', () => { type: 'map/setMapBackground', }); }); - it('should dispatch plugin event with empty background id', () => { - const pluginDispatchEvent = jest.spyOn(PluginsEventBus, 'dispatchEvent'); - getStateSpy.mockImplementation( - () => - ({ - map: initialMapStateFixture, - backgrounds: { ...BACKGROUND_INITIAL_STATE_MOCK, data: BACKGROUNDS_MOCK }, - }) as RootState, - ); - - setBackgroundtoEmptyIfAvailable(); - - expect(pluginDispatchEvent).toHaveBeenCalledWith( - 'onBackgroundOverlayChange', - EMPTY_BACKGROUND_ID, - ); - }); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts index 2d23f45e..59c6c061 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/setBackgroundtoEmptyIfAvailable.ts @@ -1,7 +1,6 @@ import { emptyBackgroundIdSelector } from '@/redux/backgrounds/background.selectors'; import { setMapBackground } from '@/redux/map/map.slice'; import { store } from '@/redux/store'; -import { PluginsEventBus } from '@/services/pluginsManager/pluginsEventBus'; export const setBackgroundtoEmptyIfAvailable = (): void => { const { dispatch, getState } = store; @@ -9,7 +8,5 @@ export const setBackgroundtoEmptyIfAvailable = (): void => { if (emptyBackgroundId) { dispatch(setMapBackground(emptyBackgroundId)); - - PluginsEventBus.dispatchEvent('onBackgroundOverlayChange', emptyBackgroundId); } }; diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts index 3276622c..3ccebefa 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.test.ts @@ -108,7 +108,7 @@ describe('showDataOverlay function', () => { expect(pluginDispatchEvent).toHaveBeenCalledWith('onShowOverlay', overlaysFixture[0]); }); - it('should set empty background when it show overlay', () => { + it('should not set empty background when it show overlay', () => { getStateSpy.mockImplementation( () => ({ @@ -126,6 +126,26 @@ describe('showDataOverlay function', () => { showDataOverlay(OVERLAY_ID); + expect(setBackgroundtoEmptyIfAvailable).not.toHaveBeenCalled(); + }); + it('should set empty background when it show overlay if setBackgroundEmpty is true', () => { + getStateSpy.mockImplementation( + () => + ({ + overlays: { + ...OVERLAYS_INITIAL_STATE_MOCK, + userOverlays: { + data: overlaysFixture, + loading: 'idle', + error: DEFAULT_ERROR, + }, + }, + overlayBioEntity: { ...OVERLAY_BIO_ENTITY_INITIAL_STATE_MOCK }, + }) as RootState, + ); + + showDataOverlay(OVERLAY_ID, true); + expect(setBackgroundtoEmptyIfAvailable).toHaveBeenCalled(); }); }); diff --git a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts index b299debf..8304beed 100644 --- a/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts +++ b/src/services/pluginsManager/map/overlays/showDataOverlay/showDataOverlay.ts @@ -9,7 +9,7 @@ import { } from '@/services/pluginsManager/errorMessages'; import { setBackgroundtoEmptyIfAvailable } from './setBackgroundtoEmptyIfAvailable'; -export const showDataOverlay = (overlayId: number): void => { +export const showDataOverlay = (overlayId: number, setBackgroundEmpty?: boolean): void => { const { dispatch, getState } = store; const state = getState(); const isOverlayActive = isOverlayActiveSelector(state, overlayId); @@ -24,7 +24,9 @@ export const showDataOverlay = (overlayId: number): void => { throw new Error(ERROR_OVERLAY_ID_ALREADY_ACTIVE); } - setBackgroundtoEmptyIfAvailable(); + if (setBackgroundEmpty) { + setBackgroundtoEmptyIfAvailable(); + } dispatch(getOverlayBioEntityForAllModels({ overlayId })); PluginsEventBus.dispatchEvent('onShowOverlay', matchingOverlay); -- GitLab