aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+about/about-follows/about-follows.component.ts
blob: f0e1375d66e4d5d34256b3c8d63cf4a1a08f613d (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Component, OnInit } from '@angular/core'
import { FollowService } from '@app/shared/instance/follow.service'
import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
import { Notifier } from '@app/core'
import { RestService } from '@app/shared'
import { SortMeta } from 'primeng/api'

@Component({
  selector: 'my-about-follows',
  templateUrl: './about-follows.component.html',
  styleUrls: [ './about-follows.component.scss' ]
})

export class AboutFollowsComponent implements OnInit {
  followers: string[] = []
  followings: string[] = []

  followersPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 40,
    totalItems: null
  }

  followingsPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 40,
    totalItems: null
  }

  sort: SortMeta = {
    field: 'createdAt',
    order: -1
  }

  constructor (
    private restService: RestService,
    private notifier: Notifier,
    private followService: FollowService
  ) { }

  ngOnInit () {
    this.loadMoreFollowers()

    this.loadMoreFollowings()
  }

  onNearOfBottom () {
    this.onNearOfFollowersBottom()

    this.onNearOfFollowingsBottom()
  }

  onNearOfFollowersBottom () {
    if (!hasMoreItems(this.followersPagination)) return

    this.followersPagination.currentPage += 1
    this.loadMoreFollowers()
  }

  onNearOfFollowingsBottom () {
    if (!hasMoreItems(this.followingsPagination)) return

    this.followingsPagination.currentPage += 1
    this.loadMoreFollowings()
  }

  buildLink (host: string) {
    return window.location.protocol + '//' + host
  }

  private loadMoreFollowers () {
    const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)

    this.followService.getFollowers(pagination, this.sort)
        .subscribe(
          resultList => {
            const newFollowers = resultList.data.map(r => r.follower.host)
            this.followers = this.followers.concat(newFollowers)

            this.followersPagination.totalItems = resultList.total
          },

          err => this.notifier.error(err.message)
        )
  }

  private loadMoreFollowings () {
    const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)

    this.followService.getFollowing(pagination, this.sort)
        .subscribe(
          resultList => {
            const newFollowings = resultList.data.map(r => r.following.host)
            this.followings = this.followings.concat(newFollowings)

            this.followingsPagination.totalItems = resultList.total
          },

          err => this.notifier.error(err.message)
        )
  }

}