aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/modal/account-setup-warning-modal.component.ts
blob: b999a3407d54935155d61928938bc4acec236835 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Component, ElementRef, ViewChild } from '@angular/core'
import { Notifier, ServerService, User, UserService } from '@app/core'
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
import { logger } from '@root-helpers/logger'
import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'

@Component({
  selector: 'my-account-setup-warning-modal',
  templateUrl: './account-setup-warning-modal.component.html',
  styleUrls: [ './account-setup-warning-modal.component.scss' ]
})
export class AccountSetupWarningModalComponent {
  @ViewChild('modal', { static: true }) modal: ElementRef

  stopDisplayModal = false
  ref: NgbModalRef

  user: User

  private LOCAL_STORAGE_KEYS = {
    NO_ACCOUNT_SETUP_WARNING_MODAL: 'no_account_setup_warning_modal'
  }

  constructor (
    private userService: UserService,
    private modalService: NgbModal,
    private notifier: Notifier,
    private serverService: ServerService
  ) { }

  get instanceName () {
    return this.serverService.getHTMLConfig().instance.name
  }

  hasAccountAvatar (user: User) {
    return user.account.avatars.length !== 0
  }

  hasAccountDescription (user: User) {
    return !!user.account.description
  }

  shouldOpen (user: User) {
    if (user.noAccountSetupWarningModal === true) return false
    if (peertubeLocalStorage.getItem(this.LOCAL_STORAGE_KEYS.NO_ACCOUNT_SETUP_WARNING_MODAL) === 'true') return false

    if (this.hasAccountAvatar(user) && this.hasAccountDescription(user)) return false
    if (this.userService.hasSignupInThisSession()) return false

    return true
  }

  show (user: User) {
    this.user = user

    if (this.ref) return

    this.ref = this.modalService.open(this.modal, {
      centered: true,
      backdrop: 'static',
      keyboard: false,
      size: 'md'
    })

    this.ref.result.finally(() => {
      if (this.stopDisplayModal === true) this.doNotOpenAgain()
    })
  }

  private doNotOpenAgain () {
    peertubeLocalStorage.setItem(this.LOCAL_STORAGE_KEYS.NO_ACCOUNT_SETUP_WARNING_MODAL, 'true')

    this.userService.updateMyProfile({ noAccountSetupWarningModal: true })
        .subscribe({
          next: () => logger.info('We will not open the account setup modal again.'),

          error: err => this.notifier.error(err.message)
        })
  }
}