74179: Deleting groups notifications, authorisation check, button on edit group page

This commit is contained in:
Marie Verdonck
2020-10-19 12:36:18 +02:00
parent aa8feb01b3
commit 1903530f77
9 changed files with 215 additions and 41 deletions

View File

@@ -1,16 +1,22 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { Subscription } from 'rxjs/internal/Subscription';
import { map, switchMap, take } from 'rxjs/operators';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { PaginatedList } from '../../../core/data/paginated-list';
import { RemoteData } from '../../../core/data/remote-data';
import { EPersonDataService } from '../../../core/eperson/eperson-data.service';
import { GroupDataService } from '../../../core/eperson/group-data.service';
import { EPerson } from '../../../core/eperson/models/eperson.model';
import { GroupDtoModel } from '../../../core/eperson/models/group-dto.model';
import { Group } from '../../../core/eperson/models/group.model';
import { RouteService } from '../../../core/services/route.service';
import { getAllSucceededRemoteDataPayload } from '../../../core/shared/operators';
import { PageInfo } from '../../../core/shared/page-info.model';
import { hasValue } from '../../../shared/empty.util';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
@@ -23,7 +29,7 @@ import { PaginationComponentOptions } from '../../../shared/pagination/paginatio
* A component used for managing all existing groups within the repository.
* The admin can create, edit or delete groups here.
*/
export class GroupsRegistryComponent implements OnInit {
export class GroupsRegistryComponent implements OnInit, OnDestroy {
messagePrefix = 'admin.access-control.groups.';
@@ -37,9 +43,19 @@ export class GroupsRegistryComponent implements OnInit {
});
/**
* A list of all the current groups within the repository or the result of the search
* A list of all the current Groups within the repository or the result of the search
*/
groups: Observable<RemoteData<PaginatedList<Group>>>;
groups$: BehaviorSubject<RemoteData<PaginatedList<Group>>> = new BehaviorSubject<RemoteData<PaginatedList<Group>>>({} as any);
/**
* A BehaviorSubject with the list of GroupDtoModel objects made from the Groups in the repository or
* as the result of the search
*/
groupsDto$: BehaviorSubject<PaginatedList<GroupDtoModel>> = new BehaviorSubject<PaginatedList<GroupDtoModel>>({} as any);
/**
* An observable for the pageInfo, needed to pass to the pagination component
*/
pageInfoState$: BehaviorSubject<PageInfo> = new BehaviorSubject<PageInfo>(undefined);
// The search form
searchForm;
@@ -47,13 +63,19 @@ export class GroupsRegistryComponent implements OnInit {
// Current search in groups registry
currentSearchQuery: string;
/**
* List of subscriptions
*/
subs: Subscription[] = [];
constructor(public groupService: GroupDataService,
private ePersonDataService: EPersonDataService,
private translateService: TranslateService,
private notificationsService: NotificationsService,
private formBuilder: FormBuilder,
protected routeService: RouteService,
private router: Router) {
private router: Router,
private authorizationService: AuthorizationDataService) {
this.currentSearchQuery = '';
this.searchForm = this.formBuilder.group(({
query: this.currentSearchQuery,
@@ -84,28 +106,51 @@ export class GroupsRegistryComponent implements OnInit {
this.currentSearchQuery = query;
this.config.currentPage = 1;
}
this.groups = this.groupService.searchGroups(this.currentSearchQuery.trim(), {
this.subs.push(this.groupService.searchGroups(this.currentSearchQuery.trim(), {
currentPage: this.config.currentPage,
elementsPerPage: this.config.pageSize
});
}).subscribe((groupsRD: RemoteData<PaginatedList<Group>>) => {
this.groups$.next(groupsRD)
}
));
this.subs.push(this.groups$.pipe(
getAllSucceededRemoteDataPayload(),
switchMap((groups) => {
return combineLatest(...groups.page.map((group) => {
return this.authorizationService.isAuthorized(FeatureID.CanDelete, hasValue(group) ? group.self : undefined).pipe(
map((authorized: boolean) => {
const groupDtoModel: GroupDtoModel = new GroupDtoModel();
groupDtoModel.ableToDelete = authorized;
groupDtoModel.group = group;
return groupDtoModel;
})
);
})).pipe(map((dtos: GroupDtoModel[]) => {
return new PaginatedList(groups.pageInfo, dtos);
}))
})).subscribe((value: PaginatedList<GroupDtoModel>) => {
this.groupsDto$.next(value);
this.pageInfoState$.next(value.pageInfo);
}));
}
/**
* Delete Group
*/
deleteGroup(group: Group) {
// TODO (backend)
console.log('TODO implement editGroup', group);
this.notificationsService.error('TODO implement deleteGroup (not yet implemented in backend)');
if (hasValue(group.id)) {
this.groupService.deleteGroup(group).pipe(take(1)).subscribe((success: boolean) => {
if (success) {
this.notificationsService.success(this.translateService.get(this.messagePrefix + 'notification.deleted.success', { name: group.name }));
this.forceUpdateGroup();
} else {
this.notificationsService.error(this.translateService.get(this.messagePrefix + 'notification.deleted.failure', { name: group.name }));
}
})
this.groupService.deleteGroup(group).pipe(take(1))
.subscribe(([success, optionalErrorMessage]: [boolean, string]) => {
if (success) {
this.notificationsService.success(this.translateService.get(this.messagePrefix + 'notification.deleted.success', { name: group.name }));
this.forceUpdateGroup();
} else {
this.notificationsService.error(
this.translateService.get(this.messagePrefix + 'notification.deleted.failure.title', { name: group.name }),
this.translateService.get(this.messagePrefix + 'notification.deleted.failure.content', { cause: optionalErrorMessage }));
}
})
}
}
@@ -151,4 +196,15 @@ export class GroupsRegistryComponent implements OnInit {
getOptionalComColFromName(groupName: string): string {
return this.groupService.getUUIDFromString(groupName);
}
/**
* Unsub all subscriptions
*/
ngOnDestroy(): void {
this.cleanupSubscribes();
}
cleanupSubscribes() {
this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe());
}
}