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'
11 selector: 'my-abuse-message-modal',
12 templateUrl: './abuse-message-modal.component.html',
13 styleUrls: [ './abuse-message-modal.component.scss' ]
15 export class AbuseMessageModalComponent extends FormReactive implements OnInit {
16 @ViewChild('modal', { static: true }) modal: NgbModal
17 @ViewChild('messagesBlock', { static: false }) messagesBlock: ElementRef
19 @Output() countMessagesUpdated = new EventEmitter<{ abuseId: number, countMessages: number }>()
21 abuseMessages: AbuseMessage[] = []
22 textareaMessage: string
23 sendingMessage = false
25 private openedModal: NgbModalRef
26 private abuse: UserAbuse
29 protected formValidatorService: FormValidatorService,
30 private abuseValidatorsService: AbuseValidatorsService,
31 private modalService: NgbModal,
32 private auth: AuthService,
33 private notifier: Notifier,
35 private abuseService: AbuseService
42 message: this.abuseValidatorsService.ABUSE_MESSAGE
46 openModal (abuse: UserAbuse) {
49 this.openedModal = this.modalService.open(this.modal, { centered: true })
55 this.abuseMessages = []
56 this.openedModal.close()
60 this.sendingMessage = true
62 this.abuseService.addAbuseMessage(this.abuse, this.form.value['message'])
66 this.sendingMessage = false
67 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length + 1 })
73 this.sendingMessage = false
75 this.notifier.error('Sorry but you cannot send this message. Please retry later')
80 deleteMessage (abuseMessage: AbuseMessage) {
81 this.abuseService.deleteAbuseMessage(this.abuse, abuseMessage)
84 this.countMessagesUpdated.emit({ abuseId: this.abuse.id, countMessages: this.abuseMessages.length - 1 })
86 this.abuseMessages = this.abuseMessages.filter(m => m.id !== abuseMessage.id)
89 err => this.notifier.error(err.message)
93 isMessageByMe (abuseMessage: AbuseMessage) {
94 return this.auth.getUser().account.id === abuseMessage.account.id
97 private loadMessages () {
98 this.abuseService.listAbuseMessages(this.abuse)
101 this.abuseMessages = res.data
104 if (!this.messagesBlock) return
106 const element = this.messagesBlock.nativeElement as HTMLElement
107 element.scrollIntoView({ block: 'end', inline: 'nearest' })
111 err => this.notifier.error(err.message)