Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ <h1>{{headerMessage | translate: {id: item.handle} }}</h1>
<div class="space-children-mr">
<button (click)="performAction()" class="btn btn-outline-secondary perform-action">{{confirmMessage | translate}}
</button>
<button [routerLink]="[itemPageRoute, 'edit']" class="btn btn-outline-secondary cancel">
{{cancelMessage| translate}}
<button (click)="router.navigateByUrl(returnUrl)" class="btn btn-outline-secondary cancel">
{{cancelMessage | translate}}
</button>
</div>
</div>
</div>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ describe('AbstractSimpleItemActionComponent', () => {
itemPageUrl = `fake-url/${mockItem.id}`;
routerStub = Object.assign(new RouterStub(), {
url: `${itemPageUrl}/edit`,
lastSuccessfulNavigation: {
previousNavigation: {
finalUrl: { toString: () => '/admin/search' },
},
},
});
routerStub.navigateByUrl = jasmine.createSpy('navigateByUrl');

mockItemDataService = jasmine.createSpyObj({
findById: createSuccessfulRemoteDataObject$(mockItem),
Expand Down Expand Up @@ -148,18 +154,28 @@ describe('AbstractSimpleItemActionComponent', () => {
expect(comp.performAction).toHaveBeenCalled();
});

it('should process a RemoteData to navigate and display success notification', () => {
it('should navigate to previous URL and show success notification on success', () => {
comp.processRestResponse(successfulRemoteData);

expect(notificationsServiceStub.success).toHaveBeenCalled();
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditRoute(mockItem)]);
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/admin/search');
});

it('should process a RemoteData to navigate and display success notification', () => {
it('should navigate to previous URL and show error notification on failure', () => {
comp.processRestResponse(failedRemoteData);

expect(notificationsServiceStub.error).toHaveBeenCalled();
expect(routerStub.navigate).toHaveBeenCalledWith([getItemEditRoute(mockItem)]);
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/admin/search');
});

it('should navigate to item edit route when no previous URL exists', () => {
routerStub.lastSuccessfulNavigation = null;
comp.ngOnInit();
fixture.detectChanges();

comp.processRestResponse(failedRemoteData);

expect(routerStub.navigateByUrl).toHaveBeenCalledWith(getItemEditRoute(mockItem));
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { getItemEditRoute } from '../../item-page-routing-paths';
import { findSuccessfulAccordingTo } from '../edit-item-operators';
import { ModifyItemOverviewComponent } from '../modify-item-overview/modify-item-overview.component';

/**
* Component to render and handle simple item edit actions such as withdrawal and reinstatement.
* This component is not meant to be used itself but to be extended.
Expand Down Expand Up @@ -57,6 +56,8 @@ export class AbstractSimpleItemActionComponent implements OnInit {
*/
itemPageRoute: string;

returnUrl: string;

protected predicate: Predicate<RemoteData<Item>>;

constructor(protected route: ActivatedRoute,
Expand All @@ -67,22 +68,28 @@ export class AbstractSimpleItemActionComponent implements OnInit {
}

ngOnInit(): void {
const previousUrl = this.router.lastSuccessfulNavigation?.previousNavigation?.finalUrl?.toString();
this.returnUrl = previousUrl ?? null;

this.itemRD$ = this.route.data.pipe(
map((data) => data.dso),
getFirstSucceededRemoteData(),
)as Observable<RemoteData<Item>>;
) as Observable<RemoteData<Item>>;

this.itemRD$.pipe(first()).subscribe((rd) => {
this.item = rd.payload;
this.itemPageRoute = getItemPageRoute(this.item);
},
);
if (!this.returnUrl) {
this.returnUrl = getItemEditRoute(this.item);
}
});

this.confirmMessage = 'item.edit.' + this.messageKey + '.confirm';
this.cancelMessage = 'item.edit.' + this.messageKey + '.cancel';
this.headerMessage = 'item.edit.' + this.messageKey + '.header';
this.descriptionMessage = 'item.edit.' + this.messageKey + '.description';
}

/**
* Perform the operation linked to this action
*/
Expand All @@ -100,11 +107,11 @@ export class AbstractSimpleItemActionComponent implements OnInit {
findSuccessfulAccordingTo((itemRd: RemoteData<Item>) => this.predicate(itemRd)),
).subscribe(() => {
this.notificationsService.success(this.translateService.get('item.edit.' + this.messageKey + '.success'));
this.router.navigate([getItemEditRoute(this.item)]);
this.router.navigateByUrl(this.returnUrl);
});
} else {
this.notificationsService.error(this.translateService.get('item.edit.' + this.messageKey + '.error'));
this.router.navigate([getItemEditRoute(this.item)]);
this.router.navigateByUrl(this.returnUrl);
}
}

Expand Down
Loading