From 1bdc2c6d7cf1ebc700f75689fc1e680cad5451a7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sun, 3 Nov 2024 23:19:44 +0100 Subject: [PATCH] refactor discord notification view --- app/Livewire/Notifications/Discord.php | 99 ++++++++++++++----- .../livewire/notifications/discord.blade.php | 21 ++-- 2 files changed, 85 insertions(+), 35 deletions(-) diff --git a/app/Livewire/Notifications/Discord.php b/app/Livewire/Notifications/Discord.php index db8594558e..df5489a24a 100644 --- a/app/Livewire/Notifications/Discord.php +++ b/app/Livewire/Notifications/Discord.php @@ -4,47 +4,94 @@ 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', - ]; - - protected $validationAttributes = [ - 'team.discord_webhook_url' => 'Discord Webhook', - ]; + #[Rule(['boolean'])] + public bool $discordEnabled = false; + + #[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 @@ public function saveModel() 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() diff --git a/resources/views/livewire/notifications/discord.blade.php b/resources/views/livewire/notifications/discord.blade.php index 300af6d73a..bb94c86f0d 100644 --- a/resources/views/livewire/notifications/discord.blade.php +++ b/resources/views/livewire/notifications/discord.blade.php @@ -9,7 +9,7 @@ Save - @if ($team->discord_enabled) + @if ($discordEnabled) Send Test Notifications @@ -17,27 +17,26 @@ @endif
- +
+ id="discordWebhookUrl" label="Webhook" /> - @if (data_get($team, 'discord_enabled')) + @if ($discordEnabled)

Subscribe to events

@if (isDev()) - + @endif - - - - + -
@endif