Skip to content

Commit

Permalink
Refactor code to reduce cognitive complexity and improve consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyurx11 committed Sep 5, 2024
1 parent 86b9c45 commit c5e178a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
60 changes: 32 additions & 28 deletions server/models/views/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class ViewModel {
this.river = river
const isForecast = forecast ? forecast.forecastFlag.display_time_series : false

// Define staion navigation properties based on the station type and qualifier
this.nav = {
upstream: getNavigationLink(river.rloi_id, river.station_type, river.qualifier, river.up, river.up_station_type, 'upstream'),
downstream: getNavigationLink(river.rloi_id, river.station_type, river.qualifier, river.down, river.down_station_type, 'downstream')
}
// Define station navigation properties based on the station type and qualifier
const upstreamNavigationLink = createNavigationLink(river, 'upstream')
const downstreamNavigationLink = createNavigationLink(river, 'downstream')

this.navigationLink = { upstream: upstreamNavigationLink, downstream: downstreamNavigationLink }

this.station.trend = river.trend

Expand Down Expand Up @@ -393,34 +393,38 @@ class ViewModel {
}
}

function getNavigationLink (currentStationId, currentStationType, currentStationQualifier, targetStationId, targetStationType, direction) {
if (!targetStationId) return null
function createNavigationLink (river, direction) {
const currentStationId = river.rloi_id
const currentStationType = river.station_type
const currentStationQualifier = river.qualifier
const targetStationId = direction === 'upstream' ? river.up : river.down
const targetStationType = direction === 'upstream' ? river.up_station_type : river.down_station_type

if (!targetStationId) {
return null
}

// Logic for multi-station
if (currentStationType === 'M') {
if (currentStationQualifier === 'u') {
if (direction === 'upstream') {
return `${targetStationId}`
} else if (direction === 'downstream') {
return `${currentStationId}/downstream`
}
} else if (currentStationQualifier === 'd') {
if (direction === 'upstream') {
return `${currentStationId}`
} else if (direction === 'downstream') {
return `${targetStationId}`
}
}
return createMultiStationNavLink(direction, currentStationQualifier, targetStationId, currentStationId)
} else {
// Logic for single-station
if (direction === 'upstream') {
return targetStationType === 'M' ? `${targetStationId}/downstream` : `${targetStationId}`
} else if (direction === 'downstream') {
return targetStationType === 'M' ? `${targetStationId}` : `${targetStationId}`
}
return createSingleStationNavLink(direction, targetStationType, targetStationId)
}
}

return null
function createMultiStationNavLink (direction, currentStationQualifier, targetStationId, currentStationId) {
if (currentStationQualifier === 'u') {
return direction === 'upstream' ? `${targetStationId}` : `${currentStationId}/downstream`
} else {
return direction === 'upstream' ? `${currentStationId}` : `${targetStationId}`
}
}

function createSingleStationNavLink (direction, targetStationType, targetStationId) {
if (direction === 'upstream') {
return targetStationType === 'M' ? `${targetStationId}/downstream` : `${targetStationId}`
} else {
return `${targetStationId}`
}
}

function getBannerIcon (id) {
Expand Down
8 changes: 4 additions & 4 deletions server/views/station.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ <h1 class="govuk-heading-xl govuk-!-margin-bottom-0">
<span id="map-live"></span>
<div class="defra-navbar__group">
<ul class="defra-navbar__list">
{% if model.nav.upstream %}
{% if model.navigationLink.upstream %}
<li class="defra-navbar__item">
<a href="/station/{{model.nav.upstream}}">Upstream</a>
<a href="/station/{{model.navigationLink.upstream}}">Upstream</a>
</li>
{% endif %}
{% if model.nav.downstream %}
{% if model.navigationLink.downstream %}
<li class="defra-navbar__item">
<a href="/station/{{model.nav.downstream}}">Downstream</a>
<a href="/station/{{model.navigationLink.downstream}}">Downstream</a>
</li>
{% endif %}
<li class="defra-navbar__item">
Expand Down

0 comments on commit c5e178a

Please sign in to comment.