1+ import { Component , Input , TemplateRef , ViewChild } from "@angular/core" ;
2+ import { BannerService } from "../../../banners/banner.service" ;
3+ import { ExtendedUser } from "../../../api/types/users/extended-user" ;
4+ import { ClientService } from "../../../api/client.service" ;
5+ import { ButtonOrNavItemComponent } from "../form/button-or-navitem.component" ;
6+ import { faHeart , faHeartCrack } from "@fortawesome/free-solid-svg-icons" ;
7+ import { FancyHeaderButtonsComponent } from "./fancy-header-buttons.component" ;
8+ import { User } from "../../../api/types/users/user" ;
9+ import { UserRelations } from "../../../api/types/users/user-relations" ;
10+ import { RefreshApiError } from "../../../api/refresh-api-error" ;
11+
12+ @Component ( {
13+ selector : 'app-fancy-header-user-buttons' ,
14+ imports : [
15+ ButtonOrNavItemComponent ,
16+ FancyHeaderButtonsComponent ,
17+ ] ,
18+ template : `
19+ <ng-template #heartButtonTemplate let-templateHasText="hasText" let-templateIsNavItem="isNavItem">
20+ <app-button-or-navitem
21+ text="Heart"
22+ [icon]="faHeart"
23+ color="bg-pink"
24+
25+ textAlt="Unheart"
26+ [iconAlt]="faHeartCrack"
27+ colorAlt="bg-secondary"
28+
29+ [state]="relations.isHearted"
30+ (click)="heartButtonClick()"
31+
32+ [hasText]="templateHasText"
33+ [isNavItem]="templateIsNavItem">
34+ </app-button-or-navitem>
35+ </ng-template>
36+
37+ @if (buttonsInitialized) {
38+ <app-fancy-header-buttons
39+ [buttonTemplateRefs]="buttonTemplateRefs">
40+ </app-fancy-header-buttons>
41+ }
42+ ` ,
43+ styles : ``
44+ } )
45+
46+ export class FancyHeaderUserButtonsComponent {
47+ @Input ( { required : true } ) public targetUser : User = undefined ! ;
48+ @Input ( { required : true } ) public ownUser : ExtendedUser = undefined ! ;
49+ @Input ( { required : true } ) public relations : UserRelations = undefined ! ;
50+
51+ @ViewChild ( 'heartButtonTemplate' ) heartButtonTemplateRef ! : TemplateRef < any > ;
52+
53+ buttonTemplateRefs : TemplateRef < any > [ ] = [ ] ;
54+ buttonsInitialized : boolean = false ;
55+
56+ constructor ( private client : ClientService , private bannerService : BannerService ) { }
57+
58+ ngAfterViewInit ( ) {
59+ // Heart Button
60+ if ( this . ownUser . userId !== this . targetUser . userId ) {
61+ this . buttonTemplateRefs . push ( this . heartButtonTemplateRef ) ;
62+ }
63+
64+ // TODO: Other buttons, such as an edit button for mods
65+
66+ this . buttonsInitialized = true ;
67+ }
68+
69+ async heartButtonClick ( ) {
70+ if ( this . relations . isHearted ) {
71+ this . client . setUserAsUnhearted ( this . targetUser . userId ) . subscribe ( {
72+ error : error => {
73+ const apiError : RefreshApiError | undefined = error . error ?. error ;
74+ this . bannerService . warn ( "Failed to unheart user" , apiError == null ? error . message : apiError . message ) ;
75+ } ,
76+ next : _ => {
77+ this . relations . isHearted = false ;
78+ }
79+ } ) ;
80+ }
81+ else {
82+ this . client . setUserAsHearted ( this . targetUser . userId ) . subscribe ( {
83+ error : error => {
84+ const apiError : RefreshApiError | undefined = error . error ?. error ;
85+ this . bannerService . warn ( "Failed to heart user" , apiError == null ? error . message : apiError . message ) ;
86+ } ,
87+ next : _ => {
88+ this . relations . isHearted = true ;
89+ }
90+ } ) ;
91+ }
92+ }
93+
94+ protected readonly faHeart = faHeart ;
95+ protected readonly faHeartCrack = faHeartCrack ;
96+ }
0 commit comments