60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Datacenter;
|
|
use Livewire\Attributes\Validate;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
/**
|
|
* Edit a datacenter's name and location in a modal (avoids the row-height jump
|
|
* of inline editing). The code is immutable — it is referenced by hosts. Country
|
|
* is picked from a list so it can't be mistyped.
|
|
*/
|
|
class EditDatacenter extends ModalComponent
|
|
{
|
|
public string $uuid = '';
|
|
|
|
public string $code = '';
|
|
|
|
#[Validate('required|string|max:255')]
|
|
public string $name = '';
|
|
|
|
public string $location = '';
|
|
|
|
public function mount(string $uuid): void
|
|
{
|
|
$dc = Datacenter::query()->where('uuid', $uuid)->firstOrFail();
|
|
$this->uuid = $uuid;
|
|
$this->code = $dc->code;
|
|
$this->name = $dc->name;
|
|
$this->location = (string) $dc->location;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->authorize('datacenters.manage');
|
|
// Validate the country against the configured list so add + edit stay in sync.
|
|
$data = $this->validate([
|
|
'name' => 'required|string|max:255',
|
|
'location' => 'nullable|in:'.implode(',', array_keys((array) config('countries'))),
|
|
]);
|
|
|
|
Datacenter::query()->where('uuid', $this->uuid)->update([
|
|
'name' => $data['name'],
|
|
'location' => $data['location'] ?: null,
|
|
]);
|
|
|
|
$this->dispatch('notify', message: __('datacenters.updated'));
|
|
|
|
return $this->redirectRoute('admin.datacenters', navigate: true);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.edit-datacenter', [
|
|
'countries' => config('countries'),
|
|
]);
|
|
}
|
|
}
|