1 import { ChartData } from 'chart.js'
2 import { max, maxBy, min, minBy } from 'lodash-es'
3 import { flatMap, debounceTime } from 'rxjs/operators'
4 import { Component, OnInit } from '@angular/core'
5 import { AuthService, ConfirmService, Notifier, ScreenService, User } from '@app/core'
6 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { Subject } from 'rxjs'
11 selector: 'my-account-video-channels',
12 templateUrl: './my-account-video-channels.component.html',
13 styleUrls: [ './my-account-video-channels.component.scss' ]
15 export class MyAccountVideoChannelsComponent implements OnInit {
18 videoChannels: VideoChannel[] = []
19 videoChannelsChartData: ChartData[]
20 videoChannelsMinimumDailyViews = 0
21 videoChannelsMaximumDailyViews: number
23 channelsSearch: string
24 channelsSearchChanged = new Subject<string>()
29 private authService: AuthService,
30 private notifier: Notifier,
31 private confirmService: ConfirmService,
32 private videoChannelService: VideoChannelService,
33 private screenService: ScreenService,
38 this.user = this.authService.getUser()
40 this.loadVideoChannels()
42 this.channelsSearchChanged
43 .pipe(debounceTime(500))
45 this.loadVideoChannels()
49 get isInSmallView () {
50 return this.screenService.isInSmallView()
65 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
66 max: Math.max(1, this.videoChannelsMaximumDailyViews)
86 custom: function (tooltip: any) {
88 // disable displaying the color box
89 tooltip.displayColors = false
92 label: (tooltip: any, data: any) => `${tooltip.value} views`
103 this.channelsSearch = ''
104 this.onChannelsSearchChanged()
107 onChannelsSearchChanged () {
108 this.channelsSearchChanged.next()
111 async deleteVideoChannel (videoChannel: VideoChannel) {
112 const res = await this.confirmService.confirmWithInput(
115 'Do you really want to delete {{channelDisplayName}}? It will delete {{videosCount}} videos uploaded in this channel, and you will not be able to create another channel with the same name ({{channelName}})!',
116 { channelDisplayName: videoChannel.displayName, videosCount: videoChannel.videosCount, channelName: videoChannel.name }
119 'Please type the display name of the video channel ({{displayName}}) to confirm',
120 { displayName: videoChannel.displayName }
122 videoChannel.displayName,
125 if (res === false) return
127 this.videoChannelService.removeVideoChannel(videoChannel)
130 this.loadVideoChannels()
131 this.notifier.success(
132 this.i18n('Video channel {{videoChannelName}} deleted.', { videoChannelName: videoChannel.displayName })
136 error => this.notifier.error(error.message)
140 private loadVideoChannels () {
141 this.authService.userInformationLoaded
142 .pipe(flatMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true, this.channelsSearch)))
144 this.videoChannels = res.data
145 this.totalItems = res.total
148 this.videoChannelsChartData = this.videoChannels.map(v => ({
149 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
152 label: this.i18n('Views for the day'),
153 data: v.viewsPerDay.map(day => day.views),
155 borderColor: "#c6c6c6"
160 // chart options that depend on chart data:
161 // we don't want to skew values and have min at 0, so we define what the floor/ceiling is here
162 this.videoChannelsMinimumDailyViews = min(
163 this.videoChannels.map(v => minBy( // compute local minimum daily views for each channel, by their "views" attribute
166 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
168 this.videoChannelsMaximumDailyViews = max(
169 this.videoChannels.map(v => maxBy( // compute local maximum daily views for each channel, by their "views" attribute
172 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute