Skip to content

Commit

Permalink
Panel is compatible with observable model #9844
Browse files Browse the repository at this point in the history
If `model` is found in the resolved data of a panel, then it will be
automatically subscribed to and `this.data` will be updated whenever it
emits.
  • Loading branch information
PowerKiKi committed Mar 23, 2024
1 parent b34cc51 commit 6d77f49
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 20 additions & 2 deletions projects/natural/src/lib/modules/panels/abstract-panel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Directive, HostBinding, HostListener} from '@angular/core';
import {merge} from 'lodash-es';
import {NaturalAbstractController} from '../../classes/abstract-controller';
import {NaturalPanelsService} from './panels.service';
import {NaturalPanelData} from './types';
import {Observable, Subscription, takeUntil} from 'rxjs';

@Directive()
export class NaturalAbstractPanel extends NaturalAbstractController {
Expand All @@ -11,6 +11,7 @@ export class NaturalAbstractPanel extends NaturalAbstractController {
* When loading a component from a panel opening (dialog), receives the data provided by the service
*/
public data: any = {};
#modelSubscription: Subscription | null = null;

/**
* Bind isFrontPanel style class on root component
Expand Down Expand Up @@ -48,7 +49,24 @@ export class NaturalAbstractPanel extends NaturalAbstractController {
this.isPanel = true;

if (this.panelData?.data) {
merge(this.data, this.panelData.data);
if (this.panelData.data.model instanceof Observable) {
// Subscribe to model to know when Apollo cache is changed, so we can reflect it into `data.model`
this.#modelSubscription?.unsubscribe();
this.#modelSubscription = this.panelData.data.model
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(model => {
this.data = {
...this.data,
...this.panelData?.data,
model: model,
};
});
} else {
this.data = {
...this.data,
...this.panelData.data,
};
}
}
}
}
9 changes: 2 additions & 7 deletions projects/natural/src/lib/modules/panels/panels.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
import {ActivatedRoute, DefaultUrlSerializer, NavigationError, Router, UrlSegment} from '@angular/router';
import {differenceWith, flatten, isEqual} from 'lodash-es';
import {forkJoin, Observable, of, Subject, Subscription} from 'rxjs';
import {map} from 'rxjs/operators';
import {NaturalAbstractPanel} from './abstract-panel';
import {getStackConfig} from './panels.urlmatcher';
import {
Expand Down Expand Up @@ -280,7 +279,7 @@ export class NaturalPanelsService {
const subject = new Subject<Observable<any> | null>();

// Resolve everything before opening a single panel
const resolves = newItemsConfig.map((conf: NaturalPanelConfig) => this.getResolvedData(conf));
const resolves = newItemsConfig.map(conf => this.getResolvedData(conf));

// ForkJoin emits when all promises are executed;
forkJoin(resolves).subscribe(resolvedResult => {
Expand Down Expand Up @@ -334,11 +333,7 @@ export class NaturalPanelsService {
});
}

return forkJoin(resolvedData).pipe(
map(result => {
return (result as any).model || result;
}),
);
return forkJoin(resolvedData);
}

private openPanel(componentOrTemplateRef: ComponentType<NaturalAbstractPanel>, data: NaturalPanelData): void {
Expand Down

0 comments on commit 6d77f49

Please sign in to comment.