@@ -6,23 +6,21 @@ import { ObjectCacheService } from '../cache/object-cache.service';
66import { HALEndpointService } from '../shared/hal-endpoint.service' ;
77import { HttpHeaders } from '@angular/common/http' ;
88import { PostRequest } from './request.models' ;
9- import { Observable , of } from 'rxjs' ;
9+ import { combineLatest , Observable , of as observableOf } from 'rxjs' ;
1010import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model' ;
1111import { RemoteData } from './remote-data' ;
1212import { PaginatedList } from './paginated-list.model' ;
1313import { Version } from '../shared/version.model' ;
14- import { filter , map , switchMap , take } from 'rxjs/operators' ;
14+ import { filter , find , map , switchMap , take } from 'rxjs/operators' ;
1515import { VERSION_HISTORY } from '../shared/version-history.resource-type' ;
1616import { followLink , FollowLinkConfig } from '../../shared/utils/follow-link-config.model' ;
1717import { VersionDataService } from './version-data.service' ;
1818import { HttpOptions } from '../dspace-rest/dspace-rest.service' ;
1919import { getAllSucceededRemoteData , getFirstCompletedRemoteData , getFirstSucceededRemoteDataPayload , getRemoteDataPayload } from '../shared/operators' ;
2020import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model' ;
21- import { hasValueOperator } from '../../shared/empty.util' ;
21+ import { hasValue , hasValueOperator } from '../../shared/empty.util' ;
2222import { Item } from '../shared/item.model' ;
2323import { FindListOptions } from './find-list-options.model' ;
24- import { sendRequest } from '../shared/request.operators' ;
25- import { RestRequest } from './rest-request.model' ;
2624import { IdentifiableDataService } from './base/identifiable-data.service' ;
2725import { dataService } from './base/data-service.decorator' ;
2826
@@ -86,29 +84,31 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
8684 * @param summary the summary of the new version
8785 */
8886 createVersion ( itemHref : string , summary : string ) : Observable < RemoteData < Version > > {
87+ const requestId = this . requestService . generateRequestId ( ) ;
8988 const requestOptions : HttpOptions = Object . create ( { } ) ;
9089 let requestHeaders = new HttpHeaders ( ) ;
9190 requestHeaders = requestHeaders . append ( 'Content-Type' , 'text/uri-list' ) ;
9291 requestOptions . headers = requestHeaders ;
9392
94- const response$ = this . halService . getEndpoint ( this . versionsEndpoint ) . pipe (
93+ this . halService . getEndpoint ( this . versionsEndpoint ) . pipe (
9594 take ( 1 ) ,
9695 map ( ( endpointUrl : string ) => ( summary ?. length > 0 ) ? `${ endpointUrl } ?summary=${ summary } ` : `${ endpointUrl } ` ) ,
97- map ( ( endpointURL : string ) => new PostRequest ( this . requestService . generateRequestId ( ) , endpointURL , itemHref , requestOptions ) ) ,
98- sendRequest ( this . requestService ) ,
99- switchMap ( ( restRequest : RestRequest ) => this . rdbService . buildFromRequestUUID ( restRequest . uuid ) ) ,
100- getFirstCompletedRemoteData ( )
101- ) as Observable < RemoteData < Version > > ;
102-
103- response$ . subscribe ( ( versionRD : RemoteData < Version > ) => {
104- // invalidate version history
105- // note: we should do this regardless of whether the request succeeds,
106- // because it may have failed due to cached data that is out of date
107- this . requestService . setStaleByHrefSubstring ( versionRD . payload . _links . self . href ) ;
108- this . requestService . setStaleByHrefSubstring ( versionRD . payload . _links . versionhistory . href ) ;
96+ find ( ( href : string ) => hasValue ( href ) ) ,
97+ ) . subscribe ( ( href ) => {
98+ const request = new PostRequest ( requestId , href , itemHref , requestOptions ) ;
99+ if ( hasValue ( this . responseMsToLive ) ) {
100+ request . responseMsToLive = this . responseMsToLive ;
101+ }
102+
103+ this . requestService . send ( request ) ;
109104 } ) ;
110105
111- return response$ ;
106+ return this . rdbService . buildFromRequestUUIDAndAwait < Version > ( requestId , ( versionRD ) => combineLatest ( [
107+ this . requestService . setStaleByHrefSubstring ( versionRD . payload . _links . self . href ) ,
108+ this . requestService . setStaleByHrefSubstring ( versionRD . payload . _links . versionhistory . href ) ,
109+ ] ) ) . pipe (
110+ getFirstCompletedRemoteData ( ) ,
111+ ) ;
112112 }
113113
114114 /**
@@ -147,7 +147,7 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
147147 switchMap ( ( res ) => res . versionhistory ) ,
148148 getFirstSucceededRemoteDataPayload ( ) ,
149149 switchMap ( ( versionHistoryRD ) => this . getLatestVersionFromHistory$ ( versionHistoryRD ) ) ,
150- ) : of ( null ) ;
150+ ) : observableOf ( null ) ;
151151 }
152152
153153 /**
@@ -158,8 +158,8 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
158158 isLatest$ ( version : Version ) : Observable < boolean > {
159159 return version ? this . getLatestVersion$ ( version ) . pipe (
160160 take ( 1 ) ,
161- switchMap ( ( latestVersion ) => of ( version . version === latestVersion . version ) )
162- ) : of ( null ) ;
161+ switchMap ( ( latestVersion ) => observableOf ( version . version === latestVersion . version ) )
162+ ) : observableOf ( null ) ;
163163 }
164164
165165 /**
@@ -170,21 +170,20 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
170170 hasDraftVersion$ ( versionHref : string ) : Observable < boolean > {
171171 return this . versionDataService . findByHref ( versionHref , false , true , followLink ( 'versionhistory' ) ) . pipe (
172172 getFirstCompletedRemoteData ( ) ,
173- switchMap ( ( res ) => {
174- if ( res . hasSucceeded && ! res . hasNoContent ) {
175- return res . payload . versionhistory . pipe (
173+ switchMap ( ( versionRD : RemoteData < Version > ) => {
174+ if ( versionRD . hasSucceeded && ! versionRD . hasNoContent ) {
175+ return versionRD . payload . versionhistory . pipe (
176176 getFirstCompletedRemoteData ( ) ,
177- map ( ( versionHistoryRD ) => {
178- if ( res . hasSucceeded ) {
179- const versionHistory = versionHistoryRD . payload ;
180- return versionHistory ? versionHistory . draftVersion : false ;
177+ map ( ( versionHistoryRD : RemoteData < VersionHistory > ) => {
178+ if ( versionHistoryRD . hasSucceeded && ! versionHistoryRD . hasNoContent ) {
179+ return versionHistoryRD . payload . draftVersion ;
181180 } else {
182181 return false ;
183182 }
184183 } ) ,
185184 ) ;
186185 } else {
187- return of ( false ) ;
186+ return observableOf ( false ) ;
188187 }
189188 } ) ,
190189 ) ;
0 commit comments