Files
Scheduler/app/Livewire/BotsList.php
Oskar-Mikael 378355ad5b
All checks were successful
Deploy App / deploy (push) Successful in 11s
Add bot logs
2025-08-31 21:54:18 +02:00

91 lines
2.2 KiB
PHP

<?php
namespace App\Livewire;
use App\Jobs\RunBot;
use App\Models\Bot;
use Illuminate\Support\Facades\Auth;
use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
use Livewire\Component;
class BotsList extends Component
{
public $bots;
public string $query = '';
public function mount()
{
$this->bots = Auth::user()->bots;
}
public function search()
{
$this->bots = Auth::user()->bots()
->where('name', 'like', '%' . $this->query . '%')
->orWhere('class', 'like', '%' . $this->query . '%')
->get();
}
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
{
$log = $bot->logs()->create([
'status' => 'pending',
'started_at' => now(),
]);
// Dispatch the job to run the bot
dispatch(new RunBot($bot, $log));
flash()->success("Bot '{$bot->name}' is being executed.");
}
public function render()
{
return view('livewire.bots-list');
}
}