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