Skip to content

Commit

Permalink
[Tech] refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Dec 11, 2024
1 parent 76b3ef8 commit e0cabbc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
11 changes: 7 additions & 4 deletions frontend/src/domain/shared_slices/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CoordinatesFormat, DistanceUnit } from '../entities/map/constants'
import type { Coordinate } from 'ol/coordinate'
import type { Extent } from 'ol/extent'

type LayerType = {
export type IsolatedLayerType = {
id: number
isFilled: boolean
type: RegulatoryOrAMPOrViglanceAreaLayerType
Expand All @@ -16,10 +16,10 @@ type MapSliceStateType = {
coordinatesFormat: CoordinatesFormat
currentMapExtentTracker?: number[]
distanceUnit: DistanceUnit
excludedLayers?: Omit<LayerType, 'isFilled'>[]
excludedLayers?: Omit<IsolatedLayerType, 'isFilled'>[]
fitToExtent?: Extent
isAreaSelected: boolean
isolatedLayer: LayerType | undefined
isolatedLayer: IsolatedLayerType | undefined
selectedBaseLayer: string
zoomToCenter?: Coordinate
}
Expand Down Expand Up @@ -86,7 +86,10 @@ const mapSlice = createSlice({
},
setIsolateMode(
state,
action: PayloadAction<{ excludedLayers: Omit<LayerType, 'isFilled'>[]; isolatedLayer: LayerType | undefined }>
action: PayloadAction<{
excludedLayers: Omit<IsolatedLayerType, 'isFilled'>[]
isolatedLayer: IsolatedLayerType | undefined
}>
) {
state.isolatedLayer = action.payload.isolatedLayer
state.excludedLayers = action.payload.excludedLayers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getIsolatedLayerIsVigilanceArea, getVigilanceAreaExcludedLayers } from '@features/map/layers/utils'
import { useGetFilteredVigilanceAreasQuery } from '@features/VigilanceArea/hooks/useGetFilteredVigilanceAreasQuery'
import { useAppSelector } from '@hooks/useAppSelector'
import { Layers } from 'domain/entities/layers/constants'
Expand Down Expand Up @@ -45,17 +46,15 @@ export function PreviewVigilanceAreasLayer({ map }: BaseMapChildrenProps) {
if (vigilanceAreaSearchResult ?? vigilanceAreas) {
const vigilanceAreasToDisplay = vigilanceAreaSearchResult ?? vigilanceAreas?.ids ?? []

const isolatedLayerIsVigilanceArea = (isolatedLayer?.type.search('VIGILANCE_AREA') ?? -1) > -1
const regulatoryExcludedLayers = excludedLayers
?.filter(layer => layer.type.search('VIGILANCE_AREA') > -1)
.map(layer => layer.id)
const isolatedLayerIsVigilanceArea = getIsolatedLayerIsVigilanceArea(isolatedLayer)
const vigilanceAreasExcludedLayers = getVigilanceAreaExcludedLayers(excludedLayers)

const featuresToDisplay = vigilanceAreasToDisplay.filter(id => {
if (isolatedLayerIsVigilanceArea && id === isolatedLayer?.id) {
return false
}

return !regulatoryExcludedLayers?.map(excludeLayerId => excludeLayerId).includes(id)
return !vigilanceAreasExcludedLayers?.map(excludeLayerId => excludeLayerId).includes(id)
})

features = featuresToDisplay.reduce((amplayers, id) => {
Expand All @@ -72,7 +71,7 @@ export function PreviewVigilanceAreasLayer({ map }: BaseMapChildrenProps) {
}

return features
}, [excludedLayers, isolatedLayer?.id, isolatedLayer?.type, vigilanceAreaSearchResult, vigilanceAreas])
}, [excludedLayers, isolatedLayer, vigilanceAreaSearchResult, vigilanceAreas])

useEffect(() => {
vectorSourceRef.current?.clear(true)
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/features/map/layers/AMP/AMPPreviewLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getAMPLayerStyle } from './AMPLayers.style'
import { useGetAMPsQuery } from '../../../../api/ampsAPI'
import { Layers } from '../../../../domain/entities/layers/constants'
import { useAppSelector } from '../../../../hooks/useAppSelector'
import { getAmpExcludedLayers, getIsolatedLayerIsAmp } from '../utils'

import type { BaseMapChildrenProps } from '../../BaseMap'
import type { VectorLayerWithName } from 'domain/types/layer'
Expand Down Expand Up @@ -49,8 +50,8 @@ export function AMPPreviewLayer({ map }: BaseMapChildrenProps) {
if (ampsSearchResult || ampLayers?.entities) {
const ampsToDisplay = ampsSearchResult ?? ampLayers?.ids ?? []

const isolatedLayerTypeIsAmp = (isolatedLayer?.type?.search('AMP') ?? -1) > -1
const ampExcludedLayers = excludedLayers?.filter(layer => layer.type.search('AMP') > -1).map(layer => layer.id)
const isolatedLayerTypeIsAmp = getIsolatedLayerIsAmp(isolatedLayer)
const ampExcludedLayers = getAmpExcludedLayers(excludedLayers)

const featuresToDisplay = ampsToDisplay.filter(id => {
if (isolatedLayerTypeIsAmp && id === isolatedLayer?.id) {
Expand Down Expand Up @@ -87,8 +88,7 @@ export function AMPPreviewLayer({ map }: BaseMapChildrenProps) {
ampMetadataLayerId,
ampsSearchResult,
excludedLayers,
isolatedLayer?.id,
isolatedLayer?.type,
isolatedLayer,
showedAmpLayerIds
])

Expand Down
30 changes: 17 additions & 13 deletions frontend/src/features/map/layers/IsolationLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { getAMPFeature } from './AMP/AMPGeometryHelpers'
import { getIsolateAMPLayerStyle } from './AMP/AMPLayers.style'
import { getRegulatoryFeature } from './Regulatory/regulatoryGeometryHelpers'
import { getIsolateRegulatoryLayerStyle } from './styles/administrativeAndRegulatoryLayers.style'
import {
getAmpExcludedLayers,
getIsolatedLayerIsAmp,
getIsolatedLayerIsRegulatoryArea,
getIsolatedLayerIsVigilanceArea,
getRegulatoryExcludedLayers,
getVigilanceAreaExcludedLayers
} from './utils'
import { useGetRegulatoryLayersQuery } from '../../../api/regulatoryLayersAPI'
import { TWO_MINUTES } from '../../../constants'
import { Layers } from '../../../domain/entities/layers/constants'
Expand Down Expand Up @@ -44,9 +52,8 @@ export function IsolationLayer({ map }: BaseMapChildrenProps) {
const regulatoryLayersFeatures = useMemo(() => {
let regulatoryFeatures: Feature[] = []
if (regulatoryLayers?.entities) {
const isolatedLayerTypeIsRegulatory = (isolatedLayer?.type.search('REGULATORY') ?? -1) > -1
const regulatoryExcludedLayers =
excludedLayers?.filter(layer => layer.type.search('REGULATORY') > -1).map(layer => layer.id) ?? []
const isolatedLayerTypeIsRegulatory = getIsolatedLayerIsRegulatoryArea(isolatedLayer)
const regulatoryExcludedLayers = getRegulatoryExcludedLayers(excludedLayers)

let featuresToDisplay = regulatoryExcludedLayers ?? []
if (isolatedLayerTypeIsRegulatory && isolatedLayer?.id) {
Expand Down Expand Up @@ -77,9 +84,9 @@ export function IsolationLayer({ map }: BaseMapChildrenProps) {
let ampFeatures: Feature[] = []

if (ampLayers?.entities) {
const isolatedLayerTypeIsAmp = (isolatedLayer?.type.search('AMP') ?? -1) > -1
const ampExcludedLayers =
excludedLayers?.filter(layer => (layer.type.search('AMP') ?? -1) > -1).map(layer => layer.id) ?? []
const isolatedLayerTypeIsAmp = getIsolatedLayerIsAmp(isolatedLayer)
const ampExcludedLayers = getAmpExcludedLayers(excludedLayers)

let featuresToDisplay = ampExcludedLayers ?? []
if (isolatedLayerTypeIsAmp && isolatedLayer?.id) {
featuresToDisplay = [...featuresToDisplay, isolatedLayer.id]
Expand All @@ -88,7 +95,7 @@ export function IsolationLayer({ map }: BaseMapChildrenProps) {
ampFeatures = featuresToDisplay.reduce((amplayers, id) => {
const layer = id ? ampLayers?.entities[id] : undefined

if (layer && layer.geom) {
if (layer?.geom) {
const feature = getAMPFeature({ code: Layers.AMP_PREVIEW.code, layer })
const featureIsFilled = isolatedLayerTypeIsAmp && isolatedLayer?.id === id && isolatedLayer?.isFilled

Expand All @@ -107,20 +114,17 @@ export function IsolationLayer({ map }: BaseMapChildrenProps) {

const vigilanceAreasLayersFeatures = useMemo(() => {
let vigilanceAreasFeatures: Feature[] = []
// console.log('vigilanceAreas?.entities', vigilanceAreas?.entities)
if (vigilanceAreas?.entities) {
const isolatedLayerIsVigilanceArea = (isolatedLayer?.type.search('VIGILANCE_AREA') ?? -1) > -1
const vigilanceAreasExcludedLayers = excludedLayers
?.filter(layer => (layer.type.search('VIGILANCE_AREA') ?? -1) > -1)
.map(layer => layer.id)
const isolatedLayerIsVigilanceArea = getIsolatedLayerIsVigilanceArea(isolatedLayer)
const vigilanceAreasExcludedLayers = getVigilanceAreaExcludedLayers(excludedLayers)

let featuresToDisplay = vigilanceAreasExcludedLayers ?? []
if (isolatedLayerIsVigilanceArea && isolatedLayer?.id) {
featuresToDisplay = [...featuresToDisplay, isolatedLayer.id]
}
vigilanceAreasFeatures = featuresToDisplay.reduce((vigilanceAreasLayers, id) => {
const layer = id ? vigilanceAreas?.entities[id] : undefined
if (layer && layer.geom) {
if (layer?.geom) {
const feature = getVigilanceAreaZoneFeature(layer, Layers.VIGILANCE_AREA.code)
const featureIsFilled = isolatedLayerIsVigilanceArea && isolatedLayer?.id === id && isolatedLayer?.isFilled
if (feature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useGetRegulatoryLayersQuery } from '../../../../api/regulatoryLayersAPI
import { Layers } from '../../../../domain/entities/layers/constants'
import { useAppSelector } from '../../../../hooks/useAppSelector'
import { getRegulatoryLayerStyle } from '../styles/administrativeAndRegulatoryLayers.style'
import { getIsolatedLayerIsRegulatoryArea, getRegulatoryExcludedLayers } from '../utils'

import type { BaseMapChildrenProps } from '../../BaseMap'
import type { VectorLayerWithName } from 'domain/types/layer'
Expand Down Expand Up @@ -49,10 +50,8 @@ export function RegulatoryPreviewLayer({ map }: BaseMapChildrenProps) {
if (regulatoryLayersSearchResult || regulatoryLayers?.ids) {
const regulatoryAreasToDisplay = regulatoryLayersSearchResult ?? regulatoryLayers?.ids ?? []

const isolatedLayerTypeIsRegulatory = (isolatedLayer?.type.search('REGULATORY') ?? -1) > -1
const regulatoryExcludedLayers = excludedLayers
?.filter(layer => layer.type.search('REGULATORY') > -1)
.map(layer => layer.id)
const isolatedLayerTypeIsRegulatory = getIsolatedLayerIsRegulatoryArea(isolatedLayer)
const regulatoryExcludedLayers = getRegulatoryExcludedLayers(excludedLayers)

const featuresToDisplay = regulatoryAreasToDisplay.filter(id => {
if (isolatedLayerTypeIsRegulatory && id === isolatedLayer?.id) {
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/features/map/layers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import type { IsolatedLayerType } from 'domain/shared_slices/Map'
import type { SerializedFeature } from 'domain/types/map'

export const hasAlreadyFeature = (
currentFeatureOver: SerializedFeature<Record<string, any>> | undefined,
layersId: string[]
) => layersId.some(layerId => typeof currentFeatureOver?.id === 'string' && currentFeatureOver.id.includes(layerId))

export const getIsolatedLayerIsRegulatoryArea = (isolatedLayer: IsolatedLayerType | undefined) =>
(isolatedLayer?.type.search('REGULATORY') ?? -1) > -1

export const getRegulatoryExcludedLayers = (excludedLayers: Omit<IsolatedLayerType, 'isFilled'>[] | undefined) =>
excludedLayers?.filter(layer => layer.type.search('REGULATORY') > -1).map(layer => layer.id) ?? []

export const getIsolatedLayerIsAmp = (isolatedLayer: IsolatedLayerType | undefined) =>
(isolatedLayer?.type.search('AMP') ?? -1) > -1

export const getAmpExcludedLayers = (excludedLayers: Omit<IsolatedLayerType, 'isFilled'>[] | undefined) =>
excludedLayers?.filter(layer => (layer.type.search('AMP') ?? -1) > -1).map(layer => layer.id) ?? []

export const getIsolatedLayerIsVigilanceArea = (isolatedLayer: IsolatedLayerType | undefined) =>
(isolatedLayer?.type.search('VIGILANCE_AREA') ?? -1) > -1

export const getVigilanceAreaExcludedLayers = (excludedLayers: Omit<IsolatedLayerType, 'isFilled'>[] | undefined) =>
excludedLayers?.filter(layer => (layer.type.search('VIGILANCE_AREA') ?? -1) > -1).map(layer => layer.id)

0 comments on commit e0cabbc

Please sign in to comment.