|
8 | 8 | } from '@ng-dynamic-forms/core'; |
9 | 9 | import { TranslateService } from '@ngx-translate/core'; |
10 | 10 | 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'; |
12 | 12 | import { PaginatedList } from '../../../core/data/paginated-list.model'; |
13 | 13 | import { RemoteData } from '../../../core/data/remote-data'; |
14 | 14 | import { EPersonDataService } from '../../../core/eperson/eperson-data.service'; |
@@ -463,31 +463,42 @@ export class EPersonFormComponent implements OnInit, OnDestroy { |
463 | 463 | * Deletes the EPerson from the Repository. The EPerson will be the only that this form is showing. |
464 | 464 | * It'll either show a success or error message depending on whether the delete was successful or not. |
465 | 465 | */ |
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(); |
491 | 502 | }); |
492 | 503 | } |
493 | 504 |
|
@@ -523,7 +534,6 @@ export class EPersonFormComponent implements OnInit, OnDestroy { |
523 | 534 | * Cancel the current edit when component is destroyed & unsub all subscriptions |
524 | 535 | */ |
525 | 536 | ngOnDestroy(): void { |
526 | | - this.onCancel(); |
527 | 537 | this.subs.filter((sub) => hasValue(sub)).forEach((sub) => sub.unsubscribe()); |
528 | 538 | this.paginationService.clearPagination(this.config.id); |
529 | 539 | if (hasValue(this.emailValueChangeSubscribe)) { |
|
0 commit comments