Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"framer-motion": "^4.1.13",
"history": "^5.0.0",
"html-react-parser": "^3.0.4",
"leaflet": "^1.9.4",
"lodash": "^4.17.21",
"moment": "^2.29.3",
"notistack": "^2.0.4",
Expand All @@ -42,6 +43,7 @@
"react-datepicker": "^4.21.0",
"react-device-detect": "^1.17.0",
"react-dom": "^18.2.0",
"react-leaflet": "^4.2.1",
"react-markdown": "^8.0.6",
"react-perfect-scrollbar": "^1.5.8",
"react-redux": "^8.0.5",
Expand Down
81 changes: 81 additions & 0 deletions packages/ui/src/api/infrastructure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
militaryBases,
powerPlants,
electricalSubstations,
gasInfrastructure,
waterInfrastructure,
dataCenterSites,
allInfrastructure
} from '@/data/infrastructureData'

// API functions for infrastructure data
// Currently using static data, but can be extended to fetch from backend

const getAllInfrastructure = () => {
return Promise.resolve(allInfrastructure)
}

const getMilitaryBases = () => {
return Promise.resolve(militaryBases)
}

const getPowerPlants = () => {
return Promise.resolve(powerPlants)
}

const getElectricalSubstations = () => {
return Promise.resolve(electricalSubstations)
}

const getGasInfrastructure = () => {
return Promise.resolve(gasInfrastructure)
}

const getWaterInfrastructure = () => {
return Promise.resolve(waterInfrastructure)
}

const getDataCenterSites = () => {
return Promise.resolve(dataCenterSites)
}

const getInfrastructureByType = (type) => {
switch (type) {
case 'military':
return getMilitaryBases()
case 'power':
return getPowerPlants()
case 'electrical':
return getElectricalSubstations()
case 'gas':
return getGasInfrastructure()
case 'water':
return getWaterInfrastructure()
case 'datacenter':
return getDataCenterSites()
default:
return getAllInfrastructure()
}
}
Comment on lines +42 to +59
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The switch statement in getInfrastructureByType is functional but verbose. Using a map to associate types with their data-fetching functions would make this code more concise, easier to read, and simpler to extend with new infrastructure types in the future.

const getInfrastructureByType = (type) => {
    const typeMap = {
        military: getMilitaryBases,
        power: getPowerPlants,
        electrical: getElectricalSubstations,
        gas: getGasInfrastructure,
        water: getWaterInfrastructure,
        datacenter: getDataCenterSites
    }
    const getter = typeMap[type]
    return getter ? getter() : getAllInfrastructure()
}


const getInfrastructureById = (id) => {
const item = allInfrastructure.find((item) => item.id === id)
return Promise.resolve(item)
}

// Future: Add functions to fetch from backend API
// const getAllInfrastructure = () => client.get('/api/v1/infrastructure')
// const getMilitaryBases = () => client.get('/api/v1/infrastructure/military')
// etc.

export default {
getAllInfrastructure,
getMilitaryBases,
getPowerPlants,
getElectricalSubstations,
getGasInfrastructure,
getWaterInfrastructure,
getDataCenterSites,
getInfrastructureByType,
getInfrastructureById
}
Loading