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\Models\Team;
use App\Notifications\Test; use App\Notifications\Test;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
class Discord extends Component class Discord extends Component
{ {
public Team $team; public Team $team;
protected $rules = [ #[Rule(['boolean'])]
'team.discord_enabled' => 'nullable|boolean', public bool $discordEnabled = false;
'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 = [ #[Rule(['url', 'nullable'])]
'team.discord_webhook_url' => 'Discord Webhook', 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() public function mount()
{ {
try {
$this->team = auth()->user()->currentTeam(); $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() public function instantSave()
{ {
try { try {
$this->submit(); $this->syncData(true);
} catch (\Throwable) { } catch (\Throwable $e) {
$this->team->discord_enabled = false; return handleError($e, $this);
$this->validate();
} }
} }
public function submit() public function submit()
{ {
try {
$this->resetErrorBag(); $this->resetErrorBag();
$this->validate(); $this->syncData(true);
$this->saveModel(); $this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
} }
public function saveModel() public function saveModel()
@@ -56,8 +103,12 @@ class Discord extends Component
public function sendTestNotification() public function sendTestNotification()
{ {
$this->team?->notify(new Test); try {
$this->team->notify(new Test);
$this->dispatch('success', 'Test notification sent.'); $this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
} }
public function render() public function render()

View File

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