-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathinstitutions-users.component.ts
More file actions
232 lines (189 loc) · 7.16 KB
/
Copy pathinstitutions-users.component.ts
File metadata and controls
232 lines (189 loc) · 7.16 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { createDispatchMap, select } from '@ngxs/store';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { CheckboxModule } from 'primeng/checkbox';
import { DialogService } from 'primeng/dynamicdialog';
import { PaginatorState } from 'primeng/paginator';
import { filter } from 'rxjs';
import {
ChangeDetectionStrategy,
Component,
computed,
DestroyRef,
effect,
inject,
OnInit,
signal,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { UserSelectors } from '@osf/core/store/user';
import { SelectComponent } from '@osf/shared/components';
import { TABLE_PARAMS } from '@osf/shared/constants';
import { SortOrder } from '@osf/shared/enums';
import { Primitive } from '@osf/shared/helpers';
import { QueryParams } from '@osf/shared/models';
import { ToastService } from '@osf/shared/services';
import { InstitutionsSearchSelectors } from '@osf/shared/stores/institutions-search';
import { AdminTableComponent } from '../../components';
import { departmentOptions, userTableColumns } from '../../constants';
import { SendEmailDialogComponent } from '../../dialogs';
import { DownloadType } from '../../enums';
import { camelToSnakeCase } from '../../helpers';
import { mapUserToTableCellData } from '../../mappers';
import { InstitutionUser, SendEmailDialogData, TableCellData, TableCellLink, TableIconClickEvent } from '../../models';
import { FetchInstitutionUsers, InstitutionsAdminSelectors, SendUserMessage } from '../../store';
@Component({
selector: 'osf-institutions-users',
imports: [AdminTableComponent, FormsModule, SelectComponent, CheckboxModule, TranslatePipe],
templateUrl: './institutions-users.component.html',
styleUrl: './institutions-users.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [DialogService],
})
export class InstitutionsUsersComponent implements OnInit {
private readonly route = inject(ActivatedRoute);
private readonly translate = inject(TranslateService);
private readonly dialogService = inject(DialogService);
private readonly destroyRef = inject(DestroyRef);
private readonly toastService = inject(ToastService);
private readonly actions = createDispatchMap({
fetchInstitutionUsers: FetchInstitutionUsers,
sendUserMessage: SendUserMessage,
});
institutionId = '';
currentPage = signal(1);
currentPageSize = signal(TABLE_PARAMS.rows);
first = signal(0);
selectedDepartment = signal<string | null>(null);
hasOrcidFilter = signal<boolean>(false);
sortField = signal<string>('user_name');
sortOrder = signal<number>(SortOrder.Desc);
departmentOptions = departmentOptions;
tableColumns = userTableColumns;
users = select(InstitutionsAdminSelectors.getUsers);
institution = select(InstitutionsSearchSelectors.getInstitution);
totalCount = select(InstitutionsAdminSelectors.getUsersTotalCount);
isLoading = select(InstitutionsAdminSelectors.getUsersLoading);
currentUser = select(UserSelectors.getCurrentUser);
tableData = computed(() => {
return this.users().map((user: InstitutionUser): TableCellData => mapUserToTableCellData(user));
});
amountText = computed(() => {
const count = this.totalCount();
return count + ' ' + this.translate.instant('adminInstitutions.summary.totalUsers').toLowerCase();
});
constructor() {
this.setupDataFetchingEffect();
}
ngOnInit(): void {
const institutionId = this.route.parent?.snapshot.params['institution-id'];
if (institutionId) {
this.institutionId = institutionId;
}
}
onPageChange(event: PaginatorState): void {
this.currentPage.set(event.page ? event.page + 1 : 1);
this.first.set(event.first ?? 0);
this.currentPageSize.set(event.rows || this.currentPageSize());
}
onDepartmentChange(department: Primitive): void {
const departmentValue = department === null || department === undefined ? null : String(department);
this.selectedDepartment.set(departmentValue);
this.currentPage.set(1);
}
onOrcidFilterChange(hasOrcid: boolean): void {
this.hasOrcidFilter.set(hasOrcid);
this.currentPage.set(1);
}
onSortChange(sortEvent: QueryParams): void {
this.currentPage.set(1);
this.sortField.set(camelToSnakeCase(sortEvent.sortColumn) || 'user_name');
this.sortOrder.set(sortEvent.sortOrder);
}
onIconClick(event: TableIconClickEvent): void {
switch (event.action) {
case 'sendMessage': {
this.dialogService
.open(SendEmailDialogComponent, {
width: '448px',
focusOnShow: false,
header: this.translate.instant('adminInstitutions.institutionUsers.sendEmail'),
closeOnEscape: true,
modal: true,
closable: true,
data: this.currentUser()?.fullName,
})
.onClose.pipe(
filter((value) => !!value),
takeUntilDestroyed(this.destroyRef)
)
.subscribe((data: SendEmailDialogData) => this.sendEmailToUser(event.rowData, data));
break;
}
}
}
download(type: DownloadType) {
const baseUrl = this.institution().userMetricsUrl;
if (!baseUrl) {
return;
}
const url = this.createUrl(baseUrl, type);
window.open(url, '_blank');
}
private createUrl(baseUrl: string, mediaType: string): string {
const query = {} as Record<string, string>;
if (this.selectedDepartment()) {
query['filter[department]'] = this.selectedDepartment() || '';
}
if (this.hasOrcidFilter()) {
query['filter[orcid_id][ne]'] = '';
}
const userURL = new URL(baseUrl);
userURL.searchParams.set('format', mediaType);
userURL.searchParams.set('page[size]', '10000');
Object.entries(query).forEach(([key, value]) => {
userURL.searchParams.set(key, value);
});
return userURL.toString();
}
private setupDataFetchingEffect(): void {
effect(() => {
if (!this.institutionId) return;
const filters = this.buildFilters();
const sortField = this.sortField();
const sortOrder = this.sortOrder();
const sortParam = sortOrder === 0 ? `-${sortField}` : sortField;
this.actions.fetchInstitutionUsers(
this.institutionId,
this.currentPage(),
this.currentPageSize(),
sortParam,
filters
);
});
}
private buildFilters(): Record<string, string> {
const filters: Record<string, string> = {};
const department = this.selectedDepartment();
if (department !== null) {
filters['filter[department]'] = department;
}
if (this.hasOrcidFilter()) {
filters['filter[orcid_id][ne]'] = '';
}
return filters;
}
private sendEmailToUser(userRowData: TableCellData, emailData: SendEmailDialogData): void {
const userId = (userRowData['userLink'] as TableCellLink).text as string;
this.actions
.sendUserMessage(
userId,
this.institutionId,
emailData.emailContent,
emailData.ccSender,
emailData.allowReplyToSender
)
.subscribe(() => this.toastService.showSuccess('adminInstitutions.institutionUsers.messageSent'));
}
}