]>
Commit | Line | Data |
---|---|---|
441e453a C |
1 | import { Component, ElementRef, EventEmitter, Output, ViewChild, OnInit } from '@angular/core' |
2 | import { Notifier, AuthService } from '@app/core' | |
3 | import { FormReactive, FormValidatorService, AbuseValidatorsService } from '@app/shared/shared-forms' | |
4 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { AbuseMessage, UserAbuse } from '@shared/models' | |
8 | import { AbuseService } from './abuse.service' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-abuse-message-modal', | |
12 | templateUrl: './abuse-message-modal.component.html', | |
13 | styleUrls: [ './abuse-message-modal.component.scss' ] | |
14 | }) | |
15 | export class AbuseMessageModalComponent extends FormReactive implements OnInit { | |
16 | @ViewChild('modal', { static: true }) modal: NgbModal | |
17 | @ViewChild('messagesBlock', { static: false }) messagesBlock: ElementRef | |
18 | ||
19 | @Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>() | |
20 | ||
21 | abuseMessages: AbuseMessage[] = [] | |
22 | textareaMessage: string | |
23 | sendingMessage = false | |
24 | ||
25 | private openedModal: NgbModalRef | |
26 | private abuse: UserAbuse | |
27 | ||
28 | constructor ( | |
29 | protected formValidatorService: FormValidatorService, | |
30 | private abuseValidatorsService: AbuseValidatorsService, | |
31 | private modalService: NgbModal, | |
32 | private auth: AuthService, | |
33 | private notifier: Notifier, | |
34 | private i18n: I18n, | |
35 | private abuseService: AbuseService | |
36 | ) { | |
37 | super() | |
38 | } | |
39 | ||
40 | ngOnInit () { | |
41 | this.buildForm({ | |
42 | message: this.abuseValidatorsService.ABUSE_MESSAGE | |
43 | }) | |
44 | } | |
45 | ||
46 | openModal (abuse: UserAbuse) { | |
47 | this.abuse = abuse | |
48 | ||
49 | this.openedModal = this.modalService.open(this.modal, { centered: true }) | |
50 | ||
51 | this.loadMessages() | |
52 | } | |
53 | ||
54 | hide () { | |
55 | this.abuseMessages = [] | |
56 | this.openedModal.close() | |
57 | } | |
58 | ||
59 | addMessage () { | |
60 | this.sendingMessage = true | |
61 | ||
62 | this.abuseService.addAbuseMessage(this.abuse, this.form.value['message']) | |
63 | .subscribe( | |
64 | () => { | |
65 | this.form.reset() | |
66 | this.sendingMessage = false | |
67 | this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length + 1 }) | |
68 | ||
69 | this.loadMessages() | |
70 | }, | |
71 | ||
72 | err => { | |
73 | this.sendingMessage = false | |
74 | console.error(err) | |
75 | this.notifier.error('Sorry but you cannot send this message. Please retry later') | |
76 | } | |
77 | ) | |
78 | } | |
79 | ||
80 | deleteMessage (abuseMessage: AbuseMessage) { | |
81 | this.abuseService.deleteAbuseMessage(this.abuse, abuseMessage) | |
82 | .subscribe( | |
83 | () => { | |
84 | this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length - 1 }) | |
85 | ||
86 | this.abuseMessages = this.abuseMessages.filter(m => m.id !== abuseMessage.id) | |
87 | }, | |
88 | ||
89 | err => this.notifier.error(err.message) | |
90 | ) | |
91 | } | |
92 | ||
93 | isMessageByMe (abuseMessage: AbuseMessage) { | |
94 | return this.auth.getUser().account.id === abuseMessage.account.id | |
95 | } | |
96 | ||
97 | private loadMessages () { | |
98 | this.abuseService.listAbuseMessages(this.abuse) | |
99 | .subscribe( | |
100 | res => { | |
101 | this.abuseMessages = res.data | |
102 | ||
103 | setTimeout(() => { | |
104 | if (!this.messagesBlock) return | |
105 | ||
106 | const element = this.messagesBlock.nativeElement as HTMLElement | |
107 | element.scrollIntoView({ block: 'end', inline: 'nearest' }) | |
108 | }) | |
109 | }, | |
110 | ||
111 | err => this.notifier.error(err.message) | |
112 | ) | |
113 | } | |
114 | ||
115 | } |