-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dashboard layer and overlay to map
- Loading branch information
1 parent
27332e0
commit 9613270
Showing
15 changed files
with
577 additions
and
29 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
frontend/src/features/Dashboard/components/Overlays/DashboardCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { useGetDashboardQuery } from '@api/dashboardsAPI' | ||
import { editDashboard } from '@features/Dashboard/useCases/editDashboard' | ||
import { useAppDispatch } from '@hooks/useAppDispatch' | ||
import { Accent, Button, getLocalizedDayjs, Icon, IconButton, pluralize, Size } from '@mtes-mct/monitor-ui' | ||
import { closeAllOverlays } from 'domain/use_cases/map/closeAllOverlays' | ||
import { useEffect, useRef } from 'react' | ||
import styled from 'styled-components' | ||
|
||
type DashboardCardProps = { | ||
dashboardId: string | ||
isCardVisible?: boolean | ||
onClose: () => void | ||
selected?: boolean | ||
updateMargins: (margin: number) => void | ||
} | ||
|
||
export function DashboardCard({ | ||
dashboardId, | ||
isCardVisible = true, | ||
onClose, | ||
selected = false, | ||
updateMargins | ||
}: DashboardCardProps) { | ||
const dispatch = useAppDispatch() | ||
const ref = useRef<HTMLDivElement>(null) | ||
|
||
const { data: dashboard } = useGetDashboardQuery(dashboardId) | ||
|
||
useEffect(() => { | ||
if (dashboard && ref.current) { | ||
const cardHeight = ref.current.offsetHeight | ||
updateMargins(cardHeight === 0 ? 200 : cardHeight) | ||
} | ||
}, [dashboard, updateMargins]) | ||
if (!dashboard) { | ||
return null | ||
} | ||
const creationDate = getLocalizedDayjs(dashboard.createdAt).format('DD MMM YYYY') | ||
const numberOfItemsSelected = | ||
dashboard.amps.length + | ||
dashboard.controlUnits.length + | ||
dashboard.regulatoryAreas.length + | ||
dashboard.vigilanceAreas.length + | ||
dashboard.reportings.length | ||
|
||
return ( | ||
isCardVisible && ( | ||
<Wrapper ref={ref} data-cy="reporting-overlay"> | ||
<StyledHeader> | ||
<StyledHeaderFirstLine> | ||
<StyledBoldText>{dashboard.name}</StyledBoldText> | ||
<div> | ||
<StyledGrayText>Créé le {creationDate}</StyledGrayText> | ||
<StyledGrayText> | ||
{numberOfItemsSelected} {pluralize('élément', numberOfItemsSelected)}{' '} | ||
{pluralize('sélectionné', numberOfItemsSelected)} | ||
</StyledGrayText> | ||
</div> | ||
</StyledHeaderFirstLine> | ||
|
||
<CloseButton | ||
$isVisible={selected} | ||
accent={Accent.TERTIARY} | ||
data-cy="reporting-overlay-close" | ||
Icon={Icon.Close} | ||
iconSize={14} | ||
onClick={onClose} | ||
/> | ||
</StyledHeader> | ||
|
||
<StyledButton | ||
data-cy="map-edit-reporting" | ||
disabled={!selected} | ||
Icon={Icon.Edit} | ||
onClick={() => { | ||
dispatch(editDashboard(dashboard.id)) | ||
dispatch(closeAllOverlays()) | ||
}} | ||
size={Size.SMALL} | ||
> | ||
Éditer le tableau | ||
</StyledButton> | ||
</Wrapper> | ||
) | ||
) | ||
} | ||
|
||
const Wrapper = styled.div` | ||
padding: 10px; | ||
box-shadow: 0px 3px 6px #70778540; | ||
border-radius: 1px; | ||
background-color: ${p => p.theme.color.white}; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 16px; | ||
flex-basis: 0 0 345px; | ||
` | ||
const StyledHeader = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
align-items: start; | ||
justify-content: space-between; | ||
` | ||
const StyledGrayText = styled.span` | ||
color: ${p => p.theme.color.slateGray}; | ||
display: flex; | ||
align-items: baseline; | ||
` | ||
|
||
const StyledHeaderFirstLine = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: start; | ||
gap: 16px; | ||
${StyledGrayText} { | ||
max-width: 190px; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} | ||
` | ||
|
||
const CloseButton = styled(IconButton)<{ $isVisible: boolean }>` | ||
padding: 0px; | ||
margin-left: 8px; | ||
${p => !p.$isVisible && 'visibility: hidden;'}; | ||
` | ||
|
||
const StyledBoldText = styled.span` | ||
font-weight: 700; | ||
color: ${p => p.theme.color.gunMetal}; | ||
` | ||
|
||
// TODO delete when Monitor-ui component have good padding | ||
const StyledButton = styled(Button)` | ||
padding: 4px 12px; | ||
align-self: start; | ||
width: inherit; | ||
` |
110 changes: 110 additions & 0 deletions
110
frontend/src/features/Dashboard/components/Overlays/DashboardOverlay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { dashboardActions } from '@features/Dashboard/slice' | ||
import { OverlayPositionOnCentroid } from '@features/map/overlays/OverlayPositionOnCentroid' | ||
import { useAppDispatch } from '@hooks/useAppDispatch' | ||
import { useAppSelector } from '@hooks/useAppSelector' | ||
import { Layers } from 'domain/entities/layers/constants' | ||
import { isOverlayOpened } from 'domain/shared_slices/Global' | ||
import { convertToFeature } from 'domain/types/map' | ||
import { useState } from 'react' | ||
|
||
import { DashboardCard } from './DashboardCard' | ||
|
||
import type { BaseMapChildrenProps } from '@features/map/BaseMap' | ||
import type { VectorLayerWithName } from 'domain/types/layer' | ||
|
||
const OPTIONS = { | ||
margins: { | ||
xLeft: 50, | ||
xMiddle: 30, | ||
xRight: -55, | ||
yBottom: 50, | ||
yMiddle: 50, | ||
yTop: -55 | ||
} | ||
} | ||
export function DashboardOverlay({ currentFeatureOver, map, mapClickEvent }: BaseMapChildrenProps) { | ||
const dispatch = useAppDispatch() | ||
|
||
const [hoveredOptions, setHoveredOptions] = useState(OPTIONS) | ||
const [selectedOptions, setSelectedOptions] = useState(OPTIONS) | ||
const displayDashboardLayer = useAppSelector(state => state.global.displayDashboardLayer) | ||
|
||
const selectedDashboardOnMap = useAppSelector(state => state.dashboard.selectedDashboardOnMap) | ||
|
||
const feature = map | ||
?.getLayers() | ||
?.getArray() | ||
?.find( | ||
(l): l is VectorLayerWithName => | ||
Object.prototype.hasOwnProperty.call(l, 'name') && (l as VectorLayerWithName).name === Layers.DASHBOARDS.code | ||
) | ||
?.getSource() | ||
?.getFeatureById(`${Layers.DASHBOARDS.code}:${selectedDashboardOnMap?.id}`) | ||
|
||
const canOverlayBeOpened = useAppSelector(state => | ||
isOverlayOpened(state.global, `${Layers.DASHBOARDS.code}:${selectedDashboardOnMap?.id}`) | ||
) | ||
|
||
const hoveredFeature = convertToFeature(currentFeatureOver) | ||
|
||
const currentfeatureId = hoveredFeature?.getId() | ||
|
||
const displayHoveredFeature = | ||
typeof currentfeatureId === 'string' && | ||
currentfeatureId.startsWith(Layers.DASHBOARDS.code) && | ||
currentfeatureId !== `${Layers.DASHBOARDS.code}:${selectedDashboardOnMap?.id}` && | ||
hoveredFeature?.getProperties().dashboard | ||
|
||
const updateSelectedMargins = (cardHeight: number) => { | ||
if (OPTIONS.margins.yTop - cardHeight !== selectedOptions.margins.yTop) { | ||
setSelectedOptions({ margins: { ...selectedOptions.margins, yTop: OPTIONS.margins.yTop - cardHeight } }) | ||
} | ||
} | ||
const updateHoveredMargins = (cardHeight: number) => { | ||
if (OPTIONS.margins.yTop - cardHeight !== hoveredOptions.margins.yTop) { | ||
setHoveredOptions({ margins: { ...hoveredOptions.margins, yTop: OPTIONS.margins.yTop - cardHeight } }) | ||
} | ||
} | ||
|
||
const close = () => { | ||
dispatch(dashboardActions.setSelectedDashboardOnMap(undefined)) | ||
} | ||
|
||
return ( | ||
<> | ||
<OverlayPositionOnCentroid | ||
appClassName="overlay-reporting-selected" | ||
feature={displayDashboardLayer && canOverlayBeOpened ? feature : undefined} | ||
map={map} | ||
mapClickEvent={mapClickEvent} | ||
options={selectedOptions} | ||
zIndex={5000} | ||
> | ||
{selectedDashboardOnMap && ( | ||
<DashboardCard | ||
dashboardId={selectedDashboardOnMap?.id} | ||
onClose={close} | ||
selected | ||
updateMargins={updateSelectedMargins} | ||
/> | ||
)} | ||
</OverlayPositionOnCentroid> | ||
<OverlayPositionOnCentroid | ||
appClassName="overlay-reporting-hover" | ||
feature={displayHoveredFeature ? hoveredFeature : undefined} | ||
map={map} | ||
mapClickEvent={mapClickEvent} | ||
options={hoveredOptions} | ||
zIndex={5000} | ||
> | ||
{hoveredFeature?.getProperties().dashboard && ( | ||
<DashboardCard | ||
dashboardId={hoveredFeature?.getProperties().dashboard.id} | ||
onClose={close} | ||
updateMargins={updateHoveredMargins} | ||
/> | ||
)} | ||
</OverlayPositionOnCentroid> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.