Skip to content

Commit 00d895b

Browse files
authored
Merge pull request DSpace#2293 from 4Science/DURACOM-151
Delete EPerson from "Edit EPerson" form
2 parents ed6fe8c + 454d869 commit 00d895b

3 files changed

Lines changed: 48 additions & 35 deletions

File tree

src/app/access-control/epeople-registry/epeople-registry.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,17 @@ export class EPeopleRegistryComponent implements OnInit, OnDestroy {
287287
/**
288288
* This method will set everything to stale, which will cause the lists on this page to update.
289289
*/
290-
reset() {
290+
reset(): void {
291291
this.epersonService.getBrowseEndpoint().pipe(
292-
take(1)
293-
).subscribe((href: string) => {
294-
this.requestService.setStaleByHrefSubstring(href).pipe(take(1)).subscribe(() => {
295-
this.epersonService.cancelEditEPerson();
296-
this.isEPersonFormShown = false;
297-
});
292+
take(1),
293+
switchMap((href: string) => {
294+
return this.requestService.setStaleByHrefSubstring(href).pipe(
295+
take(1),
296+
);
297+
})
298+
).subscribe(()=>{
299+
this.epersonService.cancelEditEPerson();
300+
this.isEPersonFormShown = false;
298301
});
299302
}
300303
}

src/app/access-control/epeople-registry/eperson-form/eperson-form.component.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@ng-dynamic-forms/core';
99
import { TranslateService } from '@ngx-translate/core';
1010
import { combineLatest as observableCombineLatest, Observable, of as observableOf, Subscription } from 'rxjs';
11-
import { debounceTime, switchMap, take } from 'rxjs/operators';
11+
import { debounceTime, finalize, map, switchMap, take } from 'rxjs/operators';
1212
import { PaginatedList } from '../../../core/data/paginated-list.model';
1313
import { RemoteData } from '../../../core/data/remote-data';
1414
import { EPersonDataService } from '../../../core/eperson/eperson-data.service';
@@ -463,31 +463,42 @@ export class EPersonFormComponent implements OnInit, OnDestroy {
463463
* Deletes the EPerson from the Repository. The EPerson will be the only that this form is showing.
464464
* It'll either show a success or error message depending on whether the delete was successful or not.
465465
*/
466-
delete() {
467-
this.epersonService.getActiveEPerson().pipe(take(1)).subscribe((eperson: EPerson) => {
468-
const modalRef = this.modalService.open(ConfirmationModalComponent);
469-
modalRef.componentInstance.dso = eperson;
470-
modalRef.componentInstance.headerLabel = 'confirmation-modal.delete-eperson.header';
471-
modalRef.componentInstance.infoLabel = 'confirmation-modal.delete-eperson.info';
472-
modalRef.componentInstance.cancelLabel = 'confirmation-modal.delete-eperson.cancel';
473-
modalRef.componentInstance.confirmLabel = 'confirmation-modal.delete-eperson.confirm';
474-
modalRef.componentInstance.brandColor = 'danger';
475-
modalRef.componentInstance.confirmIcon = 'fas fa-trash';
476-
modalRef.componentInstance.response.pipe(take(1)).subscribe((confirm: boolean) => {
477-
if (confirm) {
478-
if (hasValue(eperson.id)) {
479-
this.epersonService.deleteEPerson(eperson).pipe(getFirstCompletedRemoteData()).subscribe((restResponse: RemoteData<NoContent>) => {
480-
if (restResponse.hasSucceeded) {
481-
this.notificationsService.success(this.translateService.get(this.labelPrefix + 'notification.deleted.success', { name: this.dsoNameService.getName(eperson) }));
482-
this.submitForm.emit();
483-
} else {
484-
this.notificationsService.error('Error occured when trying to delete EPerson with id: ' + eperson.id + ' with code: ' + restResponse.statusCode + ' and message: ' + restResponse.errorMessage);
485-
}
486-
this.cancelForm.emit();
487-
});
488-
}
489-
}
490-
});
466+
delete(): void {
467+
this.epersonService.getActiveEPerson().pipe(
468+
take(1),
469+
switchMap((eperson: EPerson) => {
470+
const modalRef = this.modalService.open(ConfirmationModalComponent);
471+
modalRef.componentInstance.dso = eperson;
472+
modalRef.componentInstance.headerLabel = 'confirmation-modal.delete-eperson.header';
473+
modalRef.componentInstance.infoLabel = 'confirmation-modal.delete-eperson.info';
474+
modalRef.componentInstance.cancelLabel = 'confirmation-modal.delete-eperson.cancel';
475+
modalRef.componentInstance.confirmLabel = 'confirmation-modal.delete-eperson.confirm';
476+
modalRef.componentInstance.brandColor = 'danger';
477+
modalRef.componentInstance.confirmIcon = 'fas fa-trash';
478+
479+
return modalRef.componentInstance.response.pipe(
480+
take(1),
481+
switchMap((confirm: boolean) => {
482+
if (confirm && hasValue(eperson.id)) {
483+
this.canDelete$ = observableOf(false);
484+
return this.epersonService.deleteEPerson(eperson).pipe(
485+
getFirstCompletedRemoteData(),
486+
map((restResponse: RemoteData<NoContent>) => ({ restResponse, eperson }))
487+
);
488+
} else {
489+
return observableOf(null);
490+
}
491+
}),
492+
finalize(() => this.canDelete$ = observableOf(true))
493+
);
494+
})
495+
).subscribe(({ restResponse, eperson }: { restResponse: RemoteData<NoContent> | null, eperson: EPerson }) => {
496+
if (restResponse?.hasSucceeded) {
497+
this.notificationsService.success(this.translateService.get(this.labelPrefix + 'notification.deleted.success', { name: this.dsoNameService.getName(eperson) }));
498+
} else {
499+
this.notificationsService.error(`Error occurred when trying to delete EPerson with id: ${eperson?.id} with code: ${restResponse?.statusCode} and message: ${restResponse?.errorMessage}`);
500+
}
501+
this.cancelForm.emit();
491502
});
492503
}
493504

@@ -523,7 +534,6 @@ export class EPersonFormComponent implements OnInit, OnDestroy {
523534
* Cancel the current edit when component is destroyed & unsub all subscriptions
524535
*/
525536
ngOnDestroy(): void {
526-
this.onCancel();
527537
this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe());
528538
this.paginationService.clearPagination(this.config.id);
529539
if (hasValue(this.emailValueChangeSubscribe)) {

src/app/core/data/request.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class RequestService {
331331
map((request: RequestEntry) => isStale(request.state)),
332332
filter((stale: boolean) => stale),
333333
take(1),
334-
);
334+
);
335335
}
336336

337337
/**

0 commit comments

Comments
 (0)