]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/follows/following-add/following-add.component.ts
Add i18n attributes
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / follows / following-add / following-add.component.ts
CommitLineData
e600e1fe 1import { Component } from '@angular/core'
df98563e 2import { Router } from '@angular/router'
df98563e 3import { NotificationsService } from 'angular2-notifications'
df98563e
C
4import { ConfirmService } from '../../../core'
5import { validateHost } from '../../../shared'
51548b31 6import { FollowService } from '../shared'
b1d40cff 7import { I18n } from '@ngx-translate/i18n-polyfill'
e105c19c
C
8
9@Component({
51548b31
C
10 selector: 'my-following-add',
11 templateUrl: './following-add.component.html',
12 styleUrls: [ './following-add.component.scss' ]
e105c19c 13})
e600e1fe
C
14export class FollowingAddComponent {
15 hostsString = ''
16 hostsError: string = null
df98563e 17 error: string = null
e105c19c 18
df98563e 19 constructor (
7ddd02c9
C
20 private router: Router,
21 private notificationsService: NotificationsService,
5769e1db 22 private confirmService: ConfirmService,
b1d40cff
C
23 private followService: FollowService,
24 private i18n: I18n
7ddd02c9 25 ) {}
e105c19c 26
e600e1fe 27 httpEnabled () {
df98563e 28 return window.location.protocol === 'https:'
8735451a
C
29 }
30
e600e1fe
C
31 onHostsChanged () {
32 this.hostsError = null
e105c19c 33
e600e1fe
C
34 const newHostsErrors = []
35 const hosts = this.getNotEmptyHosts()
e105c19c 36
e600e1fe
C
37 for (const host of hosts) {
38 if (validateHost(host) === false) {
b1d40cff 39 newHostsErrors.push(this.i18n('{{ host }} is not valid', { host }))
e600e1fe 40 }
9e8aa10d
C
41 }
42
e600e1fe
C
43 if (newHostsErrors.length !== 0) {
44 this.hostsError = newHostsErrors.join('. ')
9e8aa10d
C
45 }
46 }
47
1f30a185 48 async addFollowing () {
df98563e 49 this.error = ''
e105c19c 50
e600e1fe
C
51 const hosts = this.getNotEmptyHosts()
52 if (hosts.length === 0) {
b1d40cff 53 this.error = this.i18n('You need to specify hosts to follow.')
e105c19c
C
54 }
55
e600e1fe 56 if (!this.isHostsUnique(hosts)) {
b1d40cff 57 this.error = this.i18n('Hosts need to be unique.')
df98563e 58 return
e105c19c
C
59 }
60
b1d40cff
C
61 const confirmMessage = this.i18n('If you confirm, you will send a follow request to:<br /> - ') + hosts.join('<br /> - ')
62 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Follow new server(s)'))
1f30a185 63 if (res === false) return
e105c19c 64
1f30a185
C
65 this.followService.follow(hosts).subscribe(
66 () => {
b1d40cff 67 this.notificationsService.success(this.i18n('Success'), this.i18n('Follow request(s) sent!'))
e600e1fe 68
1f30a185
C
69 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
70 },
7ddd02c9 71
b1d40cff 72 err => this.notificationsService.error(this.i18n('Error'), err.message)
df98563e 73 )
e105c19c
C
74 }
75
df98563e
C
76 private isHostsUnique (hosts: string[]) {
77 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
e105c19c 78 }
e600e1fe
C
79
80 private getNotEmptyHosts () {
b1d40cff 81 return this.hostsString
e600e1fe
C
82 .split('\n')
83 .filter(host => host && host.length !== 0) // Eject empty hosts
e600e1fe 84 }
e105c19c 85}