You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
106 lines
2.7 KiB
<?php
|
|
|
|
use App\Domains\Channel\Actions\Api\AssignEndpointUrl;
|
|
use App\Domains\Channel\DTOs\Api\EndpointUrlDTO;
|
|
use App\Domains\Channel\Enums\Api\ApiType;
|
|
use App\Domains\Channel\Models\ApiEndpoint;
|
|
use App\Domains\Channel\Models\Client;
|
|
use App\Livewire\Concerns\WithModal;
|
|
use App\Livewire\Concerns\WithToast;
|
|
use Illuminate\Support\Collection;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
new class extends Component
|
|
{
|
|
use WithModal;
|
|
use WithToast;
|
|
|
|
#[Locked]
|
|
public ?string $id = null;
|
|
|
|
#[Locked]
|
|
public string $mode = 'view';
|
|
|
|
#[Validate([
|
|
'field.*.client_id' => 'required',
|
|
'field.*.value' => 'required',
|
|
])]
|
|
public array $field = [];
|
|
|
|
protected string $resourceName = 'client-api';
|
|
|
|
public function show(int|string $id): void
|
|
{
|
|
$this->id = $id;
|
|
}
|
|
|
|
public function hide(): void
|
|
{
|
|
$this->reset('id', 'mode');
|
|
}
|
|
|
|
public function editMode(): void
|
|
{
|
|
$this->mode = 'update';
|
|
}
|
|
|
|
#[Computed]
|
|
public function endpoints(): array
|
|
{
|
|
$case = ApiType::cases();
|
|
$endpoints = [];
|
|
$apiEndpoints = ApiEndpoint::all()->mapWithKeys(function ($item) {
|
|
return [$item->key->value => $item];
|
|
});
|
|
$clients = $this->clients->pluck('ulid', 'id');
|
|
|
|
foreach ($case as $item) {
|
|
$client_id = null;
|
|
$value = null;
|
|
if(isset($apiEndpoints[$item->value])) {
|
|
$client_id = $apiEndpoints[$item->value]->client_id;
|
|
$client_id = $clients[$client_id];
|
|
$value = $apiEndpoints[$item->value]->value;
|
|
}
|
|
|
|
$endpoints[$item->value] = [
|
|
'label' => $item->label(),
|
|
'value' => $value
|
|
];
|
|
$this->field[$item->value] = [
|
|
'type' => $item->value,
|
|
'value' => $value,
|
|
'client_id' => $client_id
|
|
];
|
|
}
|
|
|
|
return $endpoints;
|
|
}
|
|
|
|
#[Computed]
|
|
public function clients(): Collection
|
|
{
|
|
return Client::all(['id', 'ulid', 'name']);
|
|
}
|
|
|
|
public function save(AssignEndpointUrl $assign): void
|
|
{
|
|
$this->validate();
|
|
|
|
$clients = $this->clients->pluck('id', 'ulid');
|
|
$endpointToSave = array_map(function ($item) use ($clients) {
|
|
return new EndpointUrlDTO(
|
|
clientId: $clients[$item['client_id']],
|
|
endpointUrl: $item['value'],
|
|
field: ApiType::from($item['type'])
|
|
);
|
|
}, $this->field);
|
|
|
|
$assign->execute($endpointToSave);
|
|
|
|
$this->success($this->message());
|
|
}
|
|
};
|
|
|