*/ public array $clientOptions = []; public bool $unifiError = false; public static function modalMaxWidth(): string { return 'lg'; } public function mount(UnifiClient $unifi, ?string $person = null): void { try { $this->clientOptions = collect($unifi->clients()) ->map(fn ($c) => ['mac' => strtolower($c->mac ?? ''), 'label' => $c->name ?? $c->hostname ?? ($c->mac ?? '?')]) ->filter(fn ($o) => $o['mac'] !== '') ->sortBy('label', SORT_NATURAL | SORT_FLAG_CASE) ->values()->all(); } catch (\Throwable) { $this->unifiError = true; } if ($person !== null) { $model = Person::where('uuid', $person)->firstOrFail(); $this->editing = true; $this->personUuid = $model->uuid; $this->name = $model->name; $this->mac = $model->mac ?? ''; $this->currentAvatarUrl = $model->avatarUrl(); } } /** Validate the picture as soon as it's chosen, so a non-image shows a friendly error. */ public function updatedAvatar(): void { $this->validateOnly('avatar', ['avatar' => ['nullable', 'image', 'max:4096']]); } public function save() { $this->validate([ 'name' => 'required|string|max:100', 'mac' => ['nullable', 'string', 'max:32', Rule::unique('persons', 'mac')->ignore($this->personUuid, 'uuid')], 'avatar' => ['nullable', 'image', 'max:4096'], ]); $person = $this->editing ? Person::where('uuid', $this->personUuid)->firstOrFail() : new Person(['presence' => 'unknown']); $person->name = $this->name; $person->mac = $this->mac ?: null; if ($this->avatar !== null) { if ($person->avatar_path) { Storage::disk('public')->delete($person->avatar_path); } $person->avatar_path = $this->avatar->store('avatars', 'public'); } $person->save(); $this->closeModal(); return redirect()->route('persons.index'); } public function render() { return view('livewire.modals.manage-person'); } }