1 import { Component, ViewChild } from '@angular/core'
2 import { FormControl, FormGroup } from '@angular/forms'
3 import { NgbActiveModal, NgbAlert } from '@ng-bootstrap/ng-bootstrap'
4 import { EditDialogComponent } from 'src/app/components/common/edit-dialog/edit-dialog.component'
5 import { IMAPSecurity, MailAccount } from 'src/app/data/mail-account'
6 import { MailAccountService } from 'src/app/services/rest/mail-account.service'
7 import { UserService } from 'src/app/services/rest/user.service'
8 import { SettingsService } from 'src/app/services/settings.service'
10 const IMAP_SECURITY_OPTIONS = [
11 { id: IMAPSecurity.None, name: $localize`No encryption` },
12 { id: IMAPSecurity.SSL, name: $localize`SSL` },
13 { id: IMAPSecurity.STARTTLS, name: $localize`STARTTLS` },
17 selector: 'pngx-mail-account-edit-dialog',
18 templateUrl: './mail-account-edit-dialog.component.html',
19 styleUrls: ['./mail-account-edit-dialog.component.scss'],
21 export class MailAccountEditDialogComponent extends EditDialogComponent<MailAccount> {
22 testActive: boolean = false
26 @ViewChild('testResultAlert', { static: false }) testResultAlert: NgbAlert
29 service: MailAccountService,
30 activeModal: NgbActiveModal,
31 userService: UserService,
32 settingsService: SettingsService
34 super(service, activeModal, userService, settingsService)
38 return $localize`Create new mail account`
42 return $localize`Edit mail account`
45 getForm(): FormGroup {
46 return new FormGroup({
47 name: new FormControl(null),
48 imap_server: new FormControl(null),
49 imap_port: new FormControl(null),
50 imap_security: new FormControl(IMAPSecurity.SSL),
51 username: new FormControl(null),
52 password: new FormControl(null),
53 is_token: new FormControl(false),
54 character_set: new FormControl('UTF-8'),
58 get imapSecurityOptions() {
59 return IMAP_SECURITY_OPTIONS
63 this.testActive = true
64 this.testResult = null
65 clearTimeout(this.alertTimeout)
66 const mailService = this.service as MailAccountService
67 const newObject = Object.assign(
68 Object.assign({}, this.object),
71 mailService.test(newObject).subscribe({
72 next: (result: { success: boolean }) => {
73 this.testActive = false
74 this.testResult = result.success ? 'success' : 'danger'
75 this.alertTimeout = setTimeout(() => this.testResultAlert.close(), 5000)
78 this.testActive = false
79 this.testResult = 'danger'
80 this.alertTimeout = setTimeout(() => this.testResultAlert.close(), 5000)
85 get testResultMessage() {
86 return this.testResult === 'success'
87 ? $localize`Successfully connected to the mail server`
88 : $localize`Unable to connect to the mail server`