Skip to content

Commit f34176e

Browse files
committed
Register function to edit card after template rendering
Signed-off-by: Giovanni Ferrari <[email protected]>
1 parent 03ce8fc commit f34176e

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

ui/main/src/app/modules/card/components/card-actions/card-actions.component.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
* This file is part of the OperatorFabric project.
88
*/
99

10-
import {Component, EventEmitter, Input, OnChanges, OnDestroy, Output, TemplateRef, ViewChild} from '@angular/core';
10+
import {
11+
Component,
12+
EventEmitter,
13+
Input,
14+
OnChanges,
15+
OnDestroy,
16+
OnInit,
17+
Output,
18+
TemplateRef,
19+
ViewChild
20+
} from '@angular/core';
1121
import {NgbModal, NgbModalOptions, NgbModalRef} from '@ng-bootstrap/ng-bootstrap';
1222
import {Card} from '@ofModel/card.model';
1323
import {MessageLevel} from '@ofModel/message.model';
@@ -38,10 +48,11 @@ import {CardTemplateGateway} from '@ofServices/templateGateway/CardTemplateGatew
3848
standalone: true,
3949
imports: [NgIf, TranslateModule, UserCardComponent, SpinnerComponent]
4050
})
41-
export class CardActionsComponent implements OnChanges, OnDestroy {
51+
export class CardActionsComponent implements OnInit, OnChanges, OnDestroy {
4252
@Input() card: Card;
4353
@Input() cardState: State;
4454
@Input() parentModalRef: NgbModalRef;
55+
@Input() templateInitialized: EventEmitter<void>;
4556

4657
@Output() closeCardDetail: EventEmitter<boolean> = new EventEmitter<boolean>();
4758

@@ -62,21 +73,15 @@ export class CardActionsComponent implements OnChanges, OnDestroy {
6273
private readonly router: Router
6374
) {}
6475

65-
ngOnChanges(): void {
66-
// We need to register this function each time we have a change
67-
// as this change can trigger a rendering of the card which in turn
68-
// initialize the template gateway and unregister the function.
69-
// This initialization is done in the template rendering compornent
70-
// This is a temporary fix to avoid the issue
71-
//
72-
// TODO : find a better way to handle this as this solution works only because the ngOnchanges of
73-
// the card-actions component is called after the ngOnchanges of the template-rendering Component
74-
// which is not guaranteed to be the case in the future as it depends of angular change detection mechanism
75-
//
76-
CardTemplateGateway.registerFunctionToEditCard(() => {
77-
if (this.doesTheUserHavePermissionToEditCard()) this.editCard();
78-
});
76+
ngOnInit(): void {
77+
this.templateInitialized.subscribe(() =>
78+
CardTemplateGateway.registerFunctionToEditCard(() => {
79+
if (this.doesTheUserHavePermissionToEditCard()) this.editCard();
80+
})
81+
);
82+
}
7983

84+
ngOnChanges(): void {
8085
this.setButtonsVisibility();
8186
}
8287

ui/main/src/app/modules/card/components/card-body/card-body.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
[card]="this.card"
1818
[cardState]="this.cardState"
1919
[parentModalRef]="this.parentModalRef"
20+
[templateInitialized]="this.templateInitialized"
2021
(closeCardDetail)="closeDetails()">
2122
</of-card-actions>
2223
<div

ui/main/src/app/modules/card/components/card-body/card-body.component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class CardBodyComponent implements OnChanges, OnInit, OnDestroy {
8282

8383
@Output() closeCardDetail: EventEmitter<boolean> = new EventEmitter<boolean>();
8484

85+
public templateInitialized: EventEmitter<void> = new EventEmitter<void>();
86+
8587
public displayContext: DisplayContext = DisplayContext.REALTIME;
8688
public isUserEnabledToRespond = false;
8789
public lttdExpiredIsTrue: boolean;
@@ -366,6 +368,7 @@ export class CardBodyComponent implements OnChanges, OnInit, OnDestroy {
366368

367369
public afterTemplateRendering() {
368370
if (this.isResponseLocked) CardTemplateGateway.sendResponseLockToTemplate();
371+
this.templateInitialized.emit();
369372
this.startRegularlyCheckLttd();
370373
}
371374

0 commit comments

Comments
 (0)