Commit 18113b78 authored by LAVENIER's avatar LAVENIER
Browse files

[fix] Entity editor: allow updateView() and setValue() to be async

parent 309e0339
......@@ -134,7 +134,6 @@ export abstract class AppEntityEditor<
enableListenChanges: (injector.get(ENVIRONMENT).listenRemoteChanges === true),
pathIdAttribute: 'id',
autoLoad: true,
autoLoadDelay: 0,
autoUpdateRoute: true,
i18nPrefix: '',
......@@ -242,7 +241,7 @@ export abstract class AppEntityEditor<
const data = new this.dataType();
this._usageMode = this.computeUsageMode(data);
await this.onNewEntity(data, opts);
this.updateView(data, {
await this.updateView(data, {
openTabIndex: 0,
...opts
});
......@@ -256,7 +255,7 @@ export abstract class AppEntityEditor<
if (!data) throw {code: ErrorCodes.DATA_NOT_FOUND_ERROR, message: 'ERROR.DATA_NO_FOUND'};
this._usageMode = this.computeUsageMode(data);
await this.onEntityLoaded(data, opts);
this.updateView(data, opts);
await this.updateView(data, opts);
this.markAsLoaded({emitEvent: false});
this.startListenRemoteChanges();
}
......@@ -292,11 +291,11 @@ export abstract class AppEntityEditor<
);
}
updateView(data: T | null, opts?: {
async updateView(data: T | null, opts?: {
emitEvent?: boolean;
openTabIndex?: number;
updateRoute?: boolean;
}) {
}): Promise<void> {
const idChanged = isNotNil(data.id)
&& this.previousDataId !== undefined // Ignore if first loading (=undefined)
&& this.previousDataId !== data.id;
......@@ -310,7 +309,8 @@ export abstract class AppEntityEditor<
this.data = data;
this.previousDataId = data.id || null;
this.setValue(data);
const promiseOrVoid = this.setValue(data);
if (promiseOrVoid) await promiseOrVoid;
if (!opts || opts.emitEvent !== false) {
this.markAsPristine();
......@@ -597,7 +597,7 @@ export abstract class AppEntityEditor<
protected abstract computeTitle(data: T): Promise<string>;
protected abstract setValue(data: T);
protected abstract setValue(data: T): Promise<void> | void;
protected abstract get form(): FormGroup;
......@@ -616,6 +616,10 @@ export abstract class AppEntityEditor<
this.saving = false;
}
protected async onBeforeEntityLoad(): Promise<void> {
// can be overwrite by subclasses
}
protected async onNewEntity(data: T, options?: EntityServiceLoadOptions): Promise<void> {
// can be overwrite by subclasses
}
......
......@@ -29,6 +29,7 @@ export interface IAppForm {
markAsDirty(opts?: {onlySelf?: boolean; emitEvent?: boolean });
}
/**
* A form that do nothing
*/
......
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {ErrorCodes} from './errors';
import {FetchPolicy, InternalRefetchQueriesInclude, MutationUpdaterFn} from '@apollo/client/core';
import {SortDirection} from '@angular/material/sort';
import {BaseGraphqlService} from './base-graphql-service.class';
import {EntitiesServiceWatchOptions, EntityServiceLoadOptions, IEntitiesService, IEntityService, LoadResult} from '../../shared/services/entity-service.class';
import {GraphqlService} from '../graphql/graphql.service';
import {PlatformService} from './platform.service';
import {environment} from '../../../environments/environment';
import {Entity, EntityAsObjectOptions, EntityUtils} from './model/entity.model';
import {chainPromises} from '../../shared/observables';
import {isEmptyArray, isNil, isNotNil, removeEnd, toBoolean} from '../../shared/functions';
import {Directive} from '@angular/core';
import {FetchResult} from '@apollo/client/link/core';
import {EntityFilter, EntityFilterUtils} from './model/filter.model';
import {DocumentNode} from 'graphql';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ErrorCodes } from './errors';
import { FetchPolicy, InternalRefetchQueriesInclude, MutationUpdaterFn } from '@apollo/client/core';
import { SortDirection } from '@angular/material/sort';
import { BaseGraphqlService } from './base-graphql-service.class';
import { EntitiesServiceWatchOptions, EntityServiceLoadOptions, IEntitiesService, IEntityService, LoadResult } from '../../shared/services/entity-service.class';
import { GraphqlService } from '../graphql/graphql.service';
import { PlatformService } from './platform.service';
import { environment } from '../../../environments/environment';
import { Entity, EntityAsObjectOptions, EntityUtils } from './model/entity.model';
import { chainPromises } from '../../shared/observables';
import { isEmptyArray, isNil, isNotNil, removeEnd } from '../../shared/functions';
import { Directive } from '@angular/core';
import { FetchResult } from '@apollo/client/link/core';
import { EntityFilter, EntityFilterUtils } from './model/filter.model';
import { DocumentNode } from 'graphql';
export type MutableWatchQueriesUpdatePolicy = 'update-cache' | 'refetch-queries';
......
......@@ -121,11 +121,11 @@ export abstract class AppTable<
isRateLimitReached = false;
selection = new SelectionModel<TableElement<T>>(true, []);
editedRow: TableElement<T> = undefined;
settingsId: string;
autocompleteFields: {[key: string]: MatAutocompleteFieldConfig};
mobile: boolean;
// Table options
@Input() settingsId: string;
@Input() debug: boolean;
@Input() i18nColumnPrefix = 'COMMON.';
@Input() i18nColumnSuffix: string;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment