76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Bot;
|
|
use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
|
|
use Livewire\Component;
|
|
|
|
class BotsList extends Component
|
|
{
|
|
public $bots;
|
|
|
|
public function mount()
|
|
{
|
|
$this->bots = \App\Models\Bot::all();
|
|
}
|
|
|
|
public function toggleBot($botId)
|
|
{
|
|
$bot = \App\Models\Bot::find($botId);
|
|
|
|
if ($bot) {
|
|
$bot->enabled = !$bot->enabled;
|
|
$bot->save();
|
|
$this->bots = \App\Models\Bot::all(); // Refresh the list
|
|
flash()->success("Bot '{$bot->name}' has been " . ($bot->enabled ? 'enabled' : 'disabled') . ".");
|
|
}
|
|
}
|
|
|
|
public function deleteBot(int $botId): void
|
|
{
|
|
$bot = \App\Models\Bot::find($botId);
|
|
|
|
if ($bot) {
|
|
LivewireAlert::title('Are you sure you want to delete ' . $bot->name . '?')
|
|
->asConfirm()
|
|
->onConfirm('confirmDelete', [$bot])
|
|
->show();
|
|
}
|
|
}
|
|
|
|
public function confirmDelete(Bot $bot): void
|
|
{
|
|
$bot->delete();
|
|
$this->bots = \App\Models\Bot::all(); // Refresh the list
|
|
flash()->success("Bot '{$bot->name}' has been deleted.");
|
|
}
|
|
|
|
public function runBot($botId)
|
|
{
|
|
$bot = \App\Models\Bot::find($botId);
|
|
|
|
if ($bot) {
|
|
LivewireAlert::title('Are you sure you want to run ' . $bot->name . '?')
|
|
->asConfirm()
|
|
->onConfirm('confirmRunBot', [$bot])
|
|
->show();
|
|
}
|
|
}
|
|
|
|
public function confirmRunBot(Bot $bot): void
|
|
{
|
|
// Dispatch the job to run the bot
|
|
$class = new $bot->class($bot->config ?? []);
|
|
|
|
$class->run();
|
|
|
|
flash()->success("Bot '{$bot->name}' is being executed.");
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.bots-list');
|
|
}
|
|
}
|