-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,115 @@ | ||
import { AngorConfig, AngorConfigService, Scheme, Theme, Themes } from '@angor/services/config'; | ||
import { BooleanInput } from '@angular/cdk/coercion'; | ||
import { NgClass } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
Input, | ||
OnDestroy, | ||
OnInit, | ||
ViewEncapsulation, | ||
} from '@angular/core'; | ||
import { AngorConfig, AngorConfigService, Scheme, Theme, Themes } from '@angor/services/config'; | ||
import { Subject, takeUntil } from 'rxjs'; | ||
import { Router } from '@angular/router'; | ||
import { UserService } from 'app/core/user/user.service'; | ||
import { SignerService } from 'app/services/signer.service'; | ||
import { NostrService } from 'app/services/nostr.service'; | ||
import { NgClass, CommonModule } from '@angular/common'; | ||
import { MatButtonModule } from '@angular/material/button'; | ||
import { MatDividerModule } from '@angular/material/divider'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
import { MatMenuModule } from '@angular/material/menu'; | ||
import { Router } from '@angular/router'; | ||
import { UserService } from 'app/core/user/user.service'; | ||
import { User } from 'app/core/user/user.types'; | ||
import { Subject, takeUntil } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'user', | ||
templateUrl: './user.component.html', | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
exportAs: 'user', | ||
standalone: true, | ||
imports: [ | ||
MatButtonModule, | ||
MatMenuModule, | ||
MatIconModule, | ||
NgClass, | ||
MatDividerModule, | ||
CommonModule | ||
], | ||
}) | ||
export class UserComponent implements OnInit, OnDestroy { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
static ngAcceptInputType_showAvatar: BooleanInput; | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
|
||
@Input() showAvatar: boolean = true; | ||
user: User; | ||
user: any; | ||
isLoading: boolean = true; | ||
errorMessage: string | null = null; | ||
metadata: any; | ||
|
||
private _unsubscribeAll: Subject<any> = new Subject<any>(); | ||
config: AngorConfig; | ||
layout: string; | ||
scheme: 'dark' | 'light'; | ||
theme: string; | ||
themes: Themes; | ||
/** | ||
* Constructor | ||
*/ | ||
|
||
constructor( | ||
private _changeDetectorRef: ChangeDetectorRef, | ||
private _router: Router, | ||
private _userService: UserService, | ||
private _angorConfigService: AngorConfigService | ||
private _angorConfigService: AngorConfigService, | ||
private _nostrService: NostrService, | ||
private _signerService: SignerService | ||
) { } | ||
|
||
) {} | ||
|
||
/** | ||
* On init | ||
*/ | ||
ngOnInit(): void { | ||
// Subscribe to user changes | ||
this.user = { | ||
id: '1', | ||
name: 'Test User', | ||
email: '[email protected]', | ||
avatar: '/images/avatars/male-06.jpg', | ||
status: 'online' | ||
}; | ||
this.loadUserProfile(); | ||
|
||
this._angorConfigService.config$ | ||
this._angorConfigService.config$ | ||
.pipe(takeUntil(this._unsubscribeAll)) | ||
.subscribe((config: AngorConfig) => { | ||
localStorage.setItem('angorConfig', JSON.stringify(config)); | ||
|
||
this.config = config; | ||
}); | ||
} | ||
|
||
/** | ||
* On destroy | ||
*/ | ||
ngOnDestroy(): void { | ||
// Unsubscribe from all subscriptions | ||
this._unsubscribeAll.next(null); | ||
this._unsubscribeAll.complete(); | ||
} | ||
private async loadUserProfile(): Promise<void> { | ||
this.isLoading = true; | ||
this.errorMessage = null; | ||
const publicKey = this._signerService.getPublicKey(); | ||
|
||
/** | ||
* Update the user status | ||
* | ||
* @param status | ||
*/ | ||
updateUserStatus(status: string): void { | ||
// Return if user is not available | ||
if (!this.user) { | ||
if (!publicKey) { | ||
this.errorMessage = 'No public key found. Please log in again.'; | ||
this.isLoading = false; | ||
return; | ||
} | ||
|
||
// Update the user | ||
this._userService | ||
.update({ | ||
...this.user, | ||
status, | ||
}) | ||
.subscribe(); | ||
try { | ||
const metadata = await this._nostrService.fetchMetadata(publicKey); | ||
this.metadata = metadata; | ||
} catch (error) { | ||
console.error('Failed to load profile data:', error); | ||
this.errorMessage = 'Failed to load profile data. Please try again later.'; | ||
} finally { | ||
this.isLoading = false; | ||
this._changeDetectorRef.markForCheck(); // Trigger change detection to update the view | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this._unsubscribeAll.next(null); | ||
this._unsubscribeAll.complete(); | ||
} | ||
|
||
/** | ||
* Sign out | ||
*/ | ||
signOut(): void { | ||
logout(): void { | ||
this._router.navigate(['/logout']); | ||
} | ||
|
||
/** | ||
* Set the layout on the config | ||
* | ||
* @param layout | ||
*/ | ||
setLayout(layout: string): void { | ||
// Clear the 'layout' query param to allow layout changes | ||
this._router | ||
.navigate([], { | ||
queryParams: { | ||
layout: null, | ||
}, | ||
queryParamsHandling: 'merge', | ||
}) | ||
.then(() => { | ||
// Set the config | ||
this._angorConfigService.config = { layout }; | ||
}); | ||
} | ||
profile(): void { | ||
this._router.navigate(['/profile']); | ||
} | ||
|
||
/** | ||
* Set the scheme on the config | ||
* | ||
* @param scheme | ||
*/ | ||
setScheme(scheme: Scheme): void { | ||
this._angorConfigService.config = { scheme }; | ||
} | ||
setLayout(layout: string): void { | ||
this._angorConfigService.config = { layout }; | ||
} | ||
|
||
/** | ||
* Set the theme on the config | ||
* | ||
* @param theme | ||
*/ | ||
setTheme(theme: Theme): void { | ||
this._angorConfigService.config = { theme }; | ||
} | ||
setScheme(scheme: Scheme): void { | ||
this._angorConfigService.config = { scheme }; | ||
} | ||
|
||
setTheme(theme: Theme): void { | ||
this._angorConfigService.config = { theme }; | ||
} | ||
} |