From b6c6364933a2048a9f1f10f1c3ce8fb1085006b8 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Wed, 25 Nov 2026 11:54:00 +0000 Subject: [PATCH] Fix Kosovo activity submit resetting to step 1 with no error. ArcGIS returns a blank Country for Kosovo addresses, which threw in autocomplete, left geoposition as [], and caused EventsQuery explode() to 500. Co-authored-by: Cursor --- app/Queries/EventsQuery.php | 16 +++- .../assets/js/components/AutoCompleteGeo.vue | 16 +++- .../assets/js/components/allCountries.js | 1 + resources/js/components/AutocompleteGeo.vue | 78 ++++++++++++------- .../js/components/activity-form/FormStep1.vue | 10 ++- .../js/components/activity-form/index.vue | 18 ++++- resources/js/components/allCountries.js | 1 + 7 files changed, 100 insertions(+), 40 deletions(-) diff --git a/app/Queries/EventsQuery.php b/app/Queries/EventsQuery.php index 580cfb149..43c5b70a7 100644 --- a/app/Queries/EventsQuery.php +++ b/app/Queries/EventsQuery.php @@ -44,12 +44,20 @@ public static function store(Request $request) $request['geoposition'] = '0,0'; } - if (is_null($request['geoposition'])) { - $request['geoposition'] = '0,0'; + // Frontend may send [] / '' when address autocomplete failed to resolve coords + // (e.g. ArcGIS blank Country for Kosovo). Treat those as missing. + $geoposition = $request['geoposition'] ?? null; + if (is_array($geoposition)) { + $geoposition = implode(',', array_filter($geoposition, static fn ($v) => $v !== null && $v !== '')); + } + if (! is_string($geoposition) || ! preg_match('/^-?\d+(\.\d+)?\s*,\s*-?\d+(\.\d+)?$/', trim($geoposition))) { + $geoposition = '0,0'; } + $request['geoposition'] = $geoposition; - $request['latitude'] = explode(',', $request['geoposition'])[0]; - $request['longitude'] = explode(',', $request['geoposition'])[1]; + [$latitude, $longitude] = array_map('trim', explode(',', $request['geoposition'], 2)); + $request['latitude'] = $latitude; + $request['longitude'] = $longitude; if (empty($request['codeweek_for_all_participation_code'])) { $codeweek_4_all_generated_code = 'cw'.Carbon::now()->format('y').'-'.str_random(5); diff --git a/resources/assets/js/components/AutoCompleteGeo.vue b/resources/assets/js/components/AutoCompleteGeo.vue index 5395728e7..6bb4d8aec 100644 --- a/resources/assets/js/components/AutoCompleteGeo.vue +++ b/resources/assets/js/components/AutoCompleteGeo.vue @@ -56,18 +56,28 @@ url: baseURL + "&singleLine=" + item.name + "&magicKey=" + item.magicKey + "&outFields=Country", success: function (res) { var candidate = res.candidates[0]; + if (!candidate || !candidate.location) { + return; + } me.geoposition = [candidate.location.y, candidate.location.x]; if (window.map){ map.setView(me.geoposition,16); } - var countryIso2 = me.findCountry(candidate.attributes.Country).iso2; - $("#id_country").val(countryIso2); + // ArcGIS returns blank Country for disputed regions (e.g. Kosovo). + var countryIso2 = me.findCountry(candidate.attributes && candidate.attributes.Country); + if (countryIso2) { + $("#id_country").val(countryIso2); + } } }); } }, findCountry(iso3) { - return allCountries.find(country => country.iso3 === iso3); + if (!iso3) { + return undefined; + } + var match = allCountries.find(country => country.iso3 === iso3); + return match ? match.iso2 : undefined; }, getLabel (item) { if (item && item.name) { diff --git a/resources/assets/js/components/allCountries.js b/resources/assets/js/components/allCountries.js index 7ecddc9d9..ca9cad671 100644 --- a/resources/assets/js/components/allCountries.js +++ b/resources/assets/js/components/allCountries.js @@ -244,6 +244,7 @@ const allCountries = [ ["VI","VIR"], ["WF","WLF"], ["EH","ESH"], + ["XK","XKX"], ["YE","YEM"], ["ZM","ZMB"], ["ZW","ZWE"], diff --git a/resources/js/components/AutocompleteGeo.vue b/resources/js/components/AutocompleteGeo.vue index f3619d443..40ddb25d2 100644 --- a/resources/js/components/AutocompleteGeo.vue +++ b/resources/js/components/AutocompleteGeo.vue @@ -55,40 +55,60 @@ export default { }); const itemSelected = (selectedItem) => { - emit('onChange', { location: selectedItem?.name || '' }); - - if (selectedItem && selectedItem.name && selectedItem.magicKey) { - const baseURL = "/api/proxy/geocode"; // Update to your Laravel endpoint - axios.get(baseURL, { - params: { - singleLine: selectedItem.name, - magicKey: selectedItem.magicKey - } - }).then(response => { - const candidate = response.data.candidates[0]; - localGeoposition.value = [candidate.location.y, candidate.location.x]; - if (window.map) { - window.map.setView([candidate.location.y, candidate.location.x], 16); - } - const countryIso2 = findCountry(candidate.attributes.Country).iso2; - - emit('onChange', { - location: selectedItem?.name || '', - geoposition: [candidate.location.y, candidate.location.x], - country_iso: countryIso2 || '', - }); - - if (document.getElementById('id_country')) { - document.getElementById('id_country').value = countryIso2; - } - }).catch(error => { - console.error('Error:', error); - }); + if (!selectedItem?.name) { + return; } + + // Location text only — do not clear country/geoposition before geocode resolves. + // ArcGIS returns blank Country for disputed regions (e.g. Kosovo); that must not wipe the form. + emit('onChange', { location: selectedItem.name }); + + if (!selectedItem.magicKey) { + return; + } + + const baseURL = "/api/proxy/geocode"; + axios.get(baseURL, { + params: { + singleLine: selectedItem.name, + magicKey: selectedItem.magicKey + } + }).then(response => { + const candidate = response.data?.candidates?.[0]; + if (!candidate?.location) { + return; + } + + const geoposition = [candidate.location.y, candidate.location.x]; + localGeoposition.value = geoposition; + if (window.map) { + window.map.setView(geoposition, 16); + } + + const countryIso2 = findCountry(candidate.attributes?.Country)?.iso2; + const payload = { + location: selectedItem.name, + geoposition, + }; + if (countryIso2) { + payload.country_iso = countryIso2; + } + + emit('onChange', payload); + + if (countryIso2 && document.getElementById('id_country')) { + document.getElementById('id_country').value = countryIso2; + } + }).catch(error => { + console.error('Error:', error); + }); }; const findCountry = (iso3) => { + if (!iso3) { + return undefined; + } return allCountries.find(country => country.iso3 === iso3); }; diff --git a/resources/js/components/activity-form/FormStep1.vue b/resources/js/components/activity-form/FormStep1.vue index b70245ed6..db286f33e 100644 --- a/resources/js/components/activity-form/FormStep1.vue +++ b/resources/js/components/activity-form/FormStep1.vue @@ -150,8 +150,14 @@ export default { const handleLocationChange = ({ location, geoposition, country_iso }) => { props.formValues.location = location || ''; - props.formValues.geoposition = geoposition; - props.formValues.country_iso = country_iso; + // Only update fields that the geocoder actually resolved. Blank ArcGIS Country + // (e.g. Kosovo) must not clear a country the user already selected. + if (geoposition !== undefined) { + props.formValues.geoposition = geoposition; + } + if (country_iso !== undefined) { + props.formValues.country_iso = country_iso; + } props.formValues.locationSelected = true; props.formValues.locationDirty = true; }; diff --git a/resources/js/components/activity-form/index.vue b/resources/js/components/activity-form/index.vue index 3c8f6f410..ec80ca2ea 100644 --- a/resources/js/components/activity-form/index.vue +++ b/resources/js/components/activity-form/index.vue @@ -354,7 +354,12 @@ export default { activity_format: values.activity_format?.join(','), activity_type: values.activity_type, location: values.location, - geoposition: values.geoposition?.join(',') || [], + // Empty array becomes [] and makes EventsQuery explode() 500 — send null instead. + geoposition: Array.isArray(values.geoposition) && values.geoposition.length + ? values.geoposition.join(',') + : (typeof values.geoposition === 'string' && values.geoposition + ? values.geoposition + : null), duration: values.duration, start_date: values.start_date, end_date: values.end_date, @@ -400,7 +405,16 @@ export default { } handleMoveStep(4); } catch (error) { - errors.value = error.response?.data?.errors; + errors.value = error.response?.data?.errors || {}; + // Surface non-validation failures (e.g. 500) instead of silently jumping to step 1. + if (!error.response?.data?.errors) { + errors.value = { + title: [ + error.response?.data?.message + || 'Something went wrong while saving. Please try again, or clear the address and leave it empty for online activities.', + ], + }; + } step.value = 1; } finally { diff --git a/resources/js/components/allCountries.js b/resources/js/components/allCountries.js index 7ecddc9d9..ca9cad671 100644 --- a/resources/js/components/allCountries.js +++ b/resources/js/components/allCountries.js @@ -244,6 +244,7 @@ const allCountries = [ ["VI","VIR"], ["WF","WLF"], ["EH","ESH"], + ["XK","XKX"], ["YE","YEM"], ["ZM","ZMB"], ["ZW","ZWE"],