import { Component, inject } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatIconModule } from '@angular/material/icon'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatSelectModule } from '@angular/material/select'; import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'; import { finalize } from 'rxjs'; import { getAuthErrorMessage } from '../auth/error-message'; import { AuthService } from '../auth/auth.service'; import { TaskDigestPreference } from '../auth/auth.models'; import { OnboardingService } from '../onboarding/onboarding.service'; import { TaskPushService } from '../tasks/task-push.service'; @Component({ selector: 'app-account', imports: [ MatButtonModule, MatCardModule, MatIconModule, MatProgressSpinnerModule, MatSelectModule, MatSnackBarModule, ], templateUrl: './account.component.html', styleUrl: './account.component.scss', }) export class AccountComponent { protected readonly auth = inject(AuthService); protected readonly onboarding = inject(OnboardingService); protected readonly taskPush = inject(TaskPushService); private readonly snackBar = inject(MatSnackBar); protected savingTaskDigestPreference = false; protected readonly taskDigestPreferenceOptions: ReadonlyArray<{ value: TaskDigestPreference; label: string; description: string; }> = [ { value: 'both', label: 'Morgens und nachmittags', description: 'E-Mail um 9:00 und 15:00 Uhr, wenn Tasks offen sind.', }, { value: 'morning', label: 'Nur morgens', description: 'E-Mail um 9:00 Uhr, wenn Tasks offen sind.', }, { value: 'none', label: 'Keine Task-Mail', description: 'Es werden keine taeglichen Task-Mails gesendet.', }, ]; resetOnboarding(): void { this.onboarding.resetForCurrentUser(); } updateTaskDigestPreference(preference: TaskDigestPreference): void { if (this.savingTaskDigestPreference || preference === this.auth.user()?.taskDigestPreference) { return; } this.savingTaskDigestPreference = true; this.auth .updateTaskDigestPreference(preference) .pipe(finalize(() => (this.savingTaskDigestPreference = false))) .subscribe({ next: () => { this.snackBar.open('Task-Mail-Einstellung gespeichert.', 'OK', { duration: 3000, }); if (preference === 'none') { void this.taskPush.disableCurrentSubscription(); } }, error: (error: unknown) => { this.snackBar.open(getAuthErrorMessage(error), 'OK', { duration: 5000, }); }, }); } async enablePushNotifications(): Promise { await this.taskPush.enable(); if (this.taskPush.errorMessage()) { this.snackBar.open(this.taskPush.errorMessage()!, 'OK', { duration: 5000, }); return; } this.snackBar.open('PWA-Benachrichtigungen aktiviert.', 'OK', { duration: 3000, }); } async disablePushNotifications(): Promise { await this.taskPush.disableCurrentSubscription(); if (this.taskPush.errorMessage()) { this.snackBar.open(this.taskPush.errorMessage()!, 'OK', { duration: 5000, }); return; } this.snackBar.open('PWA-Benachrichtigungen deaktiviert.', 'OK', { duration: 3000, }); } logout(): void { this.auth.logoutThroughProvider(); } }