aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-library/my-history/my-history.component.ts
blob: c4878c957fffbb9ffb9daaadf527c4fa9fcf2756 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { tap } from 'rxjs/operators'
import { Component, OnInit, ViewChild } from '@angular/core'
import { AuthService, ComponentPagination, ConfirmService, DisableForReuseHook, Notifier, User, UserService } from '@app/core'
import { immutableAssign } from '@app/helpers'
import { UserHistoryService, Video } from '@app/shared/shared-main'
import { MiniatureDisplayOptions, VideosSelectionComponent } from '@app/shared/shared-video-miniature'

@Component({
  templateUrl: './my-history.component.html',
  styleUrls: [ './my-history.component.scss' ]
})
export class MyHistoryComponent implements OnInit, DisableForReuseHook {
  @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent

  titlePage: string
  pagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 5,
    totalItems: null
  }

  videosHistoryEnabled: boolean

  miniatureDisplayOptions: MiniatureDisplayOptions = {
    date: true,
    views: true,
    by: true,
    privacyLabel: false,
    privacyText: true,
    state: true,
    blacklistInfo: true
  }

  getVideosObservableFunction = this.getVideosObservable.bind(this)

  user: User

  videos: Video[] = []
  search: string

  disabled = false

  constructor (
    private authService: AuthService,
    private userService: UserService,
    private notifier: Notifier,
    private confirmService: ConfirmService,
    private userHistoryService: UserHistoryService
  ) {
    this.titlePage = $localize`My watch history`
  }

  ngOnInit () {
    this.user = this.authService.getUser()

    this.authService.userInformationLoaded
      .subscribe(() => this.videosHistoryEnabled = this.user.videosHistoryEnabled)
  }

  disableForReuse () {
    this.disabled = true
  }

  enabledForReuse () {
    this.disabled = false
  }

  reloadData () {
    this.videosSelection.reloadVideos()
  }

  onSearch (search: string) {
    this.search = search
    this.reloadData()
  }

  getVideosObservable (page: number) {
    const newPagination = immutableAssign(this.pagination, { currentPage: page })

    return this.userHistoryService.list(newPagination, this.search)
      .pipe(
        tap(res => this.pagination.totalItems = res.total)
      )
  }

  generateSyndicationList () {
    /* method disabled */
    throw new Error('Method not implemented.')
  }

  onVideosHistoryChange () {
    this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled })
      .subscribe({
        next: () => {
          const message = this.videosHistoryEnabled === true
            ? $localize`Video history is enabled`
            : $localize`Video history is disabled`

          this.notifier.success(message)

          this.authService.refreshUserInformation()
        },

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

  deleteHistoryElement (video: Video) {
    this.userHistoryService.deleteElement(video)
      .subscribe({
        next: () => {
          this.videos = this.videos.filter(v => v.id !== video.id)
        },

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

  async clearAllHistory () {
    const title = $localize`Delete video history`
    const message = $localize`Are you sure you want to delete all your video history?`

    const res = await this.confirmService.confirm(message, title)
    if (res !== true) return

    this.userHistoryService.clearAll()
        .subscribe({
          next: () => {
            this.notifier.success($localize`Video history deleted`)

            this.reloadData()
          },

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

  getNoResultMessage () {
    if (this.search) {
      return $localize`No videos found for "${this.search}".`
    }

    return $localize`You don't have any video in your watch history yet.`
  }
}