aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-videos/video-change-ownership/video-change-ownership.component.ts
blob: eb3f9404f83d7f83dd4e81ec6bdd97f809f40955 (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
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { NotificationsService } from 'angular2-notifications'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { FormReactive, UserService } from '../../../shared/index'
import { Video } from '@app/shared/video/video.model'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { FormValidatorService, VideoChangeOwnershipValidatorsService } from '@app/shared'
import { VideoOwnershipService } from '@app/shared/video-ownership'

@Component({
  selector: 'my-video-change-ownership',
  templateUrl: './video-change-ownership.component.html',
  styleUrls: [ './video-change-ownership.component.scss' ]
})
export class VideoChangeOwnershipComponent extends FormReactive implements OnInit {
  @ViewChild('modal') modal: ElementRef

  usernamePropositions: string[]

  error: string = null

  private video: Video | undefined = undefined

  constructor (
    protected formValidatorService: FormValidatorService,
    private videoChangeOwnershipValidatorsService: VideoChangeOwnershipValidatorsService,
    private videoOwnershipService: VideoOwnershipService,
    private notificationsService: NotificationsService,
    private userService: UserService,
    private modalService: NgbModal,
    private i18n: I18n
  ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      username: this.videoChangeOwnershipValidatorsService.USERNAME
    })
    this.usernamePropositions = []
  }

  show (video: Video) {
    this.video = video
    this.modalService
      .open(this.modal)
      .result
      .then(() => this.changeOwnership())
      .catch((_) => _) // Called when closing (cancel) the modal without validating, do nothing
  }

  // TODO: typing
  search (event: any) {
    const query = event.query
    this.userService.autocomplete(query)
      .subscribe(
        usernames => {
          this.usernamePropositions = usernames
        },

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

  changeOwnership () {
    const username = this.form.value['username']

    this.videoOwnershipService
      .changeOwnership(this.video.id, username)
      .subscribe(
        () => this.notificationsService.success(this.i18n('Success'), this.i18n('Ownership change request sent.')),

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