diff --git a/app/Http/Controllers/CityController.php b/app/Http/Controllers/CityController.php index 9345f2340..b846594da 100644 --- a/app/Http/Controllers/CityController.php +++ b/app/Http/Controllers/CityController.php @@ -3,77 +3,26 @@ namespace App\Http\Controllers; use App\City; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class CityController extends Controller { /** - * Display a listing of the resource. - * - * @return \Illuminate\Http\Response + * List cities for a country (profile city dropdown). */ - public function index() + public function index(Request $request): JsonResponse { - // - } - - /** - * Show the form for creating a new resource. - * - * @return \Illuminate\Http\Response - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - * - * @return \Illuminate\Http\Response - */ - public function store(Request $request) - { - // - } - - /** - * Display the specified resource. - * - * @return \Illuminate\Http\Response - */ - public function show(City $city) - { - // - } - - /** - * Show the form for editing the specified resource. - * - * @return \Illuminate\Http\Response - */ - public function edit(City $city) - { - // - } + $validated = $request->validate([ + 'country_iso' => 'required|string|size:2', + ]); - /** - * Update the specified resource in storage. - * - * @return \Illuminate\Http\Response - */ - public function update(Request $request, City $city) - { - // - } + $cities = City::query() + ->where('country_iso', strtoupper($validated['country_iso'])) + ->select(['id', 'city', 'country_iso']) + ->orderBy('city') + ->get(); - /** - * Remove the specified resource from storage. - * - * @return \Illuminate\Http\Response - */ - public function destroy(City $city) - { - // + return response()->json($cities); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e6f097df9..453f76246 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -114,15 +114,18 @@ function ($view) { $activeCountries = Country::query()->orderBy('name')->get(); - // Fix: filter cities to supported countries only (was using all 252 countries → 26k rows). - // Using $supportedCountries reduces the result set from ~26,500 to ~5,500 rows, - // cutting profile page load time by ~70%. - $cities = City::query() - ->whereIn('country_iso', $supportedCountries) + // Preload cities for the signed-in user's country only. Other countries are + // loaded on demand via /api/cities (cityFilter JS). Do not reuse + // $supportedCountries here — that list is for community hub copy and omits + // countries like Albania that still need a working city dropdown. + $countryIso = optional(auth()->user())->country_iso; + $cities = $countryIso + ? City::query() + ->where('country_iso', $countryIso) ->select(['id', 'city', 'country_iso']) - ->orderBy('country_iso') ->orderBy('city') - ->get(); + ->get() + : collect(); $view->with('cities', $cities); $view->with('active_countries', $activeCountries); diff --git a/public/js/ext/functions.js b/public/js/ext/functions.js index 2873af926..f8ef6c95c 100755 --- a/public/js/ext/functions.js +++ b/public/js/ext/functions.js @@ -2154,59 +2154,69 @@ var SEMICOLON = SEMICOLON || {}; var $country = $('#id_country'); var $city = $('#id_city'); if (!$country.length || !$city.length) return; - - if (!$city.data('originalHtml')) { - $city.data('originalHtml', $city.html()); + + var citiesUrl = $city.data('cities-url') || '/api/cities'; + var cache = {}; + var requestSeq = 0; + + function emptyOption() { + return $('').attr('value', ''); } - - function getAllOptions() { - var html = $city.data('originalHtml') || ''; - var $tmp = $(''); - return $tmp.find('option'); + + function fillCities(cities, prev) { + $city.empty().append(emptyOption()); + $.each(cities, function (_, row) { + $city.append( + $('') + .attr('value', row.id) + .attr('data-country', row.country_iso || '') + .text(row.city) + ); + }); + if (prev && $city.find('option[value="' + prev + '"]').length) { + $city.val(prev); + } else { + $city.val(''); + } } - + function applyCityFilter() { var iso = ($country.val() || '').toUpperCase(); var prev = $city.val(); - var $all = getAllOptions(); if (!iso) { - $city.html($all.clone()); - if ($city.find('option[value="'+ prev +'"]').length) $city.val(prev); + $city.empty().append(emptyOption()); return; } - - var $empty = $all.first().clone(); - var $filtered = $all.filter(function (i, opt) { - if (i === 0) return false; - var dc = (opt.getAttribute('data-country') || '').toUpperCase(); - return dc === iso; - }); - - $city.empty().append($empty).append($filtered.clone()); - - if ($city.find('option[value="'+ prev +'"]').length) { - $city.val(prev); - } else { - $city.val(''); // reset + + if (cache[iso]) { + fillCities(cache[iso], prev); + return; } + + var seq = ++requestSeq; + $.getJSON(citiesUrl, { country_iso: iso }) + .done(function (cities) { + if (seq !== requestSeq) return; + cache[iso] = cities || []; + fillCities(cache[iso], prev); + }) + .fail(function () { + if (seq !== requestSeq) return; + // Keep any server-rendered options for the current country. + var $kept = $city.find('option').filter(function () { + var dc = ($(this).attr('data-country') || '').toUpperCase(); + return !this.value || dc === iso; + }); + $city.empty().append(emptyOption()).append($kept.filter('[value!=""]')); + if (prev && $city.find('option[value="' + prev + '"]').length) { + $city.val(prev); + } else { + $city.val(''); + } + }); } - - (function autoSelectCountryFromCity() { - if ($country.val()) return; - var $sel = getAllOptions().filter(function(i, opt){ - return opt.value && opt.selected; - }).first(); - if ($sel.length) { - var dc = ($sel.attr('data-country') || '').toUpperCase(); - if (dc) { - var $opt = $country.find('option[value="'+ dc +'"]'); - if ($opt.length) $country.val(dc); - } - } - })(); - - // bind & run + $country.off('change.cityfilter').on('change.cityfilter', applyCityFilter); applyCityFilter(); }, diff --git a/resources/views/profile.blade.php b/resources/views/profile.blade.php index 67467c817..97899471e 100755 --- a/resources/views/profile.blade.php +++ b/resources/views/profile.blade.php @@ -276,6 +276,7 @@ class="border-2 border-solid border-dark-blue-200 w-full rounded-full h-12 px-4 @endif