import { Component, OnInit } from '@angular/core'; import { HttpResponse } from '@angular/common/http'; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { FormBuilder, Validators } from '@angular/forms'; import { ActivatedRoute } from '@angular/router'; import { Observable } from 'rxjs'; import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager'; import { faQuestion } from '@fortawesome/free-solid-svg-icons'; import { IUserWatchList, UserWatchList } from 'app/shared/model/user-watch-list.model'; import { CheckFrequency } from 'app/shared/model/enumerations/check-frequency.model'; import { IUser } from 'app/core/user/user.model'; import { UserService } from 'app/core/user/user.service'; @Component({ selector: 'jhi-user-watch-list-update', templateUrl: './bookmarks-update.component.html', }) export class UserWatchListUpdateComponent implements OnInit { isSaving = false; users: IUser[] = []; questionIcon = faQuestion; editForm = this.fb.group({ id: [], name: [null, [Validators.required, Validators.minLength(1)]], userId: [null], checkFrequency: CheckFrequency.NEVER }); constructor( protected watchlistManager: WatchlistManager, protected userService: UserService, protected activatedRoute: ActivatedRoute, private fb: FormBuilder ) {} ngOnInit(): void { this.activatedRoute.data.subscribe(({ userWatchList }) => { this.updateForm(userWatchList); this.userService.query().subscribe((res: HttpResponse<IUser[]>) => (this.users = res.body || [])); }); } updateForm(userWatchList: IUserWatchList): void { this.editForm.patchValue({ id: userWatchList.id, name: userWatchList.name, userId: userWatchList.userId, checkFrequency: userWatchList.checkFrequency, }); } previousState(): void { window.history.back(); } save(): void { this.isSaving = true; const userWatchList = this.createFromForm(); if (userWatchList.id !== undefined) { this.subscribeToSaveResponse(this.watchlistManager.update(userWatchList)); } else { this.subscribeToSaveResponse(this.watchlistManager.createForCurrentUser(userWatchList)); } } private createFromForm(): IUserWatchList { return { ...new UserWatchList(), id: this.editForm.get(['id'])!.value, name: this.editForm.get(['name'])!.value, userId: this.editForm.get(['userId'])!.value, checkFrequency: this.editForm.get(['checkFrequency'])!.value, }; } protected subscribeToSaveResponse(result: Observable<HttpResponse<IUserWatchList>>): void { result.subscribe( () => this.onSaveSuccess(), () => this.onSaveError() ); } protected onSaveSuccess(): void { this.isSaving = false; this.previousState(); } protected onSaveError(): void { this.isSaving = false; } trackById(index: number, item: IUser): any { return item.id; } }