Files
timebank-cc-public/app/Http/Livewire/Locations/LocationsDropdown.php
T

177 lines
4.9 KiB
PHP

<?php
namespace App\Http\Livewire\Locations;
use App\Models\Locations\City;
use App\Models\Locations\Country;
use App\Models\Locations\District;
use App\Models\Locations\Division;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class LocationsDropdown extends Component
{
public $country;
public $division;
public $city;
public $district;
public $hideLabel = false;
public bool $showCountry = true;
public bool $showDivision = true;
public bool $showCity = true;
public bool $showDistrict = true;
protected $listeners = ['countryToChildren', 'divisionToChildren', 'cityToChildren', 'districtToChildren'];
/**
* Receive country value from parent component.
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
*/
public function countryToChildren($value)
{
$this->country = $value;
// Don't call updatedCountry() as it resets division/city/district
// Parent is responsible for setting all values correctly
}
/**
* Receive division value from parent component.
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
*/
public function divisionToChildren($value)
{
$this->division = $value ?: null;
// Don't call updatedDivision() as it resets city/district
}
/**
* Receive city value from parent component.
* Does NOT reset downstream values - that would clear values being set by subsequent ToChildren calls.
*/
public function cityToChildren($value)
{
$this->city = $value ?: null;
// Don't call updatedCity() as it resets district
}
/**
* Receive district value from parent component.
*/
public function districtToChildren($value)
{
$this->district = $value ?: null;
// No downstream values to reset
}
public function updatedCountry()
{
$this->reset(['division', 'city', 'district']);
$this->dispatch('countryToParent', $this->country);
$this->dispatch('divisionToParent', null);
$this->dispatch('cityToParent', null);
$this->dispatch('districtToParent', null);
}
public function updatedDivision()
{
$this->reset(['city', 'district']);
if ($this->division === '') {
$this->division = null;
}
$this->dispatch('divisionToParent', $this->division);
$this->dispatch('cityToParent', null);
$this->dispatch('districtToParent', null);
}
public function updatedCity()
{
$this->reset(['district']);
if ($this->city === '') {
$this->city = null;
}
$this->dispatch('cityToParent', $this->city);
$this->dispatch('districtToParent', null);
}
public function updatedDistrict()
{
if ($this->district === '') {
$this->district = null;
}
$this->dispatch('districtToParent', $this->district);
}
public function countrySelected()
{
$this->dispatch('countryToParent', $this->country);
}
public function divisionSelected()
{
$this->dispatch('divisionToParent', $this->division);
}
public function citySelected()
{
$this->dispatch('cityToParent', $this->city);
}
public function districtSelected()
{
$this->dispatch('districtToParent', $this->district);
}
public function render()
{
$countries = Cache::remember(
'location_countries_all', 86400,
fn() => Country::with(['translations'])->get()
);
$divisions = collect();
$cities = collect();
$districts = collect();
if (!empty($this->country)) {
$divisions = Cache::remember(
'location_divisions_' . $this->country, 3600,
fn() => Division::with(['translations'])->where('country_id', $this->country)->get()
);
if (!empty($this->division)) {
$cities = Cache::remember(
'location_cities_division_' . $this->division, 3600,
fn() => City::with(['translations'])->where('division_id', $this->division)->get()
);
} else {
$cities = Cache::remember(
'location_cities_country_' . $this->country, 3600,
fn() => City::with(['translations'])->where('country_id', $this->country)->get()
);
}
}
if (!empty($this->city)) {
$districts = Cache::remember(
'location_districts_city_' . $this->city, 3600,
fn() => District::with(['translations'])->where('city_id', $this->city)->get()
);
}
return view('livewire.locations.locations-dropdown', compact('countries', 'divisions', 'cities', 'districts'));
}
}