aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-settings/my-account-email-preferences/my-account-email-preferences.component.ts
blob: 7fd59d7c8c395e3018b5965dbe54268b463bb53c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Subject } from 'rxjs'
import { Component, Input, OnInit } from '@angular/core'
import { Notifier, UserService } from '@app/core'
import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
import { User, UserUpdateMe } from '@shared/models'

@Component({
  selector: 'my-account-email-preferences',
  templateUrl: './my-account-email-preferences.component.html',
  styleUrls: [ './my-account-email-preferences.component.scss' ]
})
export class MyAccountEmailPreferencesComponent extends FormReactive implements OnInit {
  @Input() user: User = null
  @Input() userInformationLoaded: Subject<any>

  constructor (
    protected formReactiveService: FormReactiveService,
    private userService: UserService,
    private notifier: Notifier
  ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      'email-public': null
    })

    this.userInformationLoaded.subscribe(() => {
      this.form.patchValue({ 'email-public': this.user.emailPublic })
    })
  }

  updateEmailPublic () {
    const details: UserUpdateMe = {
      emailPublic: this.form.value['email-public']
    }

    this.userService.updateMyProfile(details)
      .subscribe({
        next: () => {
          if (details.emailPublic) this.notifier.success($localize`Email is now public`)
          else this.notifier.success($localize`Email is now private`)

          this.user.emailPublic = details.emailPublic
        },

        error: err => console.log(err.message)
      })
  }
}