Skip to content

Commit

Permalink
Update sort order logic
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Jul 27, 2024
1 parent 1bb83d9 commit 5e19278
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/lib/data-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,42 @@ export const removeDuplicateServices = services => {
)
}

export const sortServices = (services, query) => {
/**
* tells us the query type to help with sorting
*/
const getQueryType = query => {
let { keywords, lat, lng, location } = queryString.parse(query)
// sorting
// if there is a location then sort by distance_away
// if there is a keyword and no location then the order matters
// otherwise sort by updated_at
if (!keywords && (lat || lng || location)) {
return services.sort(
(a, b) => new Date(a.distance_away) - new Date(b.distance_away)
)
} else if (keywords && !(lat || lng || location)) {
return services.sort((a, b) => new Date(b.score) - new Date(a.score))
const hasLocation = lat || lng || location
if (keywords && !hasLocation) {
return "keyword"
} else if (keywords === undefined && hasLocation) {
return "location"
} else if (keywords && hasLocation) {
return "keyword_location"
} else {
return services.sort(
(a, b) => new Date(b.updated_at) - new Date(a.updated_at)
)
return undefined
}
}

/**
* We only do this because we're returning multiple queries
* @param {*} services
* @param {*} query
* @returns
*/
export const sortServices = (services, query) => {
const queryType = getQueryType(query)
console.log(queryType)
switch (queryType) {
case "location":
return services.sort((a, b) => a.distance_away - b.distance_away)
case "keyword_location":
return services.sort((a, b) => a.distance_away - b.distance_away)
case "keyword":
return services.sort((a, b) => b.score - a.score)
default:
return services.sort(
(a, b) => new Date(b.updated_at) - new Date(a.updated_at)
)
}
}

0 comments on commit 5e19278

Please sign in to comment.