MOBILE-3817 block: Detect changes in block input

main
Dani Palou 2022-07-21 11:04:39 +02:00
parent 63f3c440e3
commit ce9c086819
3 changed files with 14 additions and 39 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { Component, Input, OnInit, ViewChild, OnDestroy, DoCheck, KeyValueDiffers, KeyValueDiffer, Type } from '@angular/core'; import { Component, Input, ViewChild, OnDestroy, Type, OnChanges, SimpleChanges } from '@angular/core';
import { CoreBlockDelegate } from '../../services/block-delegate'; import { CoreBlockDelegate } from '../../services/block-delegate';
import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component'; import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
@ -27,7 +27,7 @@ import type { ICoreBlockComponent } from '@features/block/classes/base-block-com
templateUrl: 'core-block.html', templateUrl: 'core-block.html',
styleUrls: ['block.scss'], styleUrls: ['block.scss'],
}) })
export class CoreBlockComponent implements OnInit, OnDestroy, DoCheck { export class CoreBlockComponent implements OnChanges, OnDestroy {
@ViewChild(CoreDynamicComponent) dynamicComponent?: CoreDynamicComponent<ICoreBlockComponent>; @ViewChild(CoreDynamicComponent) dynamicComponent?: CoreDynamicComponent<ICoreBlockComponent>;
@ -40,52 +40,26 @@ export class CoreBlockComponent implements OnInit, OnDestroy, DoCheck {
data: Record<string, unknown> = {}; // Data to pass to the component. data: Record<string, unknown> = {}; // Data to pass to the component.
class?: string; // CSS class to apply to the block. class?: string; // CSS class to apply to the block.
loaded = false; loaded = false;
blockSubscription?: Subscription; blockSubscription?: Subscription;
protected differ: KeyValueDiffer<unknown, unknown>; // To detect changes in the data input.
constructor(
differs: KeyValueDiffers,
) {
this.differ = differs.find([]).create();
}
/** /**
* Component being initialized. * @inheritdoc
*/ */
ngOnInit(): void { ngOnChanges(changes: SimpleChanges): void {
if (!this.block) { if (changes.block && this.block?.visible) {
this.loaded = true; this.updateBlock();
return;
} }
if (this.block.visible) { if (this.data && changes.extraData) {
// Get the data to render the block. this.data = Object.assign(this.data, this.extraData || {});
this.initBlock();
} }
} }
/** /**
* Detect and act upon changes that Angular cant or wont detect on its own (objects and arrays). * Get block display data and initialises or updates the block. If the block is not supported at the moment, try again if the
* available blocks are updated (because it comes from a site plugin).
*/ */
ngDoCheck(): void { async updateBlock(): Promise<void> {
if (this.data) {
// Check if there's any change in the extraData object.
const changes = this.differ.diff(this.extraData);
if (changes) {
this.data = Object.assign(this.data, this.extraData || {});
}
}
}
/**
* Get block display data and initialises the block once this is available. If the block is not
* supported at the moment, try again if the available blocks are updated (because it comes
* from a site plugin).
*/
async initBlock(): Promise<void> {
try { try {
const data = await CoreBlockDelegate.getBlockDisplayData(this.block, this.contextLevel, this.instanceId); const data = await CoreBlockDelegate.getBlockDisplayData(this.block, this.contextLevel, this.instanceId);
@ -97,7 +71,7 @@ export class CoreBlockComponent implements OnInit, OnDestroy, DoCheck {
(): void => { (): void => {
this.blockSubscription?.unsubscribe(); this.blockSubscription?.unsubscribe();
delete this.blockSubscription; delete this.blockSubscription;
this.initBlock(); this.updateBlock();
}, },
); );

View File

@ -1,4 +1,4 @@
<!-- Only render the block if it's supported. --> <!-- Only render the block if it's supported. -->
<ion-card *ngIf="loaded && componentClass && block.visible" class="{{class}}"> <ion-card *ngIf="loaded && componentClass && block && block.visible" class="{{class}}">
<core-dynamic-component [component]="componentClass" [data]="data"></core-dynamic-component> <core-dynamic-component [component]="componentClass" [data]="data"></core-dynamic-component>
</ion-card> </ion-card>

View File

@ -6,6 +6,7 @@ information provided here is intended especially for developers.
- Zoom levels changed from "normal / low / high" to " none / medium / high". - Zoom levels changed from "normal / low / high" to " none / medium / high".
- --addon-messages-* CSS3 variables have been renamed to --core-messages-* - --addon-messages-* CSS3 variables have been renamed to --core-messages-*
- The database constants in CoreSite (WS_CACHE_TABLE, CONFIG_TABLE, LAST_VIEWED_TABLE) and the DBRecord types have been moved to src/core/services/database. - The database constants in CoreSite (WS_CACHE_TABLE, CONFIG_TABLE, LAST_VIEWED_TABLE) and the DBRecord types have been moved to src/core/services/database.
- The component <core-block> will no longer detect changes of properties inside the extraData object, it will only detect changes if the object itself changes.
=== 4.0.0 === === 4.0.0 ===