[CST-4499] Version history - Code refactoring (WIP) and "Edit workspace item" button

This commit is contained in:
Davide Negretti
2021-10-15 13:05:24 +02:00
parent b7d6c8e557
commit 850970e204
8 changed files with 67 additions and 11 deletions

View File

@@ -170,7 +170,7 @@ export class VersionHistoryDataService extends DataService<VersionHistory> {
*/
hasDraftVersion$(versionHref: string): Observable<boolean> {
return this.versionDataService.findByHref(versionHref, true, true, followLink('versionhistory')).pipe(
take(1),
getFirstCompletedRemoteData(),
switchMap((res) => {
if (res.hasSucceeded && !res.hasNoContent) {
return of(res).pipe(

View File

@@ -43,14 +43,18 @@ export class WorkspaceitemDataService extends DataService<WorkspaceItem> {
* Return the WorkspaceItem object found through the UUID of an item
*
* @param uuid The uuid of the item
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param options The {@link FindListOptions} object
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved
*/
public findByItem(uuid: string, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> {
public findByItem(uuid: string, useCachedVersionIfAvailable = false, reRequestOnStale = true, options: FindListOptions = {}, ...linksToFollow: FollowLinkConfig<WorkspaceItem>[]): Observable<RemoteData<WorkspaceItem>> {
const findListOptions = new FindListOptions();
findListOptions.searchParams = [new RequestParam('uuid', encodeURIComponent(uuid))];
const href$ = this.getSearchByHref(this.searchByItemLinkPath, findListOptions, ...linksToFollow);
return this.findByHref(href$, false, true, ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
}

View File

@@ -1 +0,0 @@
<p>versioned-item works!</p>

View File

@@ -4,7 +4,7 @@ import { ItemVersionsSummaryModalComponent } from '../../../../shared/item/item-
import { getFirstCompletedRemoteData, getFirstSucceededRemoteDataPayload } from '../../../../core/shared/operators';
import { RemoteData } from '../../../../core/data/remote-data';
import { Version } from '../../../../core/shared/version.model';
import { switchMap } from 'rxjs/operators';
import { switchMap, tap } from 'rxjs/operators';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { VersionHistoryDataService } from '../../../../core/data/version-history-data.service';
import { TranslateService } from '@ngx-translate/core';
@@ -58,11 +58,15 @@ export class VersionedItemComponent extends ItemComponent {
// On createVersionEvent emitted create new version and notify
activeModal.componentInstance.createVersionEvent.pipe(
switchMap((summary: string) => this.itemVersionShared.createNewVersionAndNotify(item, summary)),
switchMap((summary: string) => this.versionHistoryService.createVersion(item._links.self.href, summary)),
getFirstCompletedRemoteData(),
// show success/failure notification
tap((res: RemoteData<Version>) => { this.itemVersionShared.notifyCreateNewVersion(res); }),
getFirstSucceededRemoteDataPayload<Version>(),
// get workspace item
switchMap((newVersion: Version) => this.itemService.findByHref(newVersion._links.item.href)),
getFirstSucceededRemoteDataPayload<Item>(),
switchMap((newVersionItem: Item) => this.workspaceitemDataService.findByItem(newVersionItem.uuid)),
switchMap((newVersionItem: Item) => this.workspaceitemDataService.findByItem(newVersionItem.uuid, true, false)),
getFirstSucceededRemoteDataPayload<WorkspaceItem>(),
).subscribe((wsItem) => {
const wsiId = wsItem.id;

View File

@@ -35,13 +35,20 @@ export class ItemVersionsSharedService {
return this.versionHistoryService.createVersion(item._links.self.href, summary).pipe(
switchMap((postResult: RemoteData<Version>) => {
const newVersionNumber = postResult?.payload?.version;
this.notifyCreateNewVersion(postResult.hasSucceeded, postResult.statusCode, newVersionNumber);
this.notifyCreateNewVersionBak(postResult.hasSucceeded, postResult.statusCode, newVersionNumber);
return of(postResult);
})
);
}
private notifyCreateNewVersion(success: boolean, statusCode: number, newVersionNumber: number) {
public notifyCreateNewVersion(newVersionRD: RemoteData<Version>) {
const newVersionNumber = newVersionRD?.payload?.version;
newVersionRD.hasSucceeded ?
this.notificationsService.success(null, this.translateService.get(ItemVersionsSharedService.msg('success'), {version: newVersionNumber})) :
this.notificationsService.error(null, this.translateService.get(ItemVersionsSharedService.msg(newVersionRD?.statusCode === 422 ? 'inProgress' : 'failure')));
}
private notifyCreateNewVersionBak(success: boolean, statusCode: number, newVersionNumber: number) { // TODO delete this
success ?
this.notificationsService.success(null, this.translateService.get(ItemVersionsSharedService.msg('success'), {version: newVersionNumber})) :
this.notificationsService.error(null, this.translateService.get(ItemVersionsSharedService.msg(statusCode === 422 ? 'inProgress' : 'failure')));

View File

@@ -25,8 +25,20 @@
<tbody>
<tr *ngFor="let version of versions?.page" [id]="'version-row-' + version.id">
<td class="version-row-element-version">
<div>
<div class="left-column">
<a [routerLink]="getVersionRoute(version.id)">{{version.version}}</a>
<span *ngIf="version?.id === itemVersion?.id">*</span>
</div>
<div class="right-column">
<ng-container *ngVar="getDraftId(version?.item) as draftId$">
<button class="btn btn-outline-primary btn-sm version-row-element-edit" *ngIf="draftId$ | async"
(click)="editWorkspaceItem(draftId$)">
<i class="fas fa-pencil-alt fa-fw"></i> {{ "item.version.history.table.workspaceItem" | translate }}
</button>
</ng-container>
</div>
</div>
</td>
<td *ngIf="(hasEpersons$ | async)" class="version-row-element-editor">
<span *ngVar="(version?.eperson | async)?.payload as eperson">

View File

@@ -45,6 +45,8 @@ import { Router } from '@angular/router';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { ItemVersionsSharedService } from './item-versions-shared.service';
import { WorkspaceItem } from '../../../core/submission/models/workspaceitem.model';
import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service';
@Component({
selector: 'ds-item-versions',
@@ -170,6 +172,7 @@ export class ItemVersionsComponent implements OnInit {
private router: Router,
private itemVersionShared: ItemVersionsSharedService,
private authorizationService: AuthorizationDataService,
private workspaceItemDataService: WorkspaceitemDataService,
) {
}
@@ -379,6 +382,30 @@ export class ItemVersionsComponent implements OnInit {
});
}
/**
* Get the ID of the workspace item, if present, otherwise return undefined
* @param versionItem the item for which retrieve the workspace item id
*/
getDraftId(versionItem): Observable<string> {
return versionItem.pipe(
getFirstSucceededRemoteDataPayload(),
map((item: Item) => item.uuid),
switchMap((itemUuid: string) => this.workspaceItemDataService.findByItem(itemUuid, true)),
getFirstCompletedRemoteData<WorkspaceItem>(),
map((res: RemoteData<WorkspaceItem>) => res?.payload?.id ),
);
}
/**
* redirect to the edit page of the workspace item
* @param id$ the id of the workspace item
*/
editWorkspaceItem(id$: Observable<string>) {
id$.subscribe((id) => {
this.router.navigateByUrl('workspaceitems/' + id + '/edit');
});
}
/**
* Initialize all observables
*/
@@ -399,6 +426,7 @@ export class ItemVersionsComponent implements OnInit {
getFirstSucceededRemoteDataPayload(),
map((res) => Boolean(res?.draftVersion)),
);
this.createVersionTitle$ = this.hasDraftVersion$.pipe(
take(1),
switchMap((res) => of(res ? 'item.version.history.table.action.hasDraft' : 'item.version.history.table.action.newVersion'))

View File

@@ -1981,6 +1981,8 @@
"item.version.history.table.summary": "Summary",
"item.version.history.table.workspaceItem": "Workspace item",
"item.version.history.table.actions": "Action",