From 5e192780b2bc5b280b61485117d102ee71066dea Mon Sep 17 00:00:00 2001 From: Han Date: Sat, 27 Jul 2024 14:02:39 +0100 Subject: [PATCH] Update sort order logic --- src/lib/data-helpers.js | 49 +++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/lib/data-helpers.js b/src/lib/data-helpers.js index faddfa5..15765a8 100644 --- a/src/lib/data-helpers.js +++ b/src/lib/data-helpers.js @@ -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) + ) } }