This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
bookmarks-update.component.ts 2.89 KiB
Newer Older
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';
Michael Breu's avatar
Michael Breu committed
import { WatchlistManager } from 'app/shared/watchlist/watchlist-manager';
Michael Breu's avatar
Michael Breu committed
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[] = [];
Michael Breu's avatar
Michael Breu committed
  questionIcon = faQuestion;

  editForm = this.fb.group({
    id: [],
    name: [null, [Validators.required, Validators.minLength(1)]],
    checkFrequency: CheckFrequency.NEVER
Michael Breu's avatar
Michael Breu committed
    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) {
Michael Breu's avatar
Michael Breu committed
      this.subscribeToSaveResponse(this.watchlistManager.update(userWatchList));
Michael Breu's avatar
Michael Breu committed
      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;
  }
}