2026-01-22 23:38:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
2026-07-25 20:08:19 +02:00
|
|
|
use App\Actions\CreateFeedAction;
|
2026-01-22 23:38:00 +01:00
|
|
|
use App\Models\Feed;
|
2026-07-25 20:08:19 +02:00
|
|
|
use App\Models\Language;
|
2026-03-18 20:01:25 +01:00
|
|
|
use Illuminate\Contracts\View\View;
|
2026-07-25 20:08:19 +02:00
|
|
|
use InvalidArgumentException;
|
2026-01-22 23:38:00 +01:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class Feeds extends Component
|
|
|
|
|
{
|
2026-07-25 20:08:19 +02:00
|
|
|
public bool $showCreateModal = false;
|
|
|
|
|
|
|
|
|
|
public string $newName = '';
|
|
|
|
|
|
|
|
|
|
public string $newProvider = '';
|
|
|
|
|
|
|
|
|
|
public ?int $newLanguageId = null;
|
|
|
|
|
|
|
|
|
|
public string $newDescription = '';
|
|
|
|
|
|
2026-01-22 23:38:00 +01:00
|
|
|
public function toggle(int $feedId): void
|
|
|
|
|
{
|
|
|
|
|
$feed = Feed::findOrFail($feedId);
|
2026-03-08 14:18:28 +01:00
|
|
|
$feed->is_active = ! $feed->is_active;
|
2026-01-22 23:38:00 +01:00
|
|
|
$feed->save();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 20:08:19 +02:00
|
|
|
public function openCreateModal(): void
|
|
|
|
|
{
|
|
|
|
|
$this->reset(['newName', 'newProvider', 'newLanguageId', 'newDescription']);
|
|
|
|
|
$this->resetErrorBag();
|
|
|
|
|
$this->showCreateModal = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function closeCreateModal(): void
|
|
|
|
|
{
|
|
|
|
|
$this->showCreateModal = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createFeed(CreateFeedAction $action): void
|
|
|
|
|
{
|
|
|
|
|
$providers = array_keys($this->activeProviders());
|
|
|
|
|
|
|
|
|
|
$this->validate([
|
|
|
|
|
'newName' => 'required|string|max:255',
|
|
|
|
|
'newProvider' => ['required', 'string', 'in:'.implode(',', $providers)],
|
|
|
|
|
'newLanguageId' => 'required|integer|exists:languages,id',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$action->execute(
|
|
|
|
|
$this->newName,
|
|
|
|
|
$this->newProvider,
|
|
|
|
|
$this->newLanguageId,
|
|
|
|
|
// Blade textarea binds an empty string when blank; the action expects null for "no description".
|
|
|
|
|
$this->newDescription !== '' ? $this->newDescription : null,
|
|
|
|
|
);
|
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
|
$this->addError('newProvider', 'This provider is not available for the selected language.');
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->closeCreateModal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array<string, array<string, mixed>>
|
|
|
|
|
*/
|
|
|
|
|
private function activeProviders(): array
|
|
|
|
|
{
|
|
|
|
|
/** @var array<string, array<string, mixed>> $providers */
|
|
|
|
|
$providers = config('feed.providers', []);
|
|
|
|
|
|
|
|
|
|
return array_filter($providers, fn (array $provider): bool => ($provider['is_active'] ?? false) === true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 20:01:25 +01:00
|
|
|
public function render(): View
|
2026-01-22 23:38:00 +01:00
|
|
|
{
|
|
|
|
|
$feeds = Feed::orderBy('name')->get();
|
|
|
|
|
|
|
|
|
|
return view('livewire.feeds', [
|
|
|
|
|
'feeds' => $feeds,
|
2026-07-25 20:08:19 +02:00
|
|
|
'providers' => $this->activeProviders(),
|
|
|
|
|
'languages' => Language::where('is_active', true)->orderBy('name')->get(),
|
2026-01-22 23:38:00 +01:00
|
|
|
])->layout('layouts.app');
|
|
|
|
|
}
|
|
|
|
|
}
|