|
1 |
| -import { Component, ViewEncapsulation } from '@angular/core' |
| 1 | +import type { OnDestroy, OnInit } from '@angular/core' |
| 2 | +import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject, ViewEncapsulation } from '@angular/core' |
2 | 3 | import { FormsModule, ReactiveFormsModule } from '@angular/forms'
|
3 | 4 | import { MatButtonModule } from '@angular/material/button'
|
4 | 5 | import { MatFormFieldModule } from '@angular/material/form-field'
|
5 | 6 | import { MatIconModule } from '@angular/material/icon'
|
| 7 | +import { Subject, takeUntil } from 'rxjs' |
| 8 | +import type { CurrentUser } from '@seed/api/user' |
| 9 | +import { UserService } from '@seed/api/user' |
| 10 | +import type { Alert } from '@seed/components' |
| 11 | +import { AlertComponent } from '@seed/components' |
6 | 12 | import { SharedImports } from '@seed/directives'
|
7 | 13 | @Component({
|
8 | 14 | selector: 'seed-profile-developer',
|
9 | 15 | templateUrl: './developer.component.html',
|
10 | 16 | encapsulation: ViewEncapsulation.None,
|
11 |
| - imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, ReactiveFormsModule, SharedImports], |
| 17 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 18 | + imports: [AlertComponent, FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, ReactiveFormsModule, SharedImports], |
12 | 19 | })
|
13 |
| -export class ProfileDeveloperComponent { |
14 |
| - apikey = '12345ABCDE' |
| 20 | +export class ProfileDeveloperComponent implements OnInit, OnDestroy { |
| 21 | + private _userService = inject(UserService) |
| 22 | + private _changeDetectorRef = inject(ChangeDetectorRef) |
| 23 | + |
| 24 | + alert: Alert |
| 25 | + showAlert = false |
| 26 | + user: CurrentUser |
| 27 | + |
| 28 | + private readonly _unsubscribeAll$ = new Subject<void>() |
| 29 | + |
| 30 | + ngOnInit(): void { |
| 31 | + // Subscribe to user changes |
| 32 | + this._userService.currentUser$.pipe(takeUntil(this._unsubscribeAll$)).subscribe((currentUser) => { |
| 33 | + this.user = currentUser |
| 34 | + |
| 35 | + // Mark for check |
| 36 | + this._changeDetectorRef.markForCheck() |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + ngOnDestroy(): void { |
| 41 | + this._unsubscribeAll$.next() |
| 42 | + this._unsubscribeAll$.complete() |
| 43 | + } |
| 44 | + |
| 45 | + generateKey(): void { |
| 46 | + // send request to generate a new key; refresh user |
| 47 | + this._userService.generateApiKey().subscribe({ |
| 48 | + error: (error) => { |
| 49 | + console.error('Error:', error) |
| 50 | + this.alert = { |
| 51 | + type: 'error', |
| 52 | + message: 'Generate New Key Unsuccessful...', |
| 53 | + } |
| 54 | + this.showAlert = true |
| 55 | + }, |
| 56 | + complete: () => { |
| 57 | + this.alert = { |
| 58 | + type: 'success', |
| 59 | + message: 'New API Key Generated!', |
| 60 | + } |
| 61 | + this.showAlert = true |
| 62 | + }, |
| 63 | + }) |
| 64 | + } |
15 | 65 | }
|
0 commit comments