aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/follows/following-list/follow-modal.component.ts
blob: c55fc8d81f2380066dc88a8dbebf48e4e6cd8f1e (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
import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
import { Notifier } from '@app/core'
import { splitAndGetNotEmpty, UNIQUE_HOSTS_OR_HANDLE_VALIDATOR } from '@app/shared/form-validators/host-validators'
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
import { InstanceFollowService } from '@app/shared/shared-instance'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'

@Component({
  selector: 'my-follow-modal',
  templateUrl: './follow-modal.component.html',
  styleUrls: [ './follow-modal.component.scss' ]
})
export class FollowModalComponent extends FormReactive implements OnInit {
  @ViewChild('modal', { static: true }) modal: NgbModal

  @Output() newFollow = new EventEmitter<void>()

  placeholder = 'example.com\nchocobozzz@example.com\nchocobozzz_channel@example.com'

  private openedModal: NgbModalRef

  constructor (
    protected formValidatorService: FormValidatorService,
    private modalService: NgbModal,
    private followService: InstanceFollowService,
    private notifier: Notifier
  ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      hostsOrHandles: UNIQUE_HOSTS_OR_HANDLE_VALIDATOR
    })
  }

  openModal () {
    this.openedModal = this.modalService.open(this.modal, { centered: true })
  }

  hide () {
    this.openedModal.close()
  }

  submit () {
    this.addFollowing()

    this.form.reset()
    this.hide()
  }

  httpEnabled () {
    return window.location.protocol === 'https:'
  }

  private async addFollowing () {
    const hostsOrHandles = splitAndGetNotEmpty(this.form.value['hostsOrHandles'])

    this.followService.follow(hostsOrHandles)
      .subscribe({
        next: () => {
          this.notifier.success($localize`Follow request(s) sent!`)
          this.newFollow.emit()
        },

        error: err => this.notifier.error(err.message)
      })
  }
}