aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-history/my-account-history.component.ts
blob: 6ec4fefe8fe42f720cf8804b6c5609610d4852fc (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
import { Component, OnDestroy, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { Location } from '@angular/common'
import { immutableAssign } from '@app/shared/misc/utils'
import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
import { NotificationsService } from 'angular2-notifications'
import { AuthService } from '../../core/auth'
import { ConfirmService } from '../../core/confirm'
import { AbstractVideoList } from '../../shared/video/abstract-video-list'
import { VideoService } from '../../shared/video/video.service'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { ScreenService } from '@app/shared/misc/screen.service'
import { UserHistoryService } from '@app/shared/users/user-history.service'
import { UserService } from '@app/shared'

@Component({
  selector: 'my-account-history',
  templateUrl: './my-account-history.component.html',
  styleUrls: [ './my-account-history.component.scss' ]
})
export class MyAccountHistoryComponent extends AbstractVideoList implements OnInit, OnDestroy {
  titlePage: string
  currentRoute = '/my-account/history/videos'
  pagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 5,
    totalItems: null
  }
  videosHistoryEnabled: boolean

  protected baseVideoWidth = -1
  protected baseVideoHeight = 155

  constructor (
    protected router: Router,
    protected route: ActivatedRoute,
    protected authService: AuthService,
    protected userService: UserService,
    protected notificationsService: NotificationsService,
    protected location: Location,
    protected screenService: ScreenService,
    protected i18n: I18n,
    private confirmService: ConfirmService,
    private videoService: VideoService,
    private userHistoryService: UserHistoryService
  ) {
    super()

    this.titlePage = this.i18n('My videos history')
  }

  ngOnInit () {
    super.ngOnInit()

    this.videosHistoryEnabled = this.authService.getUser().videosHistoryEnabled
  }

  ngOnDestroy () {
    super.ngOnDestroy()
  }

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

    return this.userHistoryService.getUserVideosHistory(newPagination)
  }

  generateSyndicationList () {
    throw new Error('Method not implemented.')
  }

  onVideosHistoryChange () {
    this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled })
      .subscribe(
        () => {
          const message = this.videosHistoryEnabled === true ?
            this.i18n('Videos history is enabled') :
            this.i18n('Videos history is disabled')

          this.notificationsService.success(this.i18n('Success'), message)

          this.authService.refreshUserInformation()
        },

        err => this.notificationsService.error(this.i18n('Error'), err.message)
      )
  }

  async deleteHistory () {
    const title = this.i18n('Delete videos history')
    const message = this.i18n('Are you sure you want to delete all your videos history?')

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

    this.userHistoryService.deleteUserVideosHistory()
        .subscribe(
          () => {
            this.notificationsService.success(this.i18n('Success'), this.i18n('Videos history deleted'))

            this.reloadVideos()
          },

          err => this.notificationsService.error(this.i18n('Error'), err.message)
        )
  }
}