forked from CenterForOpenScience/angular-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification-settings.component.ts
More file actions
102 lines (84 loc) · 3.71 KB
/
notification-settings.component.ts
File metadata and controls
102 lines (84 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { createDispatchMap, select } from '@ngxs/store';
import { TranslatePipe } from '@ngx-translate/core';
import { Select } from 'primeng/select';
import { Skeleton } from 'primeng/skeleton';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ChangeDetectionStrategy, Component, DestroyRef, effect, inject, OnInit, Signal } from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { FormBuilder, FormControl, FormRecord, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { SUBSCRIPTION_FREQUENCY_OPTIONS } from '@osf/shared/constants/subscription-options.const';
import { CurrentResourceType } from '@osf/shared/enums/resource-type.enum';
import { SubscriptionFrequency } from '@osf/shared/enums/subscriptions/subscription-frequency.enum';
import { NotificationSubscription } from '@osf/shared/models/notifications/notification-subscription.model';
import { ToastService } from '@osf/shared/services/toast.service';
import {
GetProviderSubscriptions,
ProviderSubscriptionsSelectors,
UpdateProviderSubscription,
} from '../../store/provider-subscriptions';
@Component({
selector: 'osf-notification-settings',
imports: [TranslatePipe, RouterLink, ReactiveFormsModule, Select, Skeleton],
templateUrl: './notification-settings.component.html',
styleUrl: './notification-settings.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificationSettingsComponent implements OnInit {
private readonly route = inject(ActivatedRoute);
private readonly fb = inject(FormBuilder);
private readonly toastService = inject(ToastService);
private readonly destroyRef = inject(DestroyRef);
readonly providerId = toSignal(
this.route.parent?.params.pipe(map((params) => params['providerId'])) ?? of(undefined)
);
readonly resourceType: Signal<CurrentResourceType | undefined> = toSignal(
this.route.data.pipe(map((params) => params['resourceType']))
);
subscriptions = select(ProviderSubscriptionsSelectors.getSubscriptions);
isLoading = select(ProviderSubscriptionsSelectors.isLoading);
readonly form = new FormRecord<FormControl<SubscriptionFrequency>>({});
readonly frequencyOptions = SUBSCRIPTION_FREQUENCY_OPTIONS;
private readonly actions = createDispatchMap({
getProviderSubscriptions: GetProviderSubscriptions,
updateProviderSubscription: UpdateProviderSubscription,
});
constructor() {
effect(() => {
const subs = this.subscriptions();
subs.forEach((sub) => {
const control = this.form.controls[sub.id];
if (!control) {
this.form.addControl(sub.id, this.fb.control(sub.frequency, { nonNullable: true }), { emitEvent: false });
return;
}
if (control.value !== sub.frequency) {
control.setValue(sub.frequency, { emitEvent: false });
}
});
});
}
ngOnInit(): void {
const providerType = this.resourceType();
const providerId = this.providerId();
if (providerType && providerId) {
this.actions.getProviderSubscriptions(providerType, providerId);
}
}
onFrequencyChange(sub: NotificationSubscription, frequency: SubscriptionFrequency): void {
if (sub.frequency === frequency) return;
const providerType = this.resourceType();
const providerId = this.providerId();
if (!providerType || !providerId) return;
this.actions
.updateProviderSubscription({
providerType,
providerId,
subscriptionId: sub.id,
frequency,
})
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => this.toastService.showSuccess('moderation.notificationPreferences.successUpdate'));
}
}