From 959a00732767441af22b96fbd6bd0cd5a35ee95a Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Sun, 29 Dec 2024 22:36:52 -0800 Subject: [PATCH] fix: image location for location input with two parts --- assets/js/search_results.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/assets/js/search_results.js b/assets/js/search_results.js index 6141992d..a353bb7a 100644 --- a/assets/js/search_results.js +++ b/assets/js/search_results.js @@ -43,7 +43,19 @@ async function getImageForLocation() { console.log("calling image generation function..."); const location = getLocation(); const url = `/v1/gen_image`; - let parts = location.split(", "); + const parts = location.split(",").map((str) => str.trim()); + if (parts.length < 2 || parts.length > 3) { + console.log("wrong location input format", location); + $("#loadingSpinner").hide(); + return; + } + + let city = parts[0]; + let country = parts[parts.length - 1]; + let adminAreaLevelOne = "cities"; + if (parts.length == 3) { + adminAreaLevelOne = parts[1]; + } await fetch(url, { method: "POST", @@ -51,9 +63,9 @@ async function getImageForLocation() { "Content-Type": "application/json", }, body: JSON.stringify({ - city: parts[0], - adminAreaLevelOne: parts[1], - country: parts[2], + city: city, + adminAreaLevelOne: adminAreaLevelOne, + country: country, }), }) .then((response) => response.json())