@@ -6,23 +6,21 @@ import { ObjectCacheService } from '../cache/object-cache.service';
6
6
import { HALEndpointService } from '../shared/hal-endpoint.service' ;
7
7
import { HttpHeaders } from '@angular/common/http' ;
8
8
import { PostRequest } from './request.models' ;
9
- import { Observable , of } from 'rxjs' ;
9
+ import { combineLatest , Observable , of as observableOf } from 'rxjs' ;
10
10
import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model' ;
11
11
import { RemoteData } from './remote-data' ;
12
12
import { PaginatedList } from './paginated-list.model' ;
13
13
import { Version } from '../shared/version.model' ;
14
- import { filter , map , switchMap , take } from 'rxjs/operators' ;
14
+ import { filter , find , map , switchMap , take } from 'rxjs/operators' ;
15
15
import { VERSION_HISTORY } from '../shared/version-history.resource-type' ;
16
16
import { followLink , FollowLinkConfig } from '../../shared/utils/follow-link-config.model' ;
17
17
import { VersionDataService } from './version-data.service' ;
18
18
import { HttpOptions } from '../dspace-rest/dspace-rest.service' ;
19
19
import { getAllSucceededRemoteData , getFirstCompletedRemoteData , getFirstSucceededRemoteDataPayload , getRemoteDataPayload } from '../shared/operators' ;
20
20
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model' ;
21
- import { hasValueOperator } from '../../shared/empty.util' ;
21
+ import { hasValue , hasValueOperator } from '../../shared/empty.util' ;
22
22
import { Item } from '../shared/item.model' ;
23
23
import { FindListOptions } from './find-list-options.model' ;
24
- import { sendRequest } from '../shared/request.operators' ;
25
- import { RestRequest } from './rest-request.model' ;
26
24
import { IdentifiableDataService } from './base/identifiable-data.service' ;
27
25
import { dataService } from './base/data-service.decorator' ;
28
26
@@ -86,19 +84,31 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
86
84
* @param summary the summary of the new version
87
85
*/
88
86
createVersion ( itemHref : string , summary : string ) : Observable < RemoteData < Version > > {
87
+ const requestId = this . requestService . generateRequestId ( ) ;
89
88
const requestOptions : HttpOptions = Object . create ( { } ) ;
90
89
let requestHeaders = new HttpHeaders ( ) ;
91
90
requestHeaders = requestHeaders . append ( 'Content-Type' , 'text/uri-list' ) ;
92
91
requestOptions . headers = requestHeaders ;
93
92
94
- return this . halService . getEndpoint ( this . versionsEndpoint ) . pipe (
93
+ this . halService . getEndpoint ( this . versionsEndpoint ) . pipe (
95
94
take ( 1 ) ,
96
95
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 > > ;
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 ) ;
104
+ } ) ;
105
+
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
+ ) ;
102
112
}
103
113
104
114
/**
@@ -137,7 +147,7 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
137
147
switchMap ( ( res ) => res . versionhistory ) ,
138
148
getFirstSucceededRemoteDataPayload ( ) ,
139
149
switchMap ( ( versionHistoryRD ) => this . getLatestVersionFromHistory$ ( versionHistoryRD ) ) ,
140
- ) : of ( null ) ;
150
+ ) : observableOf ( null ) ;
141
151
}
142
152
143
153
/**
@@ -148,8 +158,8 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
148
158
isLatest$ ( version : Version ) : Observable < boolean > {
149
159
return version ? this . getLatestVersion$ ( version ) . pipe (
150
160
take ( 1 ) ,
151
- switchMap ( ( latestVersion ) => of ( version . version === latestVersion . version ) )
152
- ) : of ( null ) ;
161
+ switchMap ( ( latestVersion ) => observableOf ( version . version === latestVersion . version ) )
162
+ ) : observableOf ( null ) ;
153
163
}
154
164
155
165
/**
@@ -158,17 +168,22 @@ export class VersionHistoryDataService extends IdentifiableDataService<VersionHi
158
168
* @returns `true` if a workspace item exists, `false` otherwise, or `null` if a version history does not exist
159
169
*/
160
170
hasDraftVersion$ ( versionHref : string ) : Observable < boolean > {
161
- return this . versionDataService . findByHref ( versionHref , true , true , followLink ( 'versionhistory' ) ) . pipe (
171
+ return this . versionDataService . findByHref ( versionHref , false , true , followLink ( 'versionhistory' ) ) . pipe (
162
172
getFirstCompletedRemoteData ( ) ,
163
- switchMap ( ( res ) => {
164
- if ( res . hasSucceeded && ! res . hasNoContent ) {
165
- return of ( res ) . pipe (
166
- getFirstSucceededRemoteDataPayload ( ) ,
167
- switchMap ( ( version ) => this . versionDataService . getHistoryFromVersion ( version ) ) ,
168
- map ( ( versionHistory ) => versionHistory ? versionHistory . draftVersion : false ) ,
173
+ switchMap ( ( versionRD : RemoteData < Version > ) => {
174
+ if ( versionRD . hasSucceeded && ! versionRD . hasNoContent ) {
175
+ return versionRD . payload . versionhistory . pipe (
176
+ getFirstCompletedRemoteData ( ) ,
177
+ map ( ( versionHistoryRD : RemoteData < VersionHistory > ) => {
178
+ if ( versionHistoryRD . hasSucceeded && ! versionHistoryRD . hasNoContent ) {
179
+ return versionHistoryRD . payload . draftVersion ;
180
+ } else {
181
+ return false ;
182
+ }
183
+ } ) ,
169
184
) ;
170
185
} else {
171
- return of ( false ) ;
186
+ return observableOf ( false ) ;
172
187
}
173
188
} ) ,
174
189
) ;
0 commit comments