diff --git a/src/main/webapp/app/app.module.ts b/src/main/webapp/app/app.module.ts index f82548f29847e655fbc97ca611a1cd79bf714c7a..4f0df201b866a39d2628b666a9259f26928a8be7 100644 --- a/src/main/webapp/app/app.module.ts +++ b/src/main/webapp/app/app.module.ts @@ -17,6 +17,7 @@ import { ActiveMenuDirective } from './layouts/navbar/active-menu.directive'; import { ErrorComponent } from './layouts/error/error.component'; import { QueryParamModule } from '@ngqp/core'; import { CacheService } from 'app/shared/service/cache.service'; +import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'; @NgModule({ imports: [ @@ -29,6 +30,7 @@ import { CacheService } from 'app/shared/service/cache.service'; GitSearchV2EntityModule, GitSearchV2AppRoutingModule, QueryParamModule, + NgbDropdown, ], declarations: [MainComponent, NavbarComponent, ErrorComponent, PageRibbonComponent, ActiveMenuDirective, FooterComponent], bootstrap: [MainComponent], diff --git a/src/main/webapp/app/shared/watchlist/watchlist-manager.ts b/src/main/webapp/app/shared/watchlist/watchlist-manager.ts new file mode 100644 index 0000000000000000000000000000000000000000..55c00486fc398060d653f0996e77df9b9c86798f --- /dev/null +++ b/src/main/webapp/app/shared/watchlist/watchlist-manager.ts @@ -0,0 +1,58 @@ +import { Injectable, OnDestroy, OnInit } from '@angular/core'; +import { HttpResponse } from '@angular/common/http'; +import { UserWatchListService } from 'app/entities/user-watch-list/user-watch-list.service'; +import { IUserWatchList } from 'app/shared/model/user-watch-list.model'; +import { AccountService } from 'app/core/auth/account.service'; +import { Subscription } from 'rxjs'; +import { Account } from 'app/core/user/account.model'; + + +/** + provides infrastructure for watchlists management. + The manager is is charge to guarantee that the local watchlists and the watchlists on the server are in synchrony. + If the user is not logged in, the watchlists are undefined. + */ +@Injectable({ providedIn: 'root' }) +export class WatchlistManager implements OnInit, OnDestroy { + + account: Account | null = null; + authSubscription?: Subscription; + + myWatchLists?: IUserWatchList[]; + currentWatchlist? = ''; + constructor( + protected watchListService: UserWatchListService, + private accountService: AccountService, + + ) { + this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => { + this.account = account; + if(this.accountService.isAuthenticated()) { + this.watchListService.findMyWatchlists().subscribe((data: HttpResponse<IUserWatchList[]>) => { + if (data.body) { + this.myWatchLists = data.body; + if(this.myWatchLists.length > 0) { + this.currentWatchlist = this.myWatchLists[0].name + } + } + + }) + + } + else { this.myWatchLists = undefined; } + }); + } + + public getMyWatchLists(): IUserWatchList[]| undefined { + return this.myWatchLists; + } + + ngOnInit(): void { + } + + ngOnDestroy(): void { + if (this.authSubscription) { + this.authSubscription.unsubscribe(); + } + } +} diff --git a/src/main/webapp/app/watchlist/watchlist.component.html b/src/main/webapp/app/watchlist/watchlist.component.html index d0e04b1f7eac4dd400f34a1152dd035ce54a94a2..57f638551c5f5bed135759c50cea67099286a709 100644 --- a/src/main/webapp/app/watchlist/watchlist.component.html +++ b/src/main/webapp/app/watchlist/watchlist.component.html @@ -1,6 +1,8 @@ <div > - <div *ngFor="let watchList of getMyWatchLists()">{{watchList.name}} - </div> + <button class="btn btn-outline-primary mr-2" id="dropdownManual" ngbDropdownAnchor>Toggle dropdown</button> + <div ngbDropdownMenu aria-labelledby="dropdownManual" > + <button ngbDropdownItem *ngFor="let watchList of getMyWatchLists()">{{watchList.name}}</button> + </div> </div> diff --git a/src/main/webapp/app/watchlist/watchlist.component.ts b/src/main/webapp/app/watchlist/watchlist.component.ts index 7b458e4c61fa92ce80109bc1ff2a8f2b789adb61..098f44a8f9dac65a84c9fc51cd47764b02c63553 100644 --- a/src/main/webapp/app/watchlist/watchlist.component.ts +++ b/src/main/webapp/app/watchlist/watchlist.component.ts @@ -1,10 +1,6 @@ -import { Component, OnDestroy, OnInit } from '@angular/core'; -import { HttpResponse } from '@angular/common/http'; -import { UserWatchListService } from '../entities/user-watch-list/user-watch-list.service'; +import { Component } from '@angular/core'; +import {WatchlistManager} from 'app/shared/watchlist/watchlist-manager'; import { IUserWatchList } from 'app/shared/model/user-watch-list.model'; -import { AccountService } from 'app/core/auth/account.service'; -import { Subscription } from 'rxjs'; -import { Account } from 'app/core/user/account.model'; @Component({ @@ -12,53 +8,19 @@ import { Account } from 'app/core/user/account.model'; templateUrl: './watchlist.component.html', styleUrls: ['./watchlist.component.scss'], }) -export class WatchlistComponent implements OnInit, OnDestroy { - - account: Account | null = null; - authSubscription?: Subscription; +export class WatchlistComponent { myWatchLists: IUserWatchList[] | undefined; constructor( - protected watchListService: UserWatchListService, - private accountService: AccountService, + private watchlistManager: WatchlistManager, ) { } - getMyWatchLists(): IUserWatchList[] { - if(!this.isAuthenticated()) return []; - if (!this.myWatchLists) { - this.watchListService.findMyWatchlists().subscribe((data: HttpResponse<IUserWatchList[]>) => { - if (data.body) { - this.myWatchLists = data.body; - /* - this.myWatchLists = []; - for (let i = 0; i < data.body.length; i++) { - this.myWatchLists.push(data.body[i]); - } - */ - } - - }) - return[]; - } else { - return this.myWatchLists; - } - - } + getMyWatchLists(): IUserWatchList[]|undefined { + return this.watchlistManager.getMyWatchLists(); - ngOnInit(): void { - this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account)); } - isAuthenticated(): boolean { - return this.accountService.isAuthenticated(); - } - - ngOnDestroy(): void { - if (this.authSubscription) { - this.authSubscription.unsubscribe(); - } - } }