diff --git a/server/models/views/station.js b/server/models/views/station.js index 44d245c6..66356271 100644 --- a/server/models/views/station.js +++ b/server/models/views/station.js @@ -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 @@ -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) { diff --git a/server/views/station.html b/server/views/station.html index b3f72536..20425c81 100644 --- a/server/views/station.html +++ b/server/views/station.html @@ -40,14 +40,14 @@