Files
dspace-angular/src/app/shared/resource-policies/edit/resource-policy-edit.component.ts
2020-12-11 09:53:37 +01:00

105 lines
3.5 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core';
import { ResourcePolicyService } from '../../../core/resource-policy/resource-policy.service';
import { NotificationsService } from '../../notifications/notifications.service';
import { RemoteData } from '../../../core/data/remote-data';
import { ResourcePolicy } from '../../../core/resource-policy/models/resource-policy.model';
import { ResourcePolicyEvent } from '../form/resource-policy-form.component';
import { RESOURCE_POLICY } from '../../../core/resource-policy/models/resource-policy.resource-type';
import { ITEM_EDIT_AUTHORIZATIONS_PATH } from '../../../+item-page/edit-item-page/edit-item-page.routing-paths';
import { getSucceededRemoteWithNotEmptyDataOrFailed } from '../../../core/shared/operators';
@Component({
selector: 'ds-resource-policy-edit',
templateUrl: './resource-policy-edit.component.html'
})
export class ResourcePolicyEditComponent implements OnInit {
/**
* The resource policy object to edit
*/
public resourcePolicy: ResourcePolicy;
/**
* A boolean representing if a submission editing operation is pending
* @type {BehaviorSubject<boolean>}
*/
private processing$ = new BehaviorSubject<boolean>(false);
/**
* Initialize instance variables
*
* @param {NotificationsService} notificationsService
* @param {ResourcePolicyService} resourcePolicyService
* @param {ActivatedRoute} route
* @param {Router} router
* @param {TranslateService} translate
*/
constructor(
private notificationsService: NotificationsService,
private resourcePolicyService: ResourcePolicyService,
private route: ActivatedRoute,
private router: Router,
private translate: TranslateService) {
}
/**
* Initialize the component
*/
ngOnInit(): void {
this.route.data.pipe(
map((data) => data),
take(1)
).subscribe((data: any) => {
this.resourcePolicy = (data.resourcePolicy as RemoteData<ResourcePolicy>).payload;
});
}
/**
* Return a boolean representing if an operation is pending
*
* @return {Observable<boolean>}
*/
isProcessing(): Observable<boolean> {
return this.processing$.asObservable();
}
/**
* Redirect to the authorizations page
*/
redirectToAuthorizationsPage() {
this.router.navigate([`../../${ITEM_EDIT_AUTHORIZATIONS_PATH}`], { relativeTo: this.route });
}
/**
* Update a resource policy
*
* @param event The {{ResourcePolicyEvent}} emitted
*/
updateResourcePolicy(event: ResourcePolicyEvent) {
this.processing$.next(true);
const updatedObject = Object.assign({}, event.object, {
id: this.resourcePolicy.id,
type: RESOURCE_POLICY.value,
_links: this.resourcePolicy._links
});
this.resourcePolicyService.update(updatedObject).pipe(
getSucceededRemoteWithNotEmptyDataOrFailed(),
take(1)
).subscribe((responseRD: RemoteData<ResourcePolicy>) => {
this.processing$.next(false);
if (responseRD && responseRD.hasSucceeded) {
this.notificationsService.success(null, this.translate.get('resource-policies.edit.page.success.content'));
this.redirectToAuthorizationsPage();
} else {
this.notificationsService.error(null, this.translate.get('resource-policies.edit.page.failure.content'));
}
})
}
}