Skip to content

Commit

Permalink
Correct change detection cycle for adding a widget
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Apr 10, 2024
1 parent 079aa7c commit 99ccf16
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class GridAddWidgetService {
&& this.isAllowed;
}

public widget(area:GridArea) {
this
public widget(area:GridArea):Promise<GridWidgetResource|null> {
return this
.select(area)
.then((widgetResource) => {
if (this.layout.isGap(area)) {
Expand All @@ -46,10 +46,9 @@ export class GridAddWidgetService {
this.setMaxWidth(newArea);

this.persist(newArea);
return widgetResource;
})
.catch(() => {
// user didn't select a widget
});
.catch(() => null);
}

public get addText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<div class="grid--widget-add"
*ngIf="add.isAllowed"
[title]="add.addText"
(click)="add.widget(area)">
(click)="addWidget(area)">
</div>
</div>

Expand All @@ -87,7 +87,7 @@
<div class="grid--widget-add -gap"
*ngIf="add.isAllowed"
[title]="add.addText"
(click)="add.widget(gap)">
(click)="addWidget(gap)">
</div>
</div>

Expand Down
15 changes: 11 additions & 4 deletions frontend/src/app/shared/components/grids/grid/grid.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
Component, ComponentRef, HostListener, Input, OnDestroy, OnInit,
} from '@angular/core';
import { ChangeDetectorRef, Component, ComponentRef, HostListener, Input, OnDestroy, OnInit } from '@angular/core';
import { GridResource } from 'core-app/features/hal/resources/grid-resource';
import { DomSanitizer } from '@angular/platform-browser';
import { GridWidgetsService } from 'core-app/shared/components/grids/widgets/widgets.service';
Expand Down Expand Up @@ -54,7 +52,9 @@ export class GridComponent implements OnDestroy, OnInit {
public layout:GridAreaService,
public add:GridAddWidgetService,
public remove:GridRemoveWidgetService,
readonly browserDetector:BrowserDetector) {
readonly browserDetector:BrowserDetector,
readonly cdRef:ChangeDetectorRef,
) {
}

ngOnInit() {
Expand All @@ -76,6 +76,13 @@ export class GridComponent implements OnDestroy, OnInit {
}
}

public addWidget(area:GridWidgetArea|GridArea) {
void this
.add
.widget(area)
.then(() => this.cdRef.detectChanges());
}

public widgetComponent(area:GridWidgetArea) {
const { widget } = area;

Expand Down

0 comments on commit 99ccf16

Please sign in to comment.