refactor discord notification view

This commit is contained in:
Andras Bacsai
2024-11-03 23:19:44 +01:00
parent fd546cec9d
commit 1bdc2c6d7c
2 changed files with 84 additions and 34 deletions

View File

@@ -4,47 +4,94 @@ namespace App\Livewire\Notifications;
use App\Models\Team;
use App\Notifications\Test;
use Livewire\Attributes\Rule;
use Livewire\Component;
class Discord extends Component
{
public Team $team;
protected $rules = [
'team.discord_enabled' => 'nullable|boolean',
'team.discord_webhook_url' => 'required|url',
'team.discord_notifications_test' => 'nullable|boolean',
'team.discord_notifications_deployments' => 'nullable|boolean',
'team.discord_notifications_status_changes' => 'nullable|boolean',
'team.discord_notifications_database_backups' => 'nullable|boolean',
'team.discord_notifications_scheduled_tasks' => 'nullable|boolean',
'team.discord_notifications_server_disk_usage' => 'nullable|boolean',
];
#[Rule(['boolean'])]
public bool $discordEnabled = false;
protected $validationAttributes = [
'team.discord_webhook_url' => 'Discord Webhook',
];
#[Rule(['url', 'nullable'])]
public ?string $discordWebhookUrl = null;
#[Rule(['boolean'])]
public bool $discordNotificationsTest = false;
#[Rule(['boolean'])]
public bool $discordNotificationsDeployments = false;
#[Rule(['boolean'])]
public bool $discordNotificationsStatusChanges = false;
#[Rule(['boolean'])]
public bool $discordNotificationsDatabaseBackups = false;
#[Rule(['boolean'])]
public bool $discordNotificationsScheduledTasks = false;
#[Rule(['boolean'])]
public bool $discordNotificationsServerDiskUsage = false;
public function mount()
{
$this->team = auth()->user()->currentTeam();
try {
$this->team = auth()->user()->currentTeam();
$this->syncData();
} catch (\Throwable $e) {
handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->team->discord_enabled = $this->discordEnabled;
$this->team->discord_webhook_url = $this->discordWebhookUrl;
$this->team->discord_notifications_test = $this->discordNotificationsTest;
$this->team->discord_notifications_deployments = $this->discordNotificationsDeployments;
$this->team->discord_notifications_status_changes = $this->discordNotificationsStatusChanges;
$this->team->discord_notifications_database_backups = $this->discordNotificationsDatabaseBackups;
$this->team->discord_notifications_scheduled_tasks = $this->discordNotificationsScheduledTasks;
$this->team->discord_notifications_server_disk_usage = $this->discordNotificationsServerDiskUsage;
try {
$this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
} else {
$this->discordEnabled = $this->team->discord_enabled;
$this->discordWebhookUrl = $this->team->discord_webhook_url;
$this->discordNotificationsTest = $this->team->discord_notifications_test;
$this->discordNotificationsDeployments = $this->team->discord_notifications_deployments;
$this->discordNotificationsStatusChanges = $this->team->discord_notifications_status_changes;
$this->discordNotificationsDatabaseBackups = $this->team->discord_notifications_database_backups;
$this->discordNotificationsScheduledTasks = $this->team->discord_notifications_scheduled_tasks;
$this->discordNotificationsServerDiskUsage = $this->team->discord_notifications_server_disk_usage;
}
}
public function instantSave()
{
try {
$this->submit();
} catch (\Throwable) {
$this->team->discord_enabled = false;
$this->validate();
$this->syncData(true);
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function submit()
{
$this->resetErrorBag();
$this->validate();
$this->saveModel();
try {
$this->resetErrorBag();
$this->syncData(true);
$this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function saveModel()
@@ -56,8 +103,12 @@ class Discord extends Component
public function sendTestNotification()
{
$this->team?->notify(new Test);
$this->dispatch('success', 'Test notification sent.');
try {
$this->team->notify(new Test);
$this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function render()