Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor discord notification view

Andras Bacsai1bdc2c6d7cf1
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 @@ 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',
- ];
-
- 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 @@ 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()
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 @@
<x-forms.button type="submit">
Save
</x-forms.button>
- @if ($team->discord_enabled)
+ @if ($discordEnabled)
<x-forms.button class="normal-case dark:text-white btn btn-xs no-animation btn-primary"
wire:click="sendTestNotification">
Send Test Notifications
@@ -17,27 +17,26 @@
@endif
</div>
<div class="w-32">
- <x-forms.checkbox instantSave id="team.discord_enabled" label="Enabled" />
+ <x-forms.checkbox instantSave id="discordEnabled" label="Enabled" />
</div>
<x-forms.input type="password"
helper="Generate a webhook in Discord.<br>Example: https://discord.com/api/webhooks/...." required
- id="team.discord_webhook_url" label="Webhook" />
+ id="discordWebhookUrl" label="Webhook" />
</form>
- @if (data_get($team, 'discord_enabled'))
+ @if ($discordEnabled)
<h2 class="mt-4">Subscribe to events</h2>
<div class="w-64">
@if (isDev())
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_test" label="Test" />
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsTest" label="Test" />
@endif
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_status_changes"
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsStatusChanges"
label="Container Status Changes" />
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_deployments"
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsDeployments"
label="Application Deployments" />
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_database_backups"
- label="Backup Status" />
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_scheduled_tasks"
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsDatabaseBackups" label="Backup Status" />
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsScheduledTasks"
label="Scheduled Tasks Status" />
- <x-forms.checkbox instantSave="saveModel" id="team.discord_notifications_server_disk_usage"
+ <x-forms.checkbox instantSave="saveModel" id="discordNotificationsServerDiskUsage"
label="Server Disk Usage" />
</div>
@endif