Commit 4a4a747301c05039b3f2d22f4d0986a880ac4b51

Authored by Vladyslav_Prykhodko
1 parent 77eafb14

UI: Refactoring resource library

... ... @@ -70,12 +70,12 @@ export class ResourceService {
70 70 );
71 71 }
72 72
73   - public saveResources(resources: Resource[], config?: RequestConfig) {
  73 + public saveResources(resources: Resource[], config?: RequestConfig): Observable<Resource[]> {
74 74 let partSize = 100;
75 75 partSize = resources.length > partSize ? partSize : resources.length;
76   - const resourceObservables = [];
  76 + const resourceObservables: Observable<Resource>[] = [];
77 77 for (let i = 0; i < partSize; i++) {
78   - resourceObservables.push(this.saveResource(resources[i], config).pipe(catchError(error => of(error))));
  78 + resourceObservables.push(this.saveResource(resources[i], config).pipe(catchError(() => of({} as Resource))));
79 79 }
80 80 return forkJoin(resourceObservables).pipe(
81 81 mergeMap((resource) => {
... ...
... ... @@ -24,7 +24,6 @@ import {
24 24 import { Resolve } from '@angular/router';
25 25 import { Resource, ResourceInfo, ResourceTypeTranslationMap } from '@shared/models/resource.models';
26 26 import { EntityType, entityTypeResources, entityTypeTranslations } from '@shared/models/entity-type.models';
27   -import { Direction } from '@shared/models/page/sort-order';
28 27 import { NULL_UUID } from '@shared/models/id/has-uuid';
29 28 import { DatePipe } from '@angular/common';
30 29 import { TranslateService } from '@ngx-translate/core';
... ... @@ -34,13 +33,14 @@ import { Store } from '@ngrx/store';
34 33 import { AppState } from '@core/core.state';
35 34 import { Authority } from '@shared/models/authority.enum';
36 35 import { ResourcesLibraryComponent } from '@home/pages/resource/resources-library.component';
37   -import { Observable } from 'rxjs/internal/Observable';
38   -import { PageData } from '@shared/models/page/page-data';
  36 +import { PageLink } from '@shared/models/page/page-link';
  37 +import { EntityAction } from '@home/models/entity/entity-component.models';
  38 +import { map } from 'rxjs/operators';
39 39
40 40 @Injectable()
41   -export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableConfig<Resource>> {
  41 +export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableConfig<Resource, PageLink, ResourceInfo>> {
42 42
43   - private readonly config: EntityTableConfig<Resource> = new EntityTableConfig<Resource>();
  43 + private readonly config: EntityTableConfig<Resource, PageLink, ResourceInfo> = new EntityTableConfig<Resource, PageLink, ResourceInfo>();
44 44 private readonly resourceTypesTranslationMap = ResourceTypeTranslationMap;
45 45
46 46 constructor(private store: Store<AppState>,
... ... @@ -52,17 +52,16 @@ export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableC
52 52 this.config.entityComponent = ResourcesLibraryComponent;
53 53 this.config.entityTranslations = entityTypeTranslations.get(EntityType.TB_RESOURCE);
54 54 this.config.entityResources = entityTypeResources.get(EntityType.TB_RESOURCE);
55   - this.config.defaultSortOrder = {property: 'title', direction: Direction.ASC};
56 55
57 56 this.config.entityTitle = (resource) => resource ?
58 57 resource.title : '';
59 58
60 59 this.config.columns.push(
61 60 new DateEntityTableColumn<ResourceInfo>('createdTime', 'common.created-time', this.datePipe, '150px'),
62   - new EntityTableColumn<ResourceInfo>('title', 'widgets-bundle.title', '60%'),
  61 + new EntityTableColumn<ResourceInfo>('title', 'resource.title', '60%'),
63 62 new EntityTableColumn<ResourceInfo>('resourceType', 'resource.resource-type', '40%',
64 63 entity => this.resourceTypesTranslationMap.get(entity.resourceType)),
65   - new EntityTableColumn<ResourceInfo>('tenantId', 'widgets-bundle.system', '60px',
  64 + new EntityTableColumn<ResourceInfo>('tenantId', 'resource.system', '60px',
66 65 entity => {
67 66 return checkBoxCell(entity.tenantId.id === NULL_UUID);
68 67 }),
... ... @@ -83,10 +82,12 @@ export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableC
83 82 this.config.deleteEntitiesTitle = count => this.translate.instant('resource.delete-resources-title', {count});
84 83 this.config.deleteEntitiesContent = () => this.translate.instant('resource.delete-resources-text');
85 84
86   - this.config.entitiesFetchFunction = pageLink => this.resourceService.getResources(pageLink) as Observable<PageData<Resource>>;
  85 + this.config.entitiesFetchFunction = pageLink => this.resourceService.getResources(pageLink);
87 86 this.config.loadEntity = id => this.resourceService.getResource(id.id);
88 87 this.config.saveEntity = resource => this.saveResource(resource);
89 88 this.config.deleteEntity = id => this.resourceService.deleteResource(id.id);
  89 +
  90 + this.config.onEntityAction = action => this.onResourceAction(action);
90 91 }
91 92
92 93 saveResource(resource) {
... ... @@ -100,13 +101,15 @@ export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableC
100 101 title: resource.title
101 102 });
102 103 });
103   - return this.resourceService.saveResources(resources, {resendRequest: true});
  104 + return this.resourceService.saveResources(resources, {resendRequest: true}).pipe(
  105 + map((response) => response[0])
  106 + );
104 107 } else {
105 108 return this.resourceService.saveResource(resource);
106 109 }
107 110 }
108 111
109   - resolve(): EntityTableConfig<Resource> {
  112 + resolve(): EntityTableConfig<Resource, PageLink, ResourceInfo> {
110 113 this.config.tableTitle = this.translate.instant('resource.resources-library');
111 114 const authUser = getCurrentAuthUser(this.store);
112 115 this.config.deleteEnabled = (resource) => this.isResourceEditable(resource, authUser.authority);
... ... @@ -122,7 +125,16 @@ export class ResourcesLibraryTableConfigResolver implements Resolve<EntityTableC
122 125 this.resourceService.downloadResource(resource.id.id).subscribe();
123 126 }
124 127
125   - private isResourceEditable(resource: Resource, authority: Authority): boolean {
  128 + onResourceAction(action: EntityAction<ResourceInfo>): boolean {
  129 + switch (action.action) {
  130 + case 'uploadResource':
  131 + this.exportResource(action.event, action.entity);
  132 + return true;
  133 + }
  134 + return false;
  135 + }
  136 +
  137 + private isResourceEditable(resource: ResourceInfo, authority: Authority): boolean {
126 138 if (authority === Authority.TENANT_ADMIN) {
127 139 return resource && resource.tenantId && resource.tenantId.id !== NULL_UUID;
128 140 } else {
... ...
... ... @@ -18,6 +18,12 @@
18 18 <div class="tb-details-buttons" fxLayout.xs="column">
19 19 <button mat-raised-button color="primary" fxFlex.xs
20 20 [disabled]="(isLoading$ | async)"
  21 + (click)="onEntityAction($event, 'uploadResource')"
  22 + [fxShow]="!isEdit">
  23 + {{'resource.export' | translate }}
  24 + </button>
  25 + <button mat-raised-button color="primary" fxFlex.xs
  26 + [disabled]="(isLoading$ | async)"
21 27 (click)="onEntityAction($event, 'delete')"
22 28 [fxShow]="!hideDelete() && !isEdit">
23 29 {{'resource.delete' | translate }}
... ...