Skip to content

Commit 4628332

Browse files
committed
Eliminated latestAction, added response logging
1 parent 309ca98 commit 4628332

12 files changed

+71
-96
lines changed

src/app/app.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export class AppComponent {
3131
this.selectedLanguage = ui.lang;
3232
});
3333

34-
this.store.select('document').subscribe((document) => {
35-
console.log(document.open ? "|openDocument" : "|closeDocument");
36-
this.openDocument = document.open;
34+
this.store.select('document').subscribe((documentState) => {
35+
this.openDocument = documentState.open;
3736
});
3837

3938
this.languages = this.locService.getAvailableLanguages();

src/app/calls.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Injectable } from '@angular/core';
22
import { HttpHeaders, HttpParams, HttpClient, HttpErrorResponse } from '@angular/common/http';
33
import { Observable, throwError, of } from 'rxjs';
4-
import { map, catchError } from 'rxjs/operators';
5-
4+
import { map, catchError, tap } from 'rxjs/operators';
65
import * as _ from 'lodash';
76

87
import { StrixDocument } from './strixdocument.model';
@@ -49,7 +48,9 @@ export class CallsService {
4948
params : new HttpParams({fromObject : params}),
5049
headers : window['jwt'] ? new HttpHeaders({'Authorization' : `Bearer ${window['jwt']}`}) : null,
5150
};
52-
return this.http.get<T>(this.STRIXBACKEND_URL + '/' + endpoint, options);
51+
console.log('GET', endpoint, params, !!window['jwt']);
52+
return this.http.get<T>(this.STRIXBACKEND_URL + '/' + endpoint, options)
53+
.pipe(tap(data => console.log('Response', endpoint, data)));
5354
}
5455

5556
public getCorpusInfo(): Observable<{[key: string]: StrixCorpusConfig}> {
@@ -83,7 +84,7 @@ export class CallsService {
8384
private extractLocalizationKeys(config) {
8485
for (let corpusID in config) {
8586
let corpusData = config[corpusID];
86-
87+
8788
let updateObj: any = _.mapValues(corpusData.name, (name) => {
8889
return _.fromPairs([[corpusID, name]])
8990
})
@@ -117,7 +118,7 @@ export class CallsService {
117118
filter.value.range.lte = filter.value.range.lte + "1231";
118119
filter.value.range.gte = filter.value.range.gte + "0101";
119120
}
120-
121+
121122
// filterStrings.push(`"${filter.field}":${JSON.stringify(value)}`);
122123
}
123124

@@ -129,7 +130,7 @@ export class CallsService {
129130
let rangeFilters = _.filter(filters, {type: "range"});
130131
let output = wrapValuesInArray(notRangeFilters)
131132
let rangeObj = _.fromPairs(_.map(rangeFilters, (item) => [item.field, item.value]))
132-
133+
133134
return JSON.stringify(_.merge(output, rangeObj))
134135
}
135136

src/app/docselection/docselection.component.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class DocselectionComponent implements OnInit {
2121

2222
private hasSearched = false;
2323
private disablePaginatorEvent = false;
24-
24+
2525
private currentPaginatorPage: number = 1; // Needs to be 1-based because of the paginator widget
2626

2727
private documentsWithHits: StrixDocument[] = [];//StrixDocHit[] = [];
@@ -53,16 +53,9 @@ export class DocselectionComponent implements OnInit {
5353
}
5454
});
5555

56-
this.store.select('ui').pipe(filter((d) => d.latestAction === OPENDOCUMENT)).subscribe(() => {
57-
console.log("OPENDOCUMENT");
58-
this.documentsWithHits = [];
59-
this.totalNumberOfDocuments = 0;
60-
this.hasSearched = false;
61-
//this.show = false;
56+
this.store.select('query').subscribe(queryState => {
57+
this.currentPaginatorPage = queryState.page;
6258
});
63-
this.store.pipe(filter((d) => d.ui.latestAction === CHANGEQUERY)).subscribe((data) => {
64-
this.currentPaginatorPage = data.query.page;
65-
})
6659

6760
this.searchResultSubscription = queryService.searchResult$.subscribe(
6861
(answer: SearchResult) => {
@@ -71,18 +64,16 @@ export class DocselectionComponent implements OnInit {
7164
this.totalNumberOfDocuments = answer.count;
7265
this.hasSearched = true;
7366

74-
67+
7568
},
7669
error => null//this.errorMessage = <any>error
7770
);
7871

7972
}
8073

8174
ngOnInit() {
82-
this.store.pipe(filter((d) => d.ui.latestAction === INITIATE)).subscribe((data) => {
83-
console.log("init", data)
84-
//this.currentPaginatorPage = data.page
85-
this.setPaginatorPage(data.query.page)
75+
this.store.select('query').subscribe(queryState => {
76+
this.setPaginatorPage(queryState.page)
8677
})
8778
}
8879

src/app/documents.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { BehaviorSubject, Observable, Subject } from 'rxjs';
3-
import { filter, map } from 'rxjs/operators';
3+
import { filter, map, tap } from 'rxjs/operators';
44
import { Store } from '@ngrx/store';
55
import * as _ from 'lodash';
66

@@ -41,7 +41,7 @@ export class DocumentsService {
4141
loadedDocument$: Observable<StrixMessage> = this.loadedDocument.asObservable();
4242

4343
private docLoadingStatusSubject = new BehaviorSubject<StrixEvent>(StrixEvent.INIT);
44-
docLoadingStatus$ = this.docLoadingStatusSubject.asObservable();
44+
docLoadingStatus$ = this.docLoadingStatusSubject.asObservable().pipe(tap(o => console.log('docLoadingStatus', o)));
4545

4646
constructor(private callsService: CallsService,
4747
private queryService: QueryService,

src/app/leftcolumn/leftcolumn.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class LeftcolumnComponent implements OnInit {
2626
private metadataSubscription: Subscription;
2727
private aggregatedResultSubscription: Subscription;
2828

29-
private aggregations : Aggregations = {};
29+
public aggregations: Aggregations = {};
3030
private aggregationKeys: string[] = [];
3131
//private currentFilters: any[] = []; // TODO: Make some interface
3232
private unusedFacets : string[] = [];
@@ -249,10 +249,10 @@ export class LeftcolumnComponent implements OnInit {
249249
// Filtrera på INITIATE nedan
250250
zip(
251251
this.queryService.aggregationResult$,
252-
this.store.pipe(filter((d) => d.ui.latestAction === INITIATE)),
252+
this.store.select('query'),
253253
this.metadataService.loadedMetadata$
254254

255-
).subscribe(([result, {query: {filters}}, info] : [AggregationsResult, any, any]) => {
255+
).subscribe(([result, {filters}, info] : [AggregationsResult, any, any]) => {
256256
//this.zone.run(() => {
257257
console.log("Leftcolumn init", result, filters)
258258
this.parseAggResults(result)

src/app/minidocselection/minidocselection.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('MinidocselectionComponent', () => {
2222
};
2323
documentsServiceStub = <DocumentsService>{
2424
loadedDocument$ : new Observable(),
25+
docLoadingStatus$ : new Observable(),
2526
};
2627

2728
TestBed.configureTestingModule({

src/app/minidocselection/minidocselection.component.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Store } from '@ngrx/store';
44

55
import { QueryService } from '../query.service';
66
import { DocumentsService } from '../documents.service';
7-
import { OPENDOCUMENT, RELOAD, SEARCH, AppState } from '../searchreducer';
7+
import { OPENDOCUMENT, AppState } from '../searchreducer';
88
import { StrixDocument } from '../strixdocument.model';
9-
import { filter } from 'rxjs/operators';
9+
import { StrixEvent } from '../strix-event.enum';
1010

1111
@Component({
1212
selector: 'minidocselection',
@@ -23,22 +23,25 @@ export class MinidocselectionComponent implements OnInit, OnDestroy {
2323
constructor(private queryService: QueryService,
2424
private store: Store<AppState>,
2525
private documentsService: DocumentsService) {
26-
this.store.select('ui').pipe(filter((d) => [OPENDOCUMENT, SEARCH, RELOAD].includes(d.latestAction)))
27-
.subscribe(() => {
28-
this.documentsWithHits = [];
29-
// Hide until main document is loaded.
30-
this.isMainDocumentLoaded = false;
26+
27+
// Reset when a new document is being opened.
28+
this.documentsService.docLoadingStatus$.subscribe(event => {
29+
if (event === StrixEvent.DOCLOADSTART) {
30+
this.documentsWithHits = [];
31+
// Hide until main document is loaded.
32+
this.isMainDocumentLoaded = false;
33+
}
3134
});
3235

33-
this.subscription = documentsService.loadedDocument$.subscribe(
34-
message => {
35-
this.isMainDocumentLoaded = true;
36-
documentsService.getRelatedDocuments(message.documentIndex).subscribe(
37-
answer => {
38-
console.log("related data", answer["data"]);
39-
this.documentsWithHits = answer["data"];
40-
}
41-
);
36+
// Appear when a new document is being opened. Start fetching related documents.
37+
this.subscription = documentsService.loadedDocument$.subscribe(message => {
38+
this.isMainDocumentLoaded = true;
39+
documentsService.getRelatedDocuments(message.documentIndex).subscribe(
40+
answer => {
41+
console.log("related data", answer["data"]);
42+
this.documentsWithHits = answer["data"];
43+
}
44+
);
4245
});
4346
}
4447

src/app/query.service.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CallsService } from './calls.service';
77
import { Store } from '@ngrx/store';
88
import { CLOSEDOCUMENT, AppState } from './searchreducer';
99
import { StrixEvent } from './strix-event.enum';
10-
import { filter, switchMap } from 'rxjs/operators';
10+
import { filter, switchMap, tap } from 'rxjs/operators';
1111
import * as _ from 'lodash';
1212

1313
/**
@@ -37,10 +37,10 @@ export class QueryService {
3737
aggregationResult$ = this.aggregationResultSubject.asObservable();
3838

3939
// Components should subscribe to the searchStatus$ stream
40-
// to know the *status* of the search (for displaying such
40+
// to know the *status* of the search (for displaying such
4141
// things as progress bars):
4242
// REM: searchStatusSubject needs to be a BehaviorSubject
43-
// so that any subscribing components can get the latest
43+
// so that any subscribing components can get the latest
4444
// state directly upon subscribing.
4545
private searchStatusSubject = new BehaviorSubject<StrixEvent>(StrixEvent.INIT);
4646
searchStatus$ = this.searchStatusSubject.asObservable();
@@ -68,7 +68,7 @@ export class QueryService {
6868
//let keywordSearch = this.currentQuery.keyword_search || false;
6969
//return !keywordSearch;
7070
}
71-
71+
7272
public setSearchString(searchString: string): void {
7373
this.currentQuery.queryString = searchString;
7474
}
@@ -100,7 +100,7 @@ export class QueryService {
100100
// ... we'll see what the future brings
101101
}
102102
}
103-
103+
104104
private runAggregationQuery(query: StrixQuery) {
105105
if (query.type === QueryType.Normal) {
106106
console.log("adding an aggregation search to the stream of streams");
@@ -112,8 +112,18 @@ export class QueryService {
112112

113113
onInit() {
114114

115+
/* switchMap makes sure only the most recently added query stream is listened to.
116+
All other streams are unsubscribed and the $http request should, as a
117+
consequence be cancelled. */
118+
this.streamOfStreams.pipe(switchMap(obj => obj)).subscribe( (value: SearchResult) => {
119+
this.signalEndedSearch();
120+
this.searchResultSubject.next(value);
121+
});
122+
this.streamOfAggregationStreams.pipe(switchMap(obj => obj)).subscribe((value: AggregationsResult) => {
123+
this.aggregationResultSubject.next(value);
124+
});
125+
115126
this.store.select('query').subscribe(data => {
116-
/* React upon the action SEARCH, most likely triggering a main query search. */
117127
const previousQuery = this.currentQuery;
118128
this.currentQuery = new StrixQuery();
119129
this.currentQuery.type = <QueryType>data.type;
@@ -126,25 +136,13 @@ export class QueryService {
126136
this.currentQuery.keyword_search = data.keyword_search
127137
}
128138
if (!_.isEqual(this.currentQuery, previousQuery)) {
139+
console.log('new query');
129140
this.runCurrentQuery(); // Perform the actual search
130141
}
131142
});
132143

133-
/* Redo the last query when the user closes the open document */
134-
this.store.select('ui').pipe(filter(ui => ui.latestAction === CLOSEDOCUMENT)).subscribe(() => {
135-
if (this.currentQuery) this.runCurrentQuery(); // REM: Don't know why it's sometimes null (and only in Firefox, it seems..)
136-
});
137-
138-
/* switchMap makes sure only the most recently added query stream is listened to.
139-
All other streams are unsubscribed and the $http request should, as a
140-
consequence be cancelled. */
141-
this.streamOfStreams.pipe(switchMap(obj => obj)).subscribe( (value: SearchResult) => {
142-
this.signalEndedSearch();
143-
this.searchResultSubject.next(value);
144-
});
145-
this.streamOfAggregationStreams.pipe(switchMap(obj => obj)).subscribe((value: AggregationsResult) => {
146-
this.aggregationResultSubject.next(value);
147-
});
144+
// console.log('QueryService init run query');
145+
// this.runCurrentQuery();
148146
}
149147

150148
}

src/app/routing.service.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as _ from 'lodash';
66
import { INITIATE, RELOAD, OPENDOCUMENT_NOHISTORY, CLOSEDOCUMENT_NOHISTORY, AppState, } from './searchreducer';
77
import { QueryType } from './strixquery.model';
88

9-
/** The Routing Service is responsible for keeping the web browser URL and
9+
/** The Routing Service is responsible for keeping the web browser URL and
1010
the ngrx-store app store in sync. It is the only piece of code that is allowed
1111
to change the browser's URL, and components should only communicate with this
1212
service by dispatching to the ngrx store. */
@@ -49,14 +49,11 @@ export class RoutingService {
4949
if (urlString) {
5050
urlString = "?" + urlString;
5151
}
52-
if (state.ui.latestAction !== INITIATE) {
53-
if (state.ui.history) {
54-
console.log("PUSHING STATE")
55-
window.history.pushState("", "", urlString)
56-
} else {
57-
window.history.replaceState("", "", urlString)
58-
}
59-
52+
if (state.ui.history) {
53+
console.log("PUSHING STATE")
54+
window.history.pushState("", "", urlString)
55+
} else {
56+
window.history.replaceState("", "", urlString)
6057
}
6158
});
6259

@@ -130,7 +127,7 @@ export class RoutingService {
130127
this.store.dispatch({
131128
type : OPENDOCUMENT_NOHISTORY,
132129
payload : {
133-
doc_id : startState["documentID"],
130+
doc_id : startState["documentID"],
134131
sentence_id : startState["sentenceID"],
135132
corpus_id : startState["documentCorpus"]
136133
}

src/app/searchreducer.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ export interface DocumentState {
4949

5050
export interface UiState {
5151
lang?: string;
52-
latestAction?: string;
5352
history?: boolean;
5453
}
5554

5655
export function queryStateReducer(state: QueryState = {}, action: Action): QueryState {
56+
console.log('Action', action.type, action.payload);
5757
let nextState: QueryState = {...state};
5858
switch (action.type) {
5959
case INITIATE:
60-
console.log("INITIATE.");
6160
nextState = _.assign({}, nextState, action.payload.query);
6261
break;
6362
case CHANGEQUERY:
@@ -87,7 +86,6 @@ export function documentStateReducer(state: DocumentState, action: Action): Docu
8786
let nextState: DocumentState = {...state};
8887
switch (action.type) {
8988
case INITIATE:
90-
console.log("INITIATE.");
9189
nextState = _.assign({}, nextState, action.payload.document);
9290
break;
9391
case OPENDOCUMENT:
@@ -111,12 +109,10 @@ export function documentStateReducer(state: DocumentState, action: Action): Docu
111109
export function uiStateReducer(state: UiState = {}, action: Action): UiState {
112110
let nextState: UiState = {
113111
...state,
114-
latestAction : action.type,
115112
history : true,
116113
};
117114
switch (action.type) {
118115
case INITIATE:
119-
console.log("INITIATE.");
120116
nextState = _.assign({}, nextState, action.payload.ui);
121117
nextState.history = false;
122118
break;
@@ -125,19 +121,8 @@ export function uiStateReducer(state: UiState = {}, action: Action): UiState {
125121
break;
126122
case OPENDOCUMENT_NOHISTORY:
127123
case CLOSEDOCUMENT_NOHISTORY:
128-
nextState.history = false;
129-
nextState.latestAction = CLOSEDOCUMENT;
130-
break;
131-
case SEARCHINDOCUMENT:
132-
nextState.latestAction = "OPENDOCUMENT";
133-
break;
134124
case SEARCH:
135-
nextState.history = false;
136-
break;
137125
case RELOAD:
138-
// Like search but without changing the state.
139-
// Used for the first load of the page.
140-
nextState.latestAction = "SEARCH";
141126
nextState.history = false;
142127
break;
143128
}

0 commit comments

Comments
 (0)