Skip to content

Commit

Permalink
Merge pull request #422 from DEFRA/fixing-missing-title-part-url-id
Browse files Browse the repository at this point in the history
fixed router missing title part and added underscore to separate loca…
  • Loading branch information
ulysses-cognizant authored Jan 17, 2025
2 parents f4513ba + 898aa63 commit 9ca7577
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/server/locations/helpers/convert-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Converts a string into lowercase words joined by hyphens and removes commas.
*/
const convertStringToHyphenatedLowercaseWords = (input) => {
const removedHyphens = input.replace(/ - /g, ' ')
// Remove commas, convert to lowercase, and split the string into words
const words = input.replace(/,/g, '').toLowerCase().split(' ')
const words = removedHyphens.replace(/,/g, '').toLowerCase().split(' ')

// Join the words with hyphens
return words.join('-')
Expand Down
18 changes: 15 additions & 3 deletions src/server/locations/helpers/middleware-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,24 @@ const processMatches = (matches, locationNameOrPostcode, userLocation) => {

return newMatches.reduce((acc, item) => {
let headerTitle = ''
let urlRoute = ''
if (item.GAZETTEER_ENTRY.DISTRICT_BOROUGH) {
if (item.GAZETTEER_ENTRY.NAME2) {
headerTitle = `${item.GAZETTEER_ENTRY.NAME2}, ${item.GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
urlRoute = `${item.GAZETTEER_ENTRY.NAME2}_${item.GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
} else {
headerTitle = `${item.GAZETTEER_ENTRY.NAME1}, ${item.GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
urlRoute = `${item.GAZETTEER_ENTRY.NAME1}_${item.GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
}
} else {
headerTitle = `${locationNameOrPostcode}, ${item.GAZETTEER_ENTRY.COUNTY_UNITARY}`
headerTitle = item.GAZETTEER_ENTRY.NAME2
? `${item.GAZETTEER_ENTRY.NAME2}, ${item.GAZETTEER_ENTRY.COUNTY_UNITARY}`
: `${item.GAZETTEER_ENTRY.NAME1}, ${item.GAZETTEER_ENTRY.COUNTY_UNITARY}`
urlRoute = item.GAZETTEER_ENTRY.NAME2
? `${item.GAZETTEER_ENTRY.NAME2}_${item.GAZETTEER_ENTRY.COUNTY_UNITARY}`
: `${item.GAZETTEER_ENTRY.NAME1}_${item.GAZETTEER_ENTRY.COUNTY_UNITARY}`
}
headerTitle = convertStringToHyphenatedLowercaseWords(headerTitle)
headerTitle = convertStringToHyphenatedLowercaseWords(urlRoute) // Use the helper function to generate the custom ID
item.GAZETTEER_ENTRY.ID = headerTitle // Update the nested object property
acc.push(item)
return acc
Expand All @@ -186,24 +194,28 @@ const processMatches = (matches, locationNameOrPostcode, userLocation) => {
const getTitleAndHeaderTitle = (locationDetails, locationNameOrPostcode) => {
let title = ''
let headerTitle = ''
let urlRoute = ''
const { home } = english
if (locationDetails[0]) {
if (locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH) {
if (locationDetails[0].GAZETTEER_ENTRY.NAME2) {
title = `${locationDetails[0].GAZETTEER_ENTRY.NAME2}, ${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH} - ${home.pageTitle}`
headerTitle = `${locationDetails[0].GAZETTEER_ENTRY.NAME2}, ${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
urlRoute = `${locationDetails[0].GAZETTEER_ENTRY.NAME2}_${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
} else {
title = `${locationDetails[0].GAZETTEER_ENTRY.NAME1}, ${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH} - ${home.pageTitle}`
headerTitle = `${locationDetails[0].GAZETTEER_ENTRY.NAME1}, ${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
urlRoute = `${locationDetails[0].GAZETTEER_ENTRY.NAME1}_${locationDetails[0].GAZETTEER_ENTRY.DISTRICT_BOROUGH}`
}
} else {
title = `${locationNameOrPostcode}, ${locationDetails[0].GAZETTEER_ENTRY.COUNTY_UNITARY} - ${home.pageTitle}`
headerTitle = `${locationNameOrPostcode}, ${locationDetails[0].GAZETTEER_ENTRY.COUNTY_UNITARY}`
urlRoute = `${locationNameOrPostcode}_${locationDetails[0].GAZETTEER_ENTRY.COUNTY_UNITARY}`
}
}
title = firstLetterUppercase(title)
headerTitle = firstLetterUppercase(headerTitle)
return { title, headerTitle }
return { title, headerTitle, urlRoute }
}

const getLanguageDates = (
Expand Down
5 changes: 2 additions & 3 deletions src/server/locations/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ const searchMiddleware = async (request, h) => {
)
userLocation = userLocation.toLowerCase()
userLocation = userLocation.charAt(0).toUpperCase() + userLocation.slice(1)
const { title, headerTitle } = getTitleAndHeaderTitle(
const { title, headerTitle, urlRoute } = getTitleAndHeaderTitle(
selectedMatches,
locationNameOrPostcode
)
const headerTitleRoute =
convertStringToHyphenatedLowercaseWords(headerTitle)
const headerTitleRoute = convertStringToHyphenatedLowercaseWords(urlRoute)
const titleRoute = convertStringToHyphenatedLowercaseWords(title)
const { forecastNum, nearestLocationsRange, airQuality } =
getNearestLocation(
Expand Down

0 comments on commit 9ca7577

Please sign in to comment.