aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/videos/components/list/videos-list.component.ts
blob: a17b06cd97a91e449fb601e9f7fb17957753872e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import { Component, OnInit } from '@angular/core';
import { ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated';

import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';

import { AuthService } from '../../../users/services/auth.service';
import { Pagination } from '../../pagination';
import { User } from '../../../users/models/user';
import { VideosService } from '../../videos.service';
import { Video } from '../../video';
import { VideoMiniatureComponent } from './video-miniature.component';
import { Search, SearchField } from '../../../app/search';

@Component({
  selector: 'my-videos-list',
  styleUrls: [ 'app/angular/videos/components/list/videos-list.component.css' ],
  templateUrl: 'app/angular/videos/components/list/videos-list.component.html',
  directives: [ ROUTER_DIRECTIVES, PAGINATION_DIRECTIVES, VideoMiniatureComponent ]
})

export class VideosListComponent implements OnInit {
  user: User = null;
  videos: Video[] = [];
  pagination: Pagination = {
    currentPage: 1,
    itemsPerPage: 9,
    total: 0
  }

  private search: Search;

  constructor(
    private _authService: AuthService,
    private _videosService: VideosService,
    private _routeParams: RouteParams
  ) {
    this.search = {
      value: this._routeParams.get('search'),
      field: <SearchField>this._routeParams.get('field')
    }
  }

  ngOnInit() {
    if (this._authService.isLoggedIn()) {
      this.user = User.load();
    }

    this.getVideos();
  }

  getVideos() {
    let observable = null;

    if (this.search.value !== null) {
      observable = this._videosService.searchVideos(this.search, this.pagination);
    } else {
      observable = this._videosService.getVideos(this.pagination);
    }

    observable.subscribe(
      ({ videos, totalVideos }) => {
        this.videos = videos;
        this.pagination.total = totalVideos;
      },
      error => alert(error)
    );
  }

  onRemoved(video: Video): void {
    this.videos.splice(this.videos.indexOf(video), 1);
  }

}