Nexus-InglineSystems/app/Livewire/Admin/EditClient.php

54 lines
1.4 KiB
PHP

<?php
namespace App\Livewire\Admin;
use App\Livewire\Forms\ClientForm;
use App\Models\Client;
use App\Services\ClientService;
use Livewire\Attributes\On;
use Livewire\Component;
use Exception;
class EditClient extends Component
{
public Client $client;
public ClientForm $clientForm;
// public ClientService $clientService;
#[On('update-client')]
public function loadClient($id)
{
try {
$this->client = Client::find($id);
if ($this->client) {
$this->clientForm->addClient($this->client);
}
} catch (Exception $e) {
$this->dispatch('notify', message: 'Ocorreu um erro inesperado ao editar o cliente. ' + $e);
}
}
public function edit(ClientService $clientService)
{
$data = $this->clientForm->validate();
try {
if (!$clientService->updateClient($this->client, $data)) {
throw new Exception('O serviço não confirmou a atualização.');
}
$this->dispatch('client-updated');
$this->dispatch('notify', message: $this->clientForm->client_name . ' atualizado com sucesso!');
} catch (Exception $e) {
$this->dispatch('notify', message: 'Falha na edição: ' . $e->getMessage(), type: 'error');
}
}
public function render()
{
return view('livewire.admin.edit-client');
}
}