diff --git a/src/components/FunctionalArea/MapNavigation/MapNavigation.component.tsx b/src/components/FunctionalArea/MapNavigation/MapNavigation.component.tsx index 3a6517e993b63d7c1269f146212bc7d87d423cc9..b5b638aa5d261fdf5a5ffa96a8d2a555d955601d 100644 --- a/src/components/FunctionalArea/MapNavigation/MapNavigation.component.tsx +++ b/src/components/FunctionalArea/MapNavigation/MapNavigation.component.tsx @@ -11,6 +11,8 @@ import { Icon } from '@/shared/Icon'; import { MouseEvent } from 'react'; import { twMerge } from 'tailwind-merge'; import { getComments } from '@/redux/comment/thunks/getComments'; +import { commentSelector } from '@/redux/comment/comment.selectors'; +import { hideComments, showComments } from '@/redux/comment/comment.slice'; export const MapNavigation = (): JSX.Element => { const dispatch = useAppDispatch(); @@ -21,6 +23,8 @@ export const MapNavigation = (): JSX.Element => { const isActive = (modelId: number): boolean => currentModelId === modelId; const isNotMainMap = (modelName: string): boolean => modelName !== MAIN_MAP; + const commentsOpen = useAppSelector(commentSelector).isOpen; + const onCloseSubmap = (event: MouseEvent<HTMLDivElement>, map: OppenedMap): void => { event.stopPropagation(); if (isActive(map.modelId)) { @@ -47,7 +51,12 @@ export const MapNavigation = (): JSX.Element => { }; const toggleComments = async (): Promise<void> => { - await dispatch(getComments()); + if (!commentsOpen) { + await dispatch(getComments()); + dispatch(showComments()); + } else { + dispatch(hideComments()); + } }; return ( diff --git a/src/components/Map/MapViewer/utils/config/commentsLayer/useOlMapCommentsLayer.ts b/src/components/Map/MapViewer/utils/config/commentsLayer/useOlMapCommentsLayer.ts index 638d7f4cc313308dfc13904c90790bc967a98b15..6eb9e2cbd4706836bcbb812e235b7a937d15d32c 100644 --- a/src/components/Map/MapViewer/utils/config/commentsLayer/useOlMapCommentsLayer.ts +++ b/src/components/Map/MapViewer/utils/config/commentsLayer/useOlMapCommentsLayer.ts @@ -5,22 +5,26 @@ import { Geometry } from 'ol/geom'; import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; import { useSelector } from 'react-redux'; -import { allCommentsSelectorOfCurrentMap } from '@/redux/comment/comment.selectors'; +import { + allCommentsSelectorOfCurrentMap, + commentSelector, +} from '@/redux/comment/comment.selectors'; import { getCommentsFeatures } from '@/components/Map/MapViewer/utils/config/commentsLayer/getCommentsFeatures'; import { useMemo } from 'react'; export const useOlMapCommentsLayer = (): VectorLayer<VectorSource<Feature<Geometry>>> => { const pointToProjection = usePointToProjection(); const comments = useSelector(allCommentsSelectorOfCurrentMap); + const isVisible = useSelector(commentSelector).isOpen; const elementsFeatures = useMemo( () => [ - getCommentsFeatures(comments, { + getCommentsFeatures(isVisible ? comments : [], { pointToProjection, }), ].flat(), - [comments, pointToProjection], + [comments, pointToProjection, isVisible], ); const vectorSource = useMemo(() => { diff --git a/src/redux/comment/comment.constants.ts b/src/redux/comment/comment.constants.ts index 389a12dc6f87d5eee517d7e0a623c28cb1f59363..36914070334299cf7d027a2e225937c3a0f43d7d 100644 --- a/src/redux/comment/comment.constants.ts +++ b/src/redux/comment/comment.constants.ts @@ -11,4 +11,5 @@ export const COMMENT_INITIAL_STATE: CommentsState = { data: [], loading: 'idle', error: { name: '', message: '' }, + isOpen: false, }; diff --git a/src/redux/comment/comment.mock.ts b/src/redux/comment/comment.mock.ts index 121df19f3c5dfcde599a6213c2f4c49ca756a949..e84f898da65376f1950279ff87e2c19366b8b148 100644 --- a/src/redux/comment/comment.mock.ts +++ b/src/redux/comment/comment.mock.ts @@ -5,4 +5,5 @@ export const COMMENT_INITIAL_STATE_MOCK: CommentsState = { data: [], loading: 'idle', error: DEFAULT_ERROR, + isOpen: false, }; diff --git a/src/redux/comment/comment.reducers.ts b/src/redux/comment/comment.reducers.ts index e9d5f098382c871c8654e721da794bc8fac19992..df94d01485e1418dd595333e2955a5a47c5466de 100644 --- a/src/redux/comment/comment.reducers.ts +++ b/src/redux/comment/comment.reducers.ts @@ -11,7 +11,16 @@ export const getCommentsReducer = (builder: ActionReducerMapBuilder<CommentsStat state.loading = 'succeeded'; state.data = action.payload; }); + builder.addCase(getComments.rejected, state => { state.loading = 'failed'; }); }; + +export const showCommentsReducer = (state: CommentsState): void => { + state.isOpen = true; +}; + +export const hideCommentsReducer = (state: CommentsState): void => { + state.isOpen = false; +}; diff --git a/src/redux/comment/comment.slice.ts b/src/redux/comment/comment.slice.ts index 9524be0572d605432d4760674d58643319d9a31e..2910a8727ddb8600ed1123571388441ef5a37cab 100644 --- a/src/redux/comment/comment.slice.ts +++ b/src/redux/comment/comment.slice.ts @@ -1,14 +1,23 @@ import { createSlice } from '@reduxjs/toolkit'; import { COMMENT_INITIAL_STATE } from '@/redux/comment/comment.constants'; -import { getCommentsReducer } from '@/redux/comment/comment.reducers'; +import { + getCommentsReducer, + hideCommentsReducer, + showCommentsReducer, +} from '@/redux/comment/comment.reducers'; export const commentsSlice = createSlice({ name: 'comments', initialState: COMMENT_INITIAL_STATE, - reducers: {}, + reducers: { + showComments: showCommentsReducer, + hideComments: hideCommentsReducer, + }, extraReducers: builder => { getCommentsReducer(builder); }, }); +export const { showComments, hideComments } = commentsSlice.actions; + export default commentsSlice.reducer; diff --git a/src/redux/comment/comment.types.ts b/src/redux/comment/comment.types.ts index 08975985f278f1611c09977133f1481641675d1a..b6389d9504f14cab6ff5c7d3feb9142eeb8a2d25 100644 --- a/src/redux/comment/comment.types.ts +++ b/src/redux/comment/comment.types.ts @@ -1,4 +1,6 @@ import { FetchDataState } from '@/types/fetchDataState'; import { Comment } from '@/types/models'; -export type CommentsState = FetchDataState<Comment[], []>; +export interface CommentsState extends FetchDataState<Comment[], []> { + isOpen: boolean; +}