Skip to content

Commit

Permalink
refactor discord notification view
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Nov 3, 2024
1 parent fd546ce commit 1bdc2c6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
99 changes: 75 additions & 24 deletions app/Livewire/Notifications/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
21 changes: 10 additions & 11 deletions resources/views/livewire/notifications/discord.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@
<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
</x-forms.button>
@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
Expand Down

0 comments on commit 1bdc2c6

Please sign in to comment.