]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-video-channels/my-account-video-channels.component.ts
Put channel stats behind withStats flag
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-channels / my-account-video-channels.component.ts
CommitLineData
08c1efbe 1import { Component, OnInit } from '@angular/core'
f8b2c1b4 2import { Notifier } from '@app/core'
08c1efbe
C
3import { AuthService } from '../../core/auth'
4import { ConfirmService } from '../../core/confirm'
5import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
8165d00a 7import { ScreenService } from '@app/shared/misc/screen.service'
08c1efbe 8import { User } from '@app/shared'
db400f44 9import { flatMap } from 'rxjs/operators'
b1d40cff 10import { I18n } from '@ngx-translate/i18n-polyfill'
8165d00a 11import { minBy, maxBy } from 'lodash-es'
08c1efbe
C
12
13@Component({
14 selector: 'my-account-video-channels',
15 templateUrl: './my-account-video-channels.component.html',
16 styleUrls: [ './my-account-video-channels.component.scss' ]
17})
6a478b11 18export class MyAccountVideoChannelsComponent implements OnInit {
08c1efbe 19 videoChannels: VideoChannel[] = []
8165d00a
RK
20 videoChannelsData: any[]
21 videoChannelsMinimumDailyViews = 0
22 videoChannelsMaximumDailyViews: number
08c1efbe
C
23
24 private user: User
25
26 constructor (
27 private authService: AuthService,
f8b2c1b4 28 private notifier: Notifier,
08c1efbe 29 private confirmService: ConfirmService,
b1d40cff 30 private videoChannelService: VideoChannelService,
8165d00a 31 private screenService: ScreenService,
b1d40cff 32 private i18n: I18n
08c1efbe
C
33 ) {}
34
35 ngOnInit () {
36 this.user = this.authService.getUser()
37
38 this.loadVideoChannels()
39 }
40
8165d00a
RK
41 get isInSmallView () {
42 return this.screenService.isInSmallView()
43 }
44
45 get chartOptions () {
46 return {
47 legend: {
48 display: false
49 },
50 scales: {
51 xAxes: [{
52 display: false
53 }],
54 yAxes: [{
55 display: false,
56 ticks: {
57 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
58 max: this.videoChannelsMaximumDailyViews
59 }
747c5628 60 }]
8165d00a
RK
61 },
62 layout: {
63 padding: {
64 left: 15,
65 right: 15,
66 top: 10,
67 bottom: 0
68 }
69 },
70 elements: {
747c5628 71 point: {
8165d00a
RK
72 radius: 0
73 }
74 },
75 tooltips: {
76 mode: 'index',
77 intersect: false,
78 custom: function (tooltip: any) {
747c5628
RK
79 if (!tooltip) return
80 // disable displaying the color box
81 tooltip.displayColors = false
8165d00a
RK
82 },
83 callbacks: {
747c5628 84 label: (tooltip: any, data: any) => `${tooltip.value} views`
8165d00a
RK
85 }
86 },
87 hover: {
88 mode: 'index',
89 intersect: false
90 }
91 }
92 }
93
08c1efbe
C
94 async deleteVideoChannel (videoChannel: VideoChannel) {
95 const res = await this.confirmService.confirmWithInput(
b1d40cff 96 this.i18n(
df93a9be
C
97 // tslint:disable
98 'Do you really want to delete {{channelDisplayName}}? It will delete all videos uploaded in this channel, and you will not be able to create another channel with the same name ({{channelName}})!',
b4593cd7
C
99 { channelDisplayName: videoChannel.displayName, channelName: videoChannel.name }
100 ),
101 this.i18n(
102 'Please type the display name of the video channel ({{displayName}}) to confirm',
103 { displayName: videoChannel.displayName }
b1d40cff 104 ),
08c1efbe 105 videoChannel.displayName,
b1d40cff 106 this.i18n('Delete')
08c1efbe
C
107 )
108 if (res === false) return
109
110 this.videoChannelService.removeVideoChannel(videoChannel)
111 .subscribe(
f8b2c1b4 112 () => {
08c1efbe 113 this.loadVideoChannels()
f8b2c1b4 114 this.notifier.success(
25acef90 115 this.i18n('Video channel {{videoChannelName}} deleted.', { videoChannelName: videoChannel.displayName })
b1d40cff 116 )
08c1efbe
C
117 },
118
f8b2c1b4 119 error => this.notifier.error(error.message)
08c1efbe
C
120 )
121 }
122
123 private loadVideoChannels () {
124 this.authService.userInformationLoaded
747c5628 125 .pipe(flatMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true)))
8165d00a
RK
126 .subscribe(res => {
127 this.videoChannels = res.data
128 this.videoChannelsData = this.videoChannels.map(v => ({
129 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
130 datasets: [
131 {
132 label: this.i18n('Views for the day'),
133 data: v.viewsPerDay.map(day => day.views),
134 fill: false,
135 borderColor: "#c6c6c6"
136 }
137 ]
138 }))
139 this.videoChannelsMinimumDailyViews = minBy(this.videoChannels.map(v => minBy(v.viewsPerDay, day => day.views)), day => day.views).views
140 this.videoChannelsMaximumDailyViews = maxBy(this.videoChannels.map(v => maxBy(v.viewsPerDay, day => day.views)), day => day.views).views
141 })
08c1efbe
C
142 }
143}