70834: Metadata schema component refactoring and caching issue fix #2

This commit is contained in:
Kristof De Langhe
2020-05-13 16:49:39 +02:00
parent cd46f33909
commit c97e3e0515
5 changed files with 34 additions and 10 deletions

View File

@@ -4,7 +4,7 @@ import { Observable, combineLatest as observableCombineLatest } from 'rxjs';
import { RemoteData } from '../../../core/data/remote-data';
import { PaginatedList } from '../../../core/data/paginated-list';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { map, take } from 'rxjs/operators';
import { filter, map, switchMap, take } from 'rxjs/operators';
import { hasValue } from '../../../shared/empty.util';
import { RestResponse } from '../../../core/cache/response.models';
import { zip } from 'rxjs/internal/observable/zip';
@@ -13,6 +13,7 @@ import { Route, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { MetadataSchema } from '../../../core/metadata/metadata-schema.model';
import { toFindListOptions } from '../../../shared/pagination/pagination.utils';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
@Component({
selector: 'ds-metadata-registry',
@@ -38,6 +39,11 @@ export class MetadataRegistryComponent {
pageSize: 25
});
/**
* Whether or not the list of MetadataSchemas needs an update
*/
needsUpdate$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
constructor(private registryService: RegistryService,
private notificationsService: NotificationsService,
private router: Router,
@@ -51,14 +57,17 @@ export class MetadataRegistryComponent {
*/
onPageChange(event) {
this.config.currentPage = event;
this.updateSchemas();
this.forceUpdateSchemas();
}
/**
* Update the list of schemas by fetching it from the rest api or cache
*/
private updateSchemas() {
this.metadataSchemas = this.registryService.getMetadataSchemas(toFindListOptions(this.config));
this.metadataSchemas = this.needsUpdate$.pipe(
filter((update) => update === true),
switchMap(() => this.registryService.getMetadataSchemas(toFindListOptions(this.config)))
);
}
/**
@@ -66,8 +75,7 @@ export class MetadataRegistryComponent {
* a new REST call
*/
public forceUpdateSchemas() {
this.registryService.clearMetadataSchemaRequests().subscribe();
this.updateSchemas();
this.needsUpdate$.next(true);
}
/**
@@ -126,6 +134,7 @@ export class MetadataRegistryComponent {
* Delete all the selected metadata schemas
*/
deleteSchemas() {
this.registryService.clearMetadataSchemaRequests().subscribe();
this.registryService.getSelectedMetadataSchemas().pipe(take(1)).subscribe(
(schemas) => {
const tasks$ = [];

View File

@@ -128,6 +128,7 @@ export class MetadataSchemaFormComponent implements OnInit, OnDestroy {
* Emit the updated/created schema using the EventEmitter submitForm
*/
onSubmit() {
this.registryService.clearMetadataSchemaRequests().subscribe();
this.registryService.getActiveMetadataSchema().pipe(take(1)).subscribe(
(schema) => {
const values = {
@@ -148,6 +149,7 @@ export class MetadataSchemaFormComponent implements OnInit, OnDestroy {
});
}
this.clearFields();
this.registryService.cancelEditMetadataSchema();
}
);
}

View File

@@ -153,6 +153,7 @@ export class MetadataFieldFormComponent implements OnInit, OnDestroy {
* Emit the updated/created field using the EventEmitter submitForm
*/
onSubmit() {
this.registryService.clearMetadataFieldRequests().subscribe();
this.registryService.getActiveMetadataField().pipe(take(1)).subscribe(
(field) => {
const values = {

View File

@@ -50,7 +50,7 @@ export class MetadataSchemaComponent implements OnInit {
/**
* Whether or not the list of MetadataFields needs an update
*/
needsUpdate: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
needsUpdate$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
constructor(private registryService: RegistryService,
private route: ActivatedRoute,
@@ -81,17 +81,16 @@ export class MetadataSchemaComponent implements OnInit {
*/
onPageChange(event) {
this.config.currentPage = event;
this.needsUpdate.next(true);
this.forceUpdateFields();
}
/**
* Update the list of fields by fetching it from the rest api or cache
*/
private updateFields() {
this.metadataFields$ = combineLatest(this.metadataSchema$, this.needsUpdate).pipe(
this.metadataFields$ = combineLatest(this.metadataSchema$, this.needsUpdate$).pipe(
switchMap(([schema, update]: [MetadataSchema, boolean]) => {
if (update) {
console.log('reloaded list');
return this.registryService.getMetadataFieldsBySchema(schema, toFindListOptions(this.config));
}
})
@@ -103,7 +102,7 @@ export class MetadataSchemaComponent implements OnInit {
* a new REST call
*/
public forceUpdateFields() {
this.registryService.clearMetadataFieldRequests().subscribe(() => this.needsUpdate.next(true));
this.needsUpdate$.next(true);
}
/**
@@ -162,6 +161,7 @@ export class MetadataSchemaComponent implements OnInit {
* Delete all the selected metadata fields
*/
deleteFields() {
this.registryService.clearMetadataFieldRequests().subscribe();
this.registryService.getSelectedMetadataFields().pipe(take(1)).subscribe(
(fields) => {
const tasks$ = [];

View File

@@ -42,6 +42,14 @@ export class MetadataSchemaDataService extends DataService<MetadataSchema> {
super();
}
/**
* Create or Update a MetadataSchema
* If the MetadataSchema contains an id, it is assumed the schema already exists and is updated instead
* Since creating or updating is nearly identical, the only real difference is the request (and slight difference in endpoint):
* - On creation, a CreateMetadataSchemaRequest is used
* - On update, a UpdateMetadataSchemaRequest is used
* @param schema The MetadataSchema to create or update
*/
createOrUpdateMetadataSchema(schema: MetadataSchema): Observable<RestResponse> {
const isUpdate = hasValue(schema.id);
const requestId = this.requestService.generateRequestId();
@@ -89,6 +97,10 @@ export class MetadataSchemaDataService extends DataService<MetadataSchema> {
);
}
/**
* Clear all metadata schema requests
* Used for refreshing lists after adding/updating/removing a metadata schema in the registry
*/
clearRequests(): Observable<string> {
return this.getBrowseEndpoint().pipe(
tap((href: string) => this.requestService.removeByHrefSubstring(href))