]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/+my-account-video-channels/my-account-video-channels.component.ts
variable columns for users list, more columns possible, badge display for statuses
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / +my-account-video-channels / my-account-video-channels.component.ts
CommitLineData
67ed6552
C
1import { ChartData } from 'chart.js'
2import { max, maxBy, min, minBy } from 'lodash-es'
db400f44 3import { flatMap } from 'rxjs/operators'
67ed6552
C
4import { Component, OnInit } from '@angular/core'
5import { AuthService, ConfirmService, Notifier, ScreenService, User } from '@app/core'
6import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
b1d40cff 7import { I18n } from '@ngx-translate/i18n-polyfill'
08c1efbe
C
8
9@Component({
10 selector: 'my-account-video-channels',
11 templateUrl: './my-account-video-channels.component.html',
12 styleUrls: [ './my-account-video-channels.component.scss' ]
13})
6a478b11 14export class MyAccountVideoChannelsComponent implements OnInit {
08c1efbe 15 videoChannels: VideoChannel[] = []
3d527ba1 16 videoChannelsChartData: ChartData[]
8165d00a
RK
17 videoChannelsMinimumDailyViews = 0
18 videoChannelsMaximumDailyViews: number
08c1efbe
C
19
20 private user: User
21
22 constructor (
23 private authService: AuthService,
f8b2c1b4 24 private notifier: Notifier,
08c1efbe 25 private confirmService: ConfirmService,
b1d40cff 26 private videoChannelService: VideoChannelService,
8165d00a 27 private screenService: ScreenService,
b1d40cff 28 private i18n: I18n
08c1efbe
C
29 ) {}
30
31 ngOnInit () {
32 this.user = this.authService.getUser()
33
34 this.loadVideoChannels()
35 }
36
8165d00a
RK
37 get isInSmallView () {
38 return this.screenService.isInSmallView()
39 }
40
41 get chartOptions () {
42 return {
43 legend: {
44 display: false
45 },
46 scales: {
47 xAxes: [{
48 display: false
49 }],
50 yAxes: [{
51 display: false,
52 ticks: {
53 min: Math.max(0, this.videoChannelsMinimumDailyViews - (3 * this.videoChannelsMaximumDailyViews / 100)),
d6af8146 54 max: Math.max(1, this.videoChannelsMaximumDailyViews)
8165d00a 55 }
747c5628 56 }]
8165d00a
RK
57 },
58 layout: {
59 padding: {
60 left: 15,
61 right: 15,
62 top: 10,
63 bottom: 0
64 }
65 },
66 elements: {
747c5628 67 point: {
8165d00a
RK
68 radius: 0
69 }
70 },
71 tooltips: {
72 mode: 'index',
73 intersect: false,
74 custom: function (tooltip: any) {
747c5628
RK
75 if (!tooltip) return
76 // disable displaying the color box
77 tooltip.displayColors = false
8165d00a
RK
78 },
79 callbacks: {
747c5628 80 label: (tooltip: any, data: any) => `${tooltip.value} views`
8165d00a
RK
81 }
82 },
83 hover: {
84 mode: 'index',
85 intersect: false
86 }
87 }
88 }
89
08c1efbe
C
90 async deleteVideoChannel (videoChannel: VideoChannel) {
91 const res = await this.confirmService.confirmWithInput(
b1d40cff 92 this.i18n(
df93a9be 93 // tslint:disable
1ba471c5
C
94 '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}})!',
95 { channelDisplayName: videoChannel.displayName, videosCount: videoChannel.videosCount, channelName: videoChannel.name }
b4593cd7
C
96 ),
97 this.i18n(
98 'Please type the display name of the video channel ({{displayName}}) to confirm',
99 { displayName: videoChannel.displayName }
b1d40cff 100 ),
08c1efbe 101 videoChannel.displayName,
b1d40cff 102 this.i18n('Delete')
08c1efbe
C
103 )
104 if (res === false) return
105
106 this.videoChannelService.removeVideoChannel(videoChannel)
107 .subscribe(
f8b2c1b4 108 () => {
08c1efbe 109 this.loadVideoChannels()
f8b2c1b4 110 this.notifier.success(
25acef90 111 this.i18n('Video channel {{videoChannelName}} deleted.', { videoChannelName: videoChannel.displayName })
b1d40cff 112 )
08c1efbe
C
113 },
114
f8b2c1b4 115 error => this.notifier.error(error.message)
08c1efbe
C
116 )
117 }
118
119 private loadVideoChannels () {
120 this.authService.userInformationLoaded
747c5628 121 .pipe(flatMap(() => this.videoChannelService.listAccountVideoChannels(this.user.account, null, true)))
8165d00a
RK
122 .subscribe(res => {
123 this.videoChannels = res.data
3d527ba1
RK
124
125 // chart data
126 this.videoChannelsChartData = this.videoChannels.map(v => ({
8165d00a
RK
127 labels: v.viewsPerDay.map(day => day.date.toLocaleDateString()),
128 datasets: [
129 {
130 label: this.i18n('Views for the day'),
131 data: v.viewsPerDay.map(day => day.views),
132 fill: false,
133 borderColor: "#c6c6c6"
134 }
135 ]
3d527ba1
RK
136 } as ChartData))
137
138 // chart options that depend on chart data:
139 // we don't want to skew values and have min at 0, so we define what the floor/ceiling is here
140 this.videoChannelsMinimumDailyViews = min(
141 this.videoChannels.map(v => minBy( // compute local minimum daily views for each channel, by their "views" attribute
142 v.viewsPerDay,
143 day => day.views
144 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
145 )
146 this.videoChannelsMaximumDailyViews = max(
147 this.videoChannels.map(v => maxBy( // compute local maximum daily views for each channel, by their "views" attribute
148 v.viewsPerDay,
149 day => day.views
150 ).views) // the object returned is a ViewPerDate, so we still need to get the views attribute
151 )
8165d00a 152 })
08c1efbe
C
153 }
154}