contact-admin-modal.component.ts 2.45 KB
Newer Older
1
import { Component, OnInit, ViewChild } from '@angular/core'
Chocobozzz's avatar
Chocobozzz committed
2
import { Notifier, ServerService } from '@app/core'
3
4
5
6
7
8
9
import {
  BODY_VALIDATOR,
  FROM_EMAIL_VALIDATOR,
  FROM_NAME_VALIDATOR,
  SUBJECT_VALIDATOR
} from '@app/shared/form-validators/instance-validators'
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
Chocobozzz's avatar
Chocobozzz committed
10
import { InstanceService } from '@app/shared/shared-instance'
11
12
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
13
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
Chocobozzz's avatar
Chocobozzz committed
14
import { HTMLServerConfig } from '@shared/models'
15
16
17
18
19
20
21

@Component({
  selector: 'my-contact-admin-modal',
  templateUrl: './contact-admin-modal.component.html',
  styleUrls: [ './contact-admin-modal.component.scss' ]
})
export class ContactAdminModalComponent extends FormReactive implements OnInit {
Chocobozzz's avatar
Chocobozzz committed
22
  @ViewChild('modal', { static: true }) modal: NgbModal
23
24
25
26

  error: string

  private openedModal: NgbModalRef
Chocobozzz's avatar
Chocobozzz committed
27
  private serverConfig: HTMLServerConfig
28
29
30
31
32

  constructor (
    protected formValidatorService: FormValidatorService,
    private modalService: NgbModal,
    private instanceService: InstanceService,
Chocobozzz's avatar
Chocobozzz committed
33
    private serverService: ServerService,
Chocobozzz's avatar
Chocobozzz committed
34
    private notifier: Notifier
35
36
37
38
  ) {
    super()
  }

Chocobozzz's avatar
Chocobozzz committed
39
  get instanceName () {
Chocobozzz's avatar
Chocobozzz committed
40
    return this.serverConfig.instance.name
Chocobozzz's avatar
Chocobozzz committed
41
42
  }

43
  ngOnInit () {
Chocobozzz's avatar
Chocobozzz committed
44
    this.serverConfig = this.serverService.getHTMLConfig()
Chocobozzz's avatar
Chocobozzz committed
45

46
    this.buildForm({
47
48
49
50
      fromName: FROM_NAME_VALIDATOR,
      fromEmail: FROM_EMAIL_VALIDATOR,
      subject: SUBJECT_VALIDATOR,
      body: BODY_VALIDATOR
51
52
53
54
    })
  }

  show () {
55
    this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
56
57
58
59
60
61
62
63
64
65
66
67
68
  }

  hide () {
    this.form.reset()
    this.error = undefined

    this.openedModal.close()
    this.openedModal = null
  }

  sendForm () {
    const fromName = this.form.value['fromName']
    const fromEmail = this.form.value[ 'fromEmail' ]
69
    const subject = this.form.value[ 'subject' ]
70
71
    const body = this.form.value[ 'body' ]

72
    this.instanceService.contactAdministrator(fromEmail, fromName, subject, body)
73
74
        .subscribe(
          () => {
Chocobozzz's avatar
Chocobozzz committed
75
            this.notifier.success($localize`Your message has been sent.`)
76
77
78
79
            this.hide()
          },

          err => {
80
            this.error = err.status === HttpStatusCode.FORBIDDEN_403
Chocobozzz's avatar
Chocobozzz committed
81
              ? $localize`You already sent this form recently`
82
83
84
85
86
              : err.message
          }
        )
  }
}