allfree-angular-frontend / src / app / board-admin / board-admin.component.ts
board-admin.component.ts
Raw
import {APIResponse, Article, User, UserLogged} from './../models/models';
import {Subscription} from 'rxjs';
import {Component, OnInit} from '@angular/core';
import {UserService} from '../_services/user-service/user.service';
import {ArticlesService} from '../_services/article-service/articles.service';
import {Router} from '@angular/router';
import {TokenStorageService} from '../_services/jwt-service/token-storage.service';

@Component({
  selector: 'app-board-admin',
  templateUrl: './board-admin.component.html',
  styleUrls: ['./board-admin.component.scss'],
})
export class BoardAdminComponent implements OnInit {
  users: any;
  userSub: Subscription;
  error: any;
  showUser: User;
  isSelected: boolean = false;
  deletedUser: User;
  pendingArticles: Array<Article>;
  declinedArticles: Array<Article>;
  loggedUser: UserLogged;

  constructor(
    private userService: UserService,
    private articleService: ArticlesService,
    private router: Router,
    private tokenService: TokenStorageService
  ) {
  }

  ngOnInit(): void {
    this.loggedUser = this.tokenService.getUser().userDetails;
    this.getPendingArticles();
    this.getDeclinedArticles();
  }

  isAdmin(): boolean {
    return this.loggedUser.roles.map(role => role.name).includes('ROLE_ADMIN');
  }

  getPendingArticles() {
    this.articleService.getArticlesByStatus('PENDING').subscribe(
      (data) => {
        this.pendingArticles = data;
        console.log(this.pendingArticles);
      },
      (error) => console.log(error)
    );
  }

  approveArticle(articleId: number): void {
    this.articleService.updateArticleStatus(articleId, 'ACCEPTED').subscribe(
      (data) => {
        console.log(data);
      },
      (error) => console.log(error)
    );
    this.reloadPage();
    return;
  }

  declineArticle(articleId: number): void {
    this.articleService.updateArticleStatus(articleId, 'DECLINED').subscribe(
      (data) => {
        console.log(data);
      },
      (error) => console.log(error)
    );
    this.reloadPage();
    return;
  }

  getDeclinedArticles() {
    this.articleService.getArticlesByStatus('DECLINED').subscribe(
      (data) => {
        this.declinedArticles = data;
        console.log('declined', this.declinedArticles);
      },
      (error) => console.log(error)
    );
  }

  pendArticle(articleId: number): void {
    this.articleService.updateArticleStatus(articleId, 'PENDING').subscribe(
      (data) => {
        console.log(data);
      },
      (error) => console.log(error)
    );
    this.reloadPage();
    return;
  }

  reloadPage(): void {
    window.location.reload();
  }

  acceptAll() {
    this.articleService.acceptAll().subscribe(
      (value) => {
        console.log(value);
      },
      (error) => console.log(error)
    );
  }

  declineAll() {
    this.articleService.declineAll().subscribe(
      (value) => {
        console.log(value);
      },
      (error) => console.log(error)
    );
  }

  getArticleDetails(articleId: number) {
    this.router.navigate(['article-details/', articleId]);
  }
}