-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathresource-policies.component.ts
More file actions
267 lines (246 loc) · 7.79 KB
/
Copy pathresource-policies.component.ts
File metadata and controls
267 lines (246 loc) · 7.79 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { AsyncPipe } from '@angular/common';
import {
ChangeDetectorRef,
Component,
Input,
OnDestroy,
OnInit,
} from '@angular/core';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import { DSONameService } from '@dspace/core/breadcrumbs/dso-name.service';
import { RequestService } from '@dspace/core/data/request.service';
import { EPersonDataService } from '@dspace/core/eperson/eperson-data.service';
import { GroupDataService } from '@dspace/core/eperson/group-data.service';
import { NotificationsService } from '@dspace/core/notification-system/notifications.service';
import { ResourcePolicy } from '@dspace/core/resource-policy/models/resource-policy.model';
import { ResourcePolicyDataService } from '@dspace/core/resource-policy/resource-policy-data.service';
import { followLink } from '@dspace/core/shared/follow-link-config.model';
import { getAllSucceededRemoteData } from '@dspace/core/shared/operators';
import {
hasValue,
isEmpty,
isNotEmpty,
} from '@dspace/shared/utils/empty.util';
import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import {
BehaviorSubject,
from as observableFrom,
Observable,
Subscription,
} from 'rxjs';
import {
concatMap,
distinctUntilChanged,
filter,
map,
reduce,
scan,
take,
} from 'rxjs/operators';
import { BtnDisabledDirective } from '../btn-disabled.directive';
import {
ResourcePolicyCheckboxEntry,
ResourcePolicyEntryComponent,
} from './entry/resource-policy-entry.component';
@Component({
selector: 'ds-resource-policies',
styleUrls: ['./resource-policies.component.scss'],
templateUrl: './resource-policies.component.html',
imports: [
AsyncPipe,
BtnDisabledDirective,
ResourcePolicyEntryComponent,
TranslateModule,
],
})
/**
* Component that shows the policies for given resource
*/
export class ResourcePoliciesComponent implements OnInit, OnDestroy {
/**
* The resource UUID
* @type {string}
*/
@Input() public resourceUUID: string;
/**
* The resource type (e.g. 'item', 'bundle' etc) used as key to build automatically translation label
* @type {string}
*/
@Input() public resourceType: string;
/**
* The resource name
* @type {string}
*/
@Input() public resourceName: string;
/**
* A boolean representing if component is active
* @type {boolean}
*/
private isActive: boolean;
/**
* A boolean representing if a submission delete operation is pending
* @type {BehaviorSubject<boolean>}
*/
private processingDelete$ = new BehaviorSubject<boolean>(false);
/**
* The list of policies for given resource
* @type {BehaviorSubject<ResourcePolicyCheckboxEntry[]>}
*/
private resourcePoliciesEntries$: BehaviorSubject<ResourcePolicyCheckboxEntry[]> =
new BehaviorSubject<ResourcePolicyCheckboxEntry[]>([]);
/**
* Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array}
*/
private subs: Subscription[] = [];
/**
* Initialize instance variables
*
* @param {ChangeDetectorRef} cdr
* @param {DSONameService} dsoNameService
* @param {EPersonDataService} ePersonService
* @param {GroupDataService} groupService
* @param {NotificationsService} notificationsService
* @param {RequestService} requestService
* @param {ResourcePolicyDataService} resourcePolicyService
* @param {ActivatedRoute} route
* @param {Router} router
* @param {TranslateService} translate
*/
constructor(
private cdr: ChangeDetectorRef,
private dsoNameService: DSONameService,
private ePersonService: EPersonDataService,
private groupService: GroupDataService,
private notificationsService: NotificationsService,
private requestService: RequestService,
private resourcePolicyService: ResourcePolicyDataService,
private route: ActivatedRoute,
private router: Router,
private translate: TranslateService,
) {
}
/**
* Initialize the component, setting up the resource's policies
*/
ngOnInit(): void {
this.isActive = true;
this.initResourcePolicyList();
}
/**
* Check if there are any selected resource's policies to be deleted
*
* @return {Observable<boolean>}
*/
canDelete(): Observable<boolean> {
return observableFrom(this.resourcePoliciesEntries$.value).pipe(
filter((entry: ResourcePolicyCheckboxEntry) => entry.checked),
reduce((acc: any, value: any) => [...acc, value], []),
map((entries: ResourcePolicyCheckboxEntry[]) => isNotEmpty(entries)),
distinctUntilChanged(),
);
}
/**
* Delete the selected resource's policies
*/
deleteSelectedResourcePolicies(): void {
this.processingDelete$.next(true);
const policiesToDelete: ResourcePolicyCheckboxEntry[] = this.resourcePoliciesEntries$.value
.filter((entry: ResourcePolicyCheckboxEntry) => entry.checked);
this.subs.push(
observableFrom(policiesToDelete).pipe(
concatMap((entry: ResourcePolicyCheckboxEntry) => this.resourcePolicyService.delete(entry.policy.id)),
scan((acc: any, value: any) => [...acc, value], []),
filter((results: boolean[]) => results.length === policiesToDelete.length),
take(1),
).subscribe((results: boolean[]) => {
const failureResults = results.filter((result: boolean) => !result);
if (isEmpty(failureResults)) {
this.notificationsService.success(null, this.translate.get('resource-policies.delete.success.content'));
} else {
this.notificationsService.error(null, this.translate.get('resource-policies.delete.failure.content'));
}
this.processingDelete$.next(false);
}),
);
}
/**
* Return all resource's policies
*
* @return an observable that emits all resource's policies
*/
getResourcePolicies(): Observable<ResourcePolicyCheckboxEntry[]> {
return this.resourcePoliciesEntries$.asObservable();
}
/**
* Initialize the resource's policies list
*/
initResourcePolicyList() {
this.subs.push(this.resourcePolicyService.searchByResource(
this.resourceUUID, null, false, true,
followLink('eperson', { useCachedVersionIfAvailable: false, reRequestOnStale: true }),
followLink('group', { useCachedVersionIfAvailable: false, reRequestOnStale: true }),
).pipe(
filter(() => this.isActive),
getAllSucceededRemoteData(),
).subscribe((result) => {
const entries = result.payload.page.map((policy: ResourcePolicy) => ({
id: policy.id,
policy: policy,
checked: false,
}));
this.resourcePoliciesEntries$.next(entries);
// TODO detectChanges still needed?
this.cdr.detectChanges();
}));
}
/**
* Return a boolean representing if a delete operation is pending
*
* @return {Observable<boolean>}
*/
isProcessingDelete(): Observable<boolean> {
return this.processingDelete$.asObservable();
}
/**
* Redirect to resource policy creation page
*/
redirectToResourcePolicyCreatePage(): void {
this.router.navigate([`./create`], {
relativeTo: this.route,
queryParams: {
policyTargetId: this.resourceUUID,
targetType: this.resourceType,
},
});
}
/**
* Select/unselect all checkbox in the list
*/
selectAllCheckbox(event: any): void {
const checked = event.target.checked;
this.resourcePoliciesEntries$.value.forEach((entry: ResourcePolicyCheckboxEntry) => entry.checked = checked);
}
/**
* Select/unselect checkbox
*/
selectCheckbox(policyEntry: ResourcePolicyCheckboxEntry, checked: boolean) {
policyEntry.checked = checked;
}
/**
* Unsubscribe from all subscriptions
*/
ngOnDestroy(): void {
this.isActive = false;
this.resourcePoliciesEntries$ = null;
this.subs
.filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe());
}
}