Newer
Older
import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpHeaders, HttpResponse } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
import { JhiEventManager, JhiParseLinks } from 'ng-jhipster';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { IUserWatchList } from 'app/shared/model/user-watch-list.model';
import { Exercise, searchResultToExercise } from 'app/shared/model/exercise.model';
import { ITEMS_PER_PAGE } from 'app/shared/constants/pagination.constants';
import { UserWatchListService } from 'app/entities/user-watch-list/user-watch-list.service';
import { BookmarksDeleteDialogComponent } from './bookmarks-delete-dialog.component';
import { SearchResultsDTO } from 'app/shared/model/search/search-results-dto.model';
import { SearchResultDTO, PluginActionInfo, equalPluginActionInfo } from 'app/shared/model/search/search-result-dto.model';
import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager';
import { ShoppingBasketInfo, ShoppingBasketRedirectInfoDTO } from 'app/shared/model/basket/shopping-basket-info.model';
import { PluginService } from 'app/shared/service/plugin-service';
import * as lodash from 'lodash';
selector: 'jhi-bookmarks',
styleUrls: ['./bookmarks.component.scss'],
templateUrl: './bookmarks.component.html',
})
export class BookmarkComponent implements OnInit, OnDestroy {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
userWatchLists: IUserWatchList[];
selectedWatchList?: IUserWatchList;
eventSubscriber?: Subscription;
itemsPerPage: number;
links: any;
page: number;
allLoaded = false;
predicate: string;
ascending: boolean;
currentSearch: string;
results: Exercise[] = [];
selectedResult?: Exercise;
hitCount = 0;
constructor(
protected userWatchListService: UserWatchListService,
protected eventManager: JhiEventManager,
protected modalService: NgbModal,
protected parseLinks: JhiParseLinks,
protected activatedRoute: ActivatedRoute,
protected watchlistManager: WatchlistManager,
protected pluginService: PluginService
) {
this.userWatchLists = [];
this.itemsPerPage = ITEMS_PER_PAGE;
this.page = 0;
this.allLoaded = false;
this.links = {
last: 0,
};
this.predicate = 'id';
this.ascending = true;
this.currentSearch =
this.activatedRoute.snapshot && this.activatedRoute.snapshot.queryParams['search']
? this.activatedRoute.snapshot.queryParams['search']
: '';
}
loadAll(): void {
if (this.currentSearch) {
this.userWatchListService
.searchForCurrentUser({
query: this.currentSearch,
page: this.page,
size: this.itemsPerPage,
sort: this.sort(),
})
.subscribe((res: HttpResponse<IUserWatchList[]>) => {
this.paginateUserWatchLists(res.body, res.headers);
this.userWatchListService
.findMyWatchlists({
page: this.page,
size: this.itemsPerPage,
sort: this.sort(),
})
.subscribe((res: HttpResponse<IUserWatchList[]>) => {
this.paginateUserWatchLists(res.body, res.headers);
if (!this.loadCurrent()) {
// should reset also current watchlist
});
}
private loadCurrent(): boolean {
let found = false;
if (this.watchlistManager.getCurrentWatchList()) {
this.userWatchLists.every(uwl => {
if (this.isSelected(uwl)) {
this.view(uwl);
found = true;
return false;
return true;
});
} else {
// currently no default yet selected
if (this.userWatchLists && this.userWatchLists.length > 0) {
this.watchlistManager.setCurrentWatchList(this.userWatchLists[0].name!);
this.view(this.userWatchLists[0]);
found = true;
return false;
}
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
return found;
}
reset(): void {
this.page = 0;
this.allLoaded = false;
this.userWatchLists = [];
this.loadAll();
}
loadPage(page: number): void {
this.page = page;
this.loadAll();
}
selectExercise(exercise: Exercise): void {
this.selectedResult = exercise;
}
search(query: string): void {
this.userWatchLists = [];
this.links = {
last: 0,
};
this.page = 0;
if (query) {
this.predicate = '_score';
this.ascending = false;
} else {
this.predicate = 'id';
this.ascending = true;
this.currentSearch = query;
this.loadAll();
}
ngOnInit(): void {
this.loadAll();
this.registerChangeInUserWatchLists();
}
ngOnDestroy(): void {
if (this.eventSubscriber) {
this.eventManager.destroy(this.eventSubscriber);
}
trackId(index: number, item: IUserWatchList): number {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return item.id!;
}
registerChangeInUserWatchLists(): void {
this.eventSubscriber = this.eventManager.subscribe('userWatchListListModification', () => this.reset());
}
private parseSearchResultsDTO(searchResultsDTO: SearchResultsDTO): Exercise[] {
this.hitCount = searchResultsDTO.hitCount;
// fix string to date conversion: unfortunatelly dates are not converted correctly :-()
searchResultsDTO.searchResult.forEach(hit => {
// eslint-disable-next-line @typescript-eslint/camelcase
hit.project.last_activity_at = new Date(hit.project.last_activity_at);
// eslint-disable-next-line @typescript-eslint/camelcase
hit.file.indexing_date = new Date(hit.file.indexing_date);
});
return searchResultsDTO.searchResult.map(searchResultToExercise);
}
delete(userWatchList: IUserWatchList): void {
const modalRef = this.modalService.open(BookmarksDeleteDialogComponent, { size: 'lg', backdrop: 'static' });
modalRef.componentInstance.userWatchList = userWatchList;
}
sort(): string[] {
const result = [this.predicate + ',' + (this.ascending ? 'asc' : 'desc')];
if (this.predicate !== 'id') {
result.push('id');
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
return result;
}
view(userWatchList: IUserWatchList): void {
this.hitCount = 0;
this.page = 0;
this.allLoaded = false;
this.results = [];
this.selectedWatchList = userWatchList;
this.watchlistManager.setCurrentWatchList(userWatchList.name!);
this.onScroll();
}
onScroll(): void {
if (this.selectedWatchList) {
if (!this.allLoaded) {
this.userWatchListService.findExercises(this.selectedWatchList, this.page).subscribe(
(data: SearchResultsDTO) => {
const searchResults = this.parseSearchResultsDTO(data);
if (data.pageStartIndex + searchResults.length < this.results.length) {
// we are replacing already existing results inside the array
for (let i = 0; i < searchResults.length; i++) {
this.results[data.pageStartIndex + i] = searchResults[i];
}
} else {
const diff = data.pageStartIndex - this.results.length;
if (diff > 0) {
// overlap with current results, throw away current result
this.results = this.results.slice(0, data.pageStartIndex - 1);
} else if (diff < 0) {
alert('reload of exercises failed!');
// fill up results
for (let i = 0; i < -diff; i++) {
// fill up array with empty content
}
}
this.results = this.results.concat(searchResults);
if (data.hitCount <= this.results.length) {
this.allLoaded = true;
}
}
++this.page; // TODO this may result in a race condition, if requests are not returned in sequence :-()
},
() => alert('Search failed')
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
}
isSelected(userWatchList: IUserWatchList) {
return (
this.watchlistManager.getCurrentWatchList() && userWatchList.id === this.watchlistManager.getCurrentWatchList()!.userWatchList.id
);
}
protected paginateUserWatchLists(data: IUserWatchList[] | null, headers: HttpHeaders): void {
const headersLink = headers.get('link');
this.links = this.parseLinks.parse(headersLink ? headersLink : '');
if (data) {
for (let i = 0; i < data.length; i++) {
this.userWatchLists.push(data[i]);
}
}
}
getCommonActions(): PluginActionInfo[] {
const result = lodash
.chain(this.results)
.map(function (ex: Exercise) {
return ex.originalResult.supportedActions;
})
.flatten()
.uniqWith(equalPluginActionInfo);
return result.value();
}
startAction(action: PluginActionInfo): void {
const selectedExercises = lodash
.chain(this.results)
.map(function (ex: Exercise) {
return ex.originalResult;
})
.filter(function (sr: SearchResultDTO) {
return sr.supportedActions.some(function (a: PluginActionInfo) {
return equalPluginActionInfo(a, action);
});
});
const basketInfo: ShoppingBasketInfo = {
plugin: action.plugin,
action: action.action,
itemInfos: selectedExercises.value(),
};
this.pluginService.getRedirectLink(basketInfo).subscribe(
(redirectInfo: ShoppingBasketRedirectInfoDTO) => {
// alert('redirecting to ' + redirectInfo.redirectURL);
// location.href = redirectInfo.redirectURL;
window.open(redirectInfo.redirectURL, action.action);
},
() => alert('Search failed')
);
}