Skip to content

Commit b862741

Browse files
committed
connect developer profile page
1 parent 8baf358 commit b862741

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

src/@seed/api/user/user.service.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { HttpClient } from '@angular/common/http'
33
import { inject, Injectable } from '@angular/core'
44
import type { Observable } from 'rxjs'
55
import { catchError, distinctUntilChanged, ReplaySubject, switchMap, take, tap } from 'rxjs'
6-
import type { CurrentUser, SetDefaultOrganizationResponse, UserUpdateRequest } from '@seed/api/user'
6+
import type { CurrentUser, GenerateApiKeyResponse, SetDefaultOrganizationResponse, UserUpdateRequest } from '@seed/api/user'
77

88
@Injectable({ providedIn: 'root' })
99
export class UserService {
@@ -58,4 +58,22 @@ export class UserService {
5858
}),
5959
)
6060
}
61+
62+
/**
63+
* Generate API Key
64+
*/
65+
generateApiKey(): Observable<GenerateApiKeyResponse> {
66+
return this.currentUser$.pipe(
67+
take(1),
68+
switchMap(({ id: userId }) => {
69+
return this._httpClient.post<GenerateApiKeyResponse>(`api/v3/users/${userId}/generate_api_key/`,
70+
{},
71+
)
72+
}),
73+
tap(() => {
74+
// Refresh user info after changing the API key
75+
this.getCurrentUser().subscribe()
76+
}),
77+
)
78+
}
6179
}

src/@seed/api/user/user.types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ export type SetDefaultOrganizationResponse = {
3434
};
3535
};
3636
}
37+
38+
export type GenerateApiKeyResponse = {
39+
status: string;
40+
api_key: string;
41+
}

src/app/modules/profile/developer/developer.component.html

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ <h2 class="pb-5 text-xl">{{ t('Manage Your API Key') }}</h2>
88
<div class="gt-xs:flex-row flex flex-col pt-4 text-center">
99
<div>
1010
<span class="font-bold">API Key: </span>
11-
<span class="bg-slate-200 px-5 py-3">{{ apikey }}</span>
11+
<span class="bg-slate-200 px-5 py-3">{{ user.api_key }}</span>
1212
</div>
1313
<div class="pt-7">
14-
<a mat-flat-button color="primary">
14+
<button mat-flat-button color="primary" (click)="generateKey()">
1515
<span class="ml-2">{{ t('Get a New API Key') }}</span>
16-
</a>
16+
</button>
1717
</div>
1818
</div>
1919

20+
<!-- Alert -->
21+
@if (showAlert) {
22+
<div class="mb-4 mt-0">
23+
<seed-alert class="-mb-4 mt-8" appearance="outline" [showIcon]="false" [type]="alert.type">
24+
{{ alert.message }}
25+
</seed-alert>
26+
</div>
27+
}
28+
2029
<!-- Divider -->
2130
<div class="mb-5 mt-10 border-t"></div>
2231
<h3 class="text-l mb-5">{{ t('Example Usage') }}</h3>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,65 @@
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'
23
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
34
import { MatButtonModule } from '@angular/material/button'
45
import { MatFormFieldModule } from '@angular/material/form-field'
56
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'
612
import { SharedImports } from '@seed/directives'
713
@Component({
814
selector: 'seed-profile-developer',
915
templateUrl: './developer.component.html',
1016
encapsulation: ViewEncapsulation.None,
11-
imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, ReactiveFormsModule, SharedImports],
17+
changeDetection: ChangeDetectionStrategy.OnPush,
18+
imports: [AlertComponent, FormsModule, MatButtonModule, MatFormFieldModule, MatIconModule, ReactiveFormsModule, SharedImports],
1219
})
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+
}
1565
}

0 commit comments

Comments
 (0)