Skip to content

Commit d2ec558

Browse files
committed
[DURACOM-247] Move check for initialized token to request effects
1 parent 4020432 commit d2ec558

3 files changed

Lines changed: 12 additions & 28 deletions

File tree

src/app/core/data/request.effects.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
map,
1414
mergeMap,
1515
take,
16+
withLatestFrom,
1617
} from 'rxjs/operators';
1718

1819
import {
@@ -25,6 +26,7 @@ import { ParsedResponse } from '../cache/response.models';
2526
import { DSpaceSerializer } from '../dspace-rest/dspace.serializer';
2627
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
2728
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
29+
import { XSRFService } from '../xsrf/xsrf.service';
2830
import {
2931
RequestActionTypes,
3032
RequestErrorAction,
@@ -35,6 +37,7 @@ import {
3537
import { RequestService } from './request.service';
3638
import { RequestEntry } from './request-entry.model';
3739
import { RequestError } from './request-error.model';
40+
import { RestRequestMethod } from './rest-request-method';
3841
import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model';
3942

4043
@Injectable()
@@ -48,7 +51,11 @@ export class RequestEffects {
4851
);
4952
}),
5053
filter((entry: RequestEntry) => hasValue(entry)),
51-
map((entry: RequestEntry) => entry.request),
54+
withLatestFrom(this.xsrfService.tokenInitialized$),
55+
// If it's a GET request, or we have an XSRF token, dispatch it immediately
56+
// Otherwise wait for the XSRF token first
57+
filter(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request.method === RestRequestMethod.GET || tokenInitialized === true),
58+
map(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request),
5259
mergeMap((request: RestRequestWithResponseParser) => {
5360
let body = request.body;
5461
if (isNotEmpty(request.body) && !request.isMultipart) {
@@ -89,6 +96,7 @@ export class RequestEffects {
8996
private restApi: DspaceRestService,
9097
private injector: Injector,
9198
protected requestService: RequestService,
99+
protected xsrfService: XSRFService,
92100
) { }
93101

94102
}

src/app/core/data/request.service.spec.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
getTestScheduler,
1818
} from 'jasmine-marbles';
1919
import {
20-
BehaviorSubject,
2120
EMPTY,
2221
Observable,
2322
of as observableOf,
@@ -34,7 +33,6 @@ import { ObjectCacheService } from '../cache/object-cache.service';
3433
import { coreReducers } from '../core.reducers';
3534
import { CoreState } from '../core-state.model';
3635
import { UUIDService } from '../shared/uuid.service';
37-
import { XSRFService } from '../xsrf/xsrf.service';
3836
import {
3937
RequestConfigureAction,
4038
RequestExecuteAction,
@@ -62,7 +60,6 @@ describe('RequestService', () => {
6260
let uuidService: UUIDService;
6361
let store: Store<CoreState>;
6462
let mockStore: MockStore<CoreState>;
65-
let xsrfService: XSRFService;
6663

6764
const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb';
6865
const testHref = 'https://rest.api/endpoint/selfLink';
@@ -108,16 +105,11 @@ describe('RequestService', () => {
108105
store = TestBed.inject(Store);
109106
mockStore = store as MockStore<CoreState>;
110107
mockStore.setState(initialState);
111-
xsrfService = {
112-
tokenInitialized$: new BehaviorSubject(false),
113-
} as XSRFService;
114108

115109
service = new RequestService(
116110
objectCache,
117111
uuidService,
118112
store,
119-
xsrfService,
120-
undefined,
121113
);
122114
serviceAsAny = service as any;
123115
});

src/app/core/data/request.service.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@ import { ObjectCacheService } from '../cache/object-cache.service';
3434
import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
3535
import { coreSelector } from '../core.selectors';
3636
import { CoreState } from '../core-state.model';
37-
import {
38-
IndexState,
39-
MetaIndexState,
40-
} from '../index/index.reducer';
37+
import { IndexState } from '../index/index.reducer';
4138
import {
4239
getUrlWithoutEmbedParams,
4340
requestIndexSelector,
4441
} from '../index/index.selectors';
4542
import { UUIDService } from '../shared/uuid.service';
46-
import { XSRFService } from '../xsrf/xsrf.service';
4743
import {
4844
RequestConfigureAction,
4945
RequestExecuteAction,
@@ -169,9 +165,7 @@ export class RequestService {
169165

170166
constructor(private objectCache: ObjectCacheService,
171167
private uuidService: UUIDService,
172-
private store: Store<CoreState>,
173-
protected xsrfService: XSRFService,
174-
private indexStore: Store<MetaIndexState>) {
168+
private store: Store<CoreState>) {
175169
}
176170

177171
generateRequestId(): string {
@@ -455,17 +449,7 @@ export class RequestService {
455449
private dispatchRequest(request: RestRequest) {
456450
asapScheduler.schedule(() => {
457451
this.store.dispatch(new RequestConfigureAction(request));
458-
// If it's a GET request, or we have an XSRF token, dispatch it immediately
459-
if (request.method === RestRequestMethod.GET || this.xsrfService.tokenInitialized$.getValue() === true) {
460-
this.store.dispatch(new RequestExecuteAction(request.uuid));
461-
} else {
462-
// Otherwise wait for the XSRF token first
463-
this.xsrfService.tokenInitialized$.pipe(
464-
find((hasInitialized: boolean) => hasInitialized === true),
465-
).subscribe(() => {
466-
this.store.dispatch(new RequestExecuteAction(request.uuid));
467-
});
468-
}
452+
this.store.dispatch(new RequestExecuteAction(request.uuid));
469453
});
470454
}
471455

0 commit comments

Comments
 (0)