forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcomcol-role.component.ts
More file actions
184 lines (160 loc) · 5.53 KB
/
Copy pathcomcol-role.component.ts
File metadata and controls
184 lines (160 loc) · 5.53 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
import { Component, Input, OnInit } from '@angular/core';
import { Group } from '../../../../../core/eperson/models/group.model';
import { Community } from '../../../../../core/shared/community.model';
import { BehaviorSubject, Observable } from 'rxjs';
import { GroupDataService } from '../../../../../core/eperson/group-data.service';
import { Collection } from '../../../../../core/shared/collection.model';
import { filter, map, switchMap, take } from 'rxjs/operators';
import { getAllCompletedRemoteData, getFirstCompletedRemoteData } from '../../../../../core/shared/operators';
import { RequestService } from '../../../../../core/data/request.service';
import { RemoteData } from '../../../../../core/data/remote-data';
import { HALLink } from '../../../../../core/shared/hal-link.model';
import { getGroupEditRoute } from '../../../../../access-control/access-control-routing-paths';
import { hasNoValue, hasValue } from '../../../../empty.util';
import { NoContent } from '../../../../../core/shared/NoContent.model';
import { NotificationsService } from '../../../../notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
import { DSONameService } from '../../../../../core/breadcrumbs/dso-name.service';
/**
* Component for managing a community or collection role.
*/
@Component({
selector: 'ds-comcol-role',
styleUrls: ['./comcol-role.component.scss'],
templateUrl: './comcol-role.component.html'
})
export class ComcolRoleComponent implements OnInit {
/**
* The community or collection to manage.
*/
@Input()
dso: Community | Collection;
/**
* The role to manage
*/
comcolRole$: BehaviorSubject<HALLink> = new BehaviorSubject(undefined);
/**
* The group for this role, as an observable remote data.
*/
groupRD$: Observable<RemoteData<Group>>;
/**
* The group for this role, as an observable.
*/
group$: Observable<Group>;
/**
* The link to the group edit page as an observable.
*/
editGroupLink$: Observable<string>;
/**
* True if there is no group for this ComcolRole.
*/
hasNoGroup$: Observable<boolean>;
/**
* Return true if the group for this ComcolRole is the Anonymous group, as an observable.
*/
hasAnonymousGroup$: Observable<boolean>;
/**
* Return true if there is a group for this ComcolRole other than the Anonymous group, as an observable.
*/
hasCustomGroup$: Observable<boolean>;
/**
* The human-readable name of this role
*/
roleName$: Observable<string>;
constructor(
protected requestService: RequestService,
protected groupService: GroupDataService,
protected notificationsService: NotificationsService,
protected translateService: TranslateService,
public dsoNameService: DSONameService,
) {
}
/**
* The link to the related group.
*/
get groupLink(): string {
return this.comcolRole.href;
}
/**
* The role to manage
*/
@Input()
set comcolRole(newRole: HALLink) {
this.comcolRole$.next(newRole);
}
get comcolRole(): HALLink {
return this.comcolRole$.getValue();
}
/**
* Create a group for this community or collection role.
*/
create() {
this.groupService.createComcolGroup(this.dso, this.comcolRole.name, this.groupLink).pipe(
getFirstCompletedRemoteData()
).subscribe((rd: RemoteData<Group>) => {
if (rd.hasSucceeded) {
this.groupService.clearGroupsRequests();
this.requestService.setStaleByHrefSubstring(this.comcolRole.href);
this.groupService.findByHref(this.comcolRole.href, false, true)
.pipe(getFirstCompletedRemoteData(), take(1))
.subscribe();
} else {
this.notificationsService.error(
this.roleName$.pipe(
switchMap(role => this.translateService.get('comcol-role.edit.create.error.title', { role }))
),
`${rd.statusCode} ${rd.errorMessage}`
);
}
});
}
/**
* Delete the group for this community or collection role.
*/
delete() {
this.groupService.deleteComcolGroup(this.groupLink).pipe(
getFirstCompletedRemoteData()
).subscribe((rd: RemoteData<NoContent>) => {
if (rd.hasSucceeded) {
this.groupService.clearGroupsRequests();
this.requestService.setStaleByHrefSubstring(this.comcolRole.href);
} else {
this.notificationsService.error(
this.roleName$.pipe(
switchMap(role => this.translateService.get('comcol-role.edit.delete.error.title', { role }))
),
rd.errorMessage
);
}
});
}
ngOnInit(): void {
this.groupRD$ = this.comcolRole$.pipe(
filter((role: HALLink) => hasValue(role)),
switchMap((role: HALLink) => this.groupService.findByHref(role.href)),
getAllCompletedRemoteData(),
);
this.group$ = this.groupRD$.pipe(
map((rd: RemoteData<Group>) => {
if (hasValue(rd.payload)) {
return rd.payload;
} else {
return undefined;
}
})
);
this.editGroupLink$ = this.group$.pipe(
map((group: Group) => hasValue(group) ? getGroupEditRoute(group.id) : undefined),
);
this.hasNoGroup$ = this.group$.pipe(
map((group: Group) => hasNoValue(group)),
);
this.hasAnonymousGroup$ = this.group$.pipe(
map((group: Group) => hasValue(group) && group.name === 'Anonymous'),
);
this.hasCustomGroup$ = this.group$.pipe(
map((group: Group) => hasValue(group) && group.name !== 'Anonymous'),
);
this.roleName$ = this.translateService.get(`comcol-role.edit.${this.comcolRole.name}.name`);
}
}