]>
Commit | Line | Data |
---|---|---|
4d029ef8 C |
1 | import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
2 | import { Notifier } from '@app/core' | |
eaa52952 | 3 | import { prepareIcu } from '@app/helpers' |
4d029ef8 | 4 | import { splitAndGetNotEmpty, UNIQUE_HOSTS_OR_HANDLE_VALIDATOR } from '@app/shared/form-validators/host-validators' |
5c5bcea2 | 5 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' |
4d029ef8 C |
6 | import { InstanceFollowService } from '@app/shared/shared-instance' |
7 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
8 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-follow-modal', | |
12 | templateUrl: './follow-modal.component.html', | |
13 | styleUrls: [ './follow-modal.component.scss' ] | |
14 | }) | |
15 | export class FollowModalComponent extends FormReactive implements OnInit { | |
16 | @ViewChild('modal', { static: true }) modal: NgbModal | |
17 | ||
18 | @Output() newFollow = new EventEmitter<void>() | |
19 | ||
20 | placeholder = 'example.com\nchocobozzz@example.com\nchocobozzz_channel@example.com' | |
21 | ||
22 | private openedModal: NgbModalRef | |
23 | ||
24 | constructor ( | |
5c5bcea2 | 25 | protected formReactiveService: FormReactiveService, |
4d029ef8 C |
26 | private modalService: NgbModal, |
27 | private followService: InstanceFollowService, | |
28 | private notifier: Notifier | |
29 | ) { | |
30 | super() | |
31 | } | |
32 | ||
33 | ngOnInit () { | |
34 | this.buildForm({ | |
35 | hostsOrHandles: UNIQUE_HOSTS_OR_HANDLE_VALIDATOR | |
36 | }) | |
37 | } | |
38 | ||
39 | openModal () { | |
40 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
41 | } | |
42 | ||
43 | hide () { | |
44 | this.openedModal.close() | |
45 | } | |
46 | ||
47 | submit () { | |
48 | this.addFollowing() | |
49 | ||
50 | this.form.reset() | |
51 | this.hide() | |
52 | } | |
53 | ||
54 | httpEnabled () { | |
55 | return window.location.protocol === 'https:' | |
56 | } | |
57 | ||
98ab5dc8 | 58 | private addFollowing () { |
4d029ef8 C |
59 | const hostsOrHandles = splitAndGetNotEmpty(this.form.value['hostsOrHandles']) |
60 | ||
1378c0d3 C |
61 | this.followService.follow(hostsOrHandles) |
62 | .subscribe({ | |
63 | next: () => { | |
eaa52952 | 64 | this.notifier.success( |
baf99fcc | 65 | prepareIcu($localize`{count, plural, =1 {Follow request sent!} other {Follow requests sent!}}`)( |
eaa52952 C |
66 | { count: hostsOrHandles.length }, |
67 | $localize`Follow request(s) sent!` | |
68 | ) | |
69 | ) | |
70 | ||
1378c0d3 C |
71 | this.newFollow.emit() |
72 | }, | |
4d029ef8 | 73 | |
1378c0d3 C |
74 | error: err => this.notifier.error(err.message) |
75 | }) | |
4d029ef8 C |
76 | } |
77 | } |