Skip to content

Commit 2d81a48

Browse files
atarix83tdonohue
authored andcommitted
[DURACOM-247] Move check for initialized token to request effects
1 parent df8f0db commit 2d81a48

3 files changed

Lines changed: 14 additions & 27 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Injectable, Injector } from '@angular/core';
22

33
import { Actions, createEffect, ofType } from '@ngrx/effects';
4-
import { catchError, filter, map, mergeMap, take } from 'rxjs/operators';
4+
import { catchError, filter, map, mergeMap, take, withLatestFrom } from 'rxjs/operators';
55

66
import { hasValue, isNotEmpty } from '../../shared/empty.util';
77
import { StoreActionTypes } from '../../store.actions';
88
import { getClassForType } from '../cache/builders/build-decorators';
99
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
1010
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
1111
import { DSpaceSerializer } from '../dspace-rest/dspace.serializer';
12+
import { XSRFService } from '../xsrf/xsrf.service';
1213
import {
1314
RequestActionTypes,
1415
RequestErrorAction,
@@ -19,6 +20,7 @@ import {
1920
import { RequestService } from './request.service';
2021
import { ParsedResponse } from '../cache/response.models';
2122
import { RequestError } from './request-error.model';
23+
import { RestRequestMethod } from './rest-request-method';
2224
import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model';
2325
import { RequestEntry } from './request-entry.model';
2426

@@ -33,7 +35,11 @@ export class RequestEffects {
3335
);
3436
}),
3537
filter((entry: RequestEntry) => hasValue(entry)),
36-
map((entry: RequestEntry) => entry.request),
38+
withLatestFrom(this.xsrfService.tokenInitialized$),
39+
// If it's a GET request, or we have an XSRF token, dispatch it immediately
40+
// Otherwise wait for the XSRF token first
41+
filter(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request.method === RestRequestMethod.GET || tokenInitialized === true),
42+
map(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request),
3743
mergeMap((request: RestRequestWithResponseParser) => {
3844
let body = request.body;
3945
if (isNotEmpty(request.body) && !request.isMultipart) {
@@ -73,7 +79,8 @@ export class RequestEffects {
7379
private actions$: Actions,
7480
private restApi: DspaceRestService,
7581
private injector: Injector,
76-
protected requestService: RequestService
82+
protected requestService: RequestService,
83+
protected xsrfService: XSRFService,
7784
) { }
7885

7986
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Store, StoreModule } from '@ngrx/store';
22
import { cold, getTestScheduler } from 'jasmine-marbles';
3-
import { BehaviorSubject, EMPTY, Observable, of as observableOf } from 'rxjs';
3+
import { EMPTY, Observable, of as observableOf } from 'rxjs';
44
import { TestScheduler } from 'rxjs/testing';
55

66
import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock';
77
import { defaultUUID, getMockUUIDService } from '../../shared/mocks/uuid.service.mock';
88
import { ObjectCacheService } from '../cache/object-cache.service';
99
import { coreReducers} from '../core.reducers';
1010
import { UUIDService } from '../shared/uuid.service';
11-
import { XSRFService } from '../xsrf/xsrf.service';
1211
import { RequestConfigureAction, RequestExecuteAction, RequestStaleAction } from './request.actions';
1312
import {
1413
DeleteRequest,
@@ -36,7 +35,6 @@ describe('RequestService', () => {
3635
let uuidService: UUIDService;
3736
let store: Store<CoreState>;
3837
let mockStore: MockStore<CoreState>;
39-
let xsrfService: XSRFService;
4038

4139
const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb';
4240
const testHref = 'https://rest.api/endpoint/selfLink';
@@ -82,16 +80,11 @@ describe('RequestService', () => {
8280
store = TestBed.inject(Store);
8381
mockStore = store as MockStore<CoreState>;
8482
mockStore.setState(initialState);
85-
xsrfService = {
86-
tokenInitialized$: new BehaviorSubject(false),
87-
} as XSRFService;
8883

8984
service = new RequestService(
9085
objectCache,
9186
uuidService,
9287
store,
93-
xsrfService,
94-
undefined
9588
);
9689
serviceAsAny = service as any;
9790
});

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import cloneDeep from 'lodash/cloneDeep';
88
import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../../shared/empty.util';
99
import { ObjectCacheEntry } from '../cache/object-cache.reducer';
1010
import { ObjectCacheService } from '../cache/object-cache.service';
11-
import { IndexState, MetaIndexState } from '../index/index.reducer';
11+
import { IndexState } from '../index/index.reducer';
1212
import { requestIndexSelector, getUrlWithoutEmbedParams } from '../index/index.selectors';
1313
import { UUIDService } from '../shared/uuid.service';
14-
import { XSRFService } from '../xsrf/xsrf.service';
1514
import {
1615
RequestConfigureAction,
1716
RequestExecuteAction,
@@ -137,9 +136,7 @@ export class RequestService {
137136

138137
constructor(private objectCache: ObjectCacheService,
139138
private uuidService: UUIDService,
140-
private store: Store<CoreState>,
141-
protected xsrfService: XSRFService,
142-
private indexStore: Store<MetaIndexState>) {
139+
private store: Store<CoreState>) {
143140
}
144141

145142
generateRequestId(): string {
@@ -421,17 +418,7 @@ export class RequestService {
421418
*/
422419
private dispatchRequest(request: RestRequest) {
423420
this.store.dispatch(new RequestConfigureAction(request));
424-
// If it's a GET request, or we have an XSRF token, dispatch it immediately
425-
if (request.method === RestRequestMethod.GET || this.xsrfService.tokenInitialized$.getValue() === true) {
426-
this.store.dispatch(new RequestExecuteAction(request.uuid));
427-
} else {
428-
// Otherwise wait for the XSRF token first
429-
this.xsrfService.tokenInitialized$.pipe(
430-
find((hasInitialized: boolean) => hasInitialized === true),
431-
).subscribe(() => {
432-
this.store.dispatch(new RequestExecuteAction(request.uuid));
433-
});
434-
}
421+
this.store.dispatch(new RequestExecuteAction(request.uuid));
435422
}
436423

437424
/**

0 commit comments

Comments
 (0)