Skip to content

Commit

Permalink
fixed errors when expanding resources in state view (#562)
Browse files Browse the repository at this point in the history
* fixed errors when expanding resources in state view

If two resources had the same id, expanding one of them would cause an infinite loop.
Switched to using the resource address as the unique identifier to avoid duplicates.
  • Loading branch information
sei-aschlackman authored Apr 23, 2024
1 parent 71f6fee commit 455e66d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
27 changes: 8 additions & 19 deletions src/app/generated/caster-api/api/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,19 @@ export class ResourcesService {
/**
* Get a single resource in a Workspace.
* @param workspaceId
* @param id
* @param type Type of the Resource.
* @param address
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public getResource(workspaceId: string, id: string, type: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<Resource>;
public getResource(workspaceId: string, id: string, type: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<HttpResponse<Resource>>;
public getResource(workspaceId: string, id: string, type: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<HttpEvent<Resource>>;
public getResource(workspaceId: string, id: string, type: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<any> {
public getResource(workspaceId: string, address: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<Resource>;
public getResource(workspaceId: string, address: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<HttpResponse<Resource>>;
public getResource(workspaceId: string, address: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<HttpEvent<Resource>>;
public getResource(workspaceId: string, address: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json', context?: HttpContext}): Observable<any> {
if (workspaceId === null || workspaceId === undefined) {
throw new Error('Required parameter workspaceId was null or undefined when calling getResource.');
}
if (id === null || id === undefined) {
throw new Error('Required parameter id was null or undefined when calling getResource.');
}
if (type === null || type === undefined) {
throw new Error('Required parameter type was null or undefined when calling getResource.');
}

let localVarQueryParameters = new HttpParams({encoder: this.encoder});
if (type !== undefined && type !== null) {
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
<any>type, 'Type');
if (address === null || address === undefined) {
throw new Error('Required parameter address was null or undefined when calling getResource.');
}

let localVarHeaders = this.defaultHeaders;
Expand Down Expand Up @@ -174,11 +164,10 @@ export class ResourcesService {
}
}

let localVarPath = `/api/workspaces/${this.configuration.encodeParam({name: "workspaceId", value: workspaceId, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: "uuid"})}/resources/${this.configuration.encodeParam({name: "id", value: id, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: undefined})}`;
let localVarPath = `/api/workspaces/${this.configuration.encodeParam({name: "workspaceId", value: workspaceId, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: "uuid"})}/resources/${this.configuration.encodeParam({name: "address", value: address, in: "path", style: "simple", explode: false, dataType: "string", dataFormat: undefined})}`;
return this.httpClient.request<Resource>('get', `${this.configuration.basePath}${localVarPath}`,
{
context: localVarHttpContext,
params: localVarQueryParameters,
responseType: <any>responseType_,
withCredentials: this.configuration.withCredentials,
headers: localVarHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class CwdTableComponent<T> implements OnInit, OnChanges {
@Input() getRowStyle: (item: T) => {};
@Input() excludedAttributes: string[] = [];
@Input() trackByPropertyName: string;
@Input() idPropertyName = 'id';
@Output() expand: EventEmitter<{
expand: boolean;
item: T;
Expand Down Expand Up @@ -163,7 +164,9 @@ export class CwdTableComponent<T> implements OnInit, OnChanges {
}

isExpanded(item) {
return this.expandedItems ? this.expandedItems.includes(item.id) : false;
return this.expandedItems
? this.expandedItems.includes(item[this.idPropertyName])
: false;
}
/**
* function to emit the item when expanded or callapsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
[excludedAttributes]="['attributes']"
[expandedItems]="expandedResourceIds$ | async"
[getRowStyle]="setRowStyle"
idPropertyName="address"
(expand)="expandResource($event)"
>
<div
Expand Down Expand Up @@ -200,7 +201,7 @@
>
<ng-container
*ngIf="{
value: (resourceActionIds$ | async).includes(item.id),
value: (resourceActionIds$ | async).includes(item.address),
action: (resourceAction$ | async)
} as loading"
>
Expand Down Expand Up @@ -268,7 +269,7 @@
</div>
<div *cwdTableItemContent="let item">
<ng-container
*ngIf="(expandedResourceIds$ | async).includes(item.id)"
*ngIf="(expandedResourceIds$ | async).includes(item.address)"
>
<pre>{{ item | json }}</pre>
</ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class WorkspaceContainerComponent

expandResource(event) {
const { expand, item } = event;
this.workspaceService.updateResource(this.workspaceId, item);
this.workspaceService.loadResource(this.workspaceId, item);
this.workspaceService.expandResource(expand, this.workspaceId, item);
}

Expand Down
52 changes: 29 additions & 23 deletions src/app/workspace/state/workspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ export class WorkspaceService {
}));
}

private processResourceCommand(workspaceId: string) {
return tap(
(result: ResourceCommandResult) => {
this.finishResourceAction(workspaceId, result);
},
() => {
this.finishResourceAction(workspaceId, null);
}
);
}

taint(workspaceId: string, items: Resource | Resource[]) {
const resourceAddresses = this.startResourceAction(
workspaceId,
Expand All @@ -144,11 +155,7 @@ export class WorkspaceService {

return this.resourceService
.taintResources(workspaceId, { resourceAddresses })
.pipe(
tap((result: ResourceCommandResult) => {
this.finishResourceAction(workspaceId, result);
})
);
.pipe(this.processResourceCommand(workspaceId));
}

untaint(workspaceId: string, items: Resource | Resource[]) {
Expand All @@ -160,11 +167,7 @@ export class WorkspaceService {

return this.resourceService
.untaintResources(workspaceId, { resourceAddresses })
.pipe(
tap((result: ResourceCommandResult) => {
this.finishResourceAction(workspaceId, result);
})
);
.pipe(this.processResourceCommand(workspaceId));
}

remove(workspaceId: string, items: Resource | Resource[]) {
Expand All @@ -176,11 +179,7 @@ export class WorkspaceService {

return this.resourceService
.removeResources(workspaceId, { resourceAddresses })
.pipe(
tap((result: ResourceCommandResult) => {
this.finishResourceAction(workspaceId, result);
})
);
.pipe(this.processResourceCommand(workspaceId));
}

import(workspaceId: string, command: ImportResourceCommand) {
Expand Down Expand Up @@ -333,13 +332,18 @@ export class WorkspaceService {
);
}

updateResource(workspaceId: string, item: Resource): void {
loadResource(workspaceId: string, item: Resource): void {
this.resourceService
.getResource(workspaceId, item.id, item.type)
.getResource(workspaceId, item.address)
.pipe(
tap((resource) =>
this.workspaceStore.update(workspaceId, (entity) => ({
resources: arrayUpsert(entity.resources, item.id, resource),
resources: arrayUpsert(
entity.resources,
item.address,
resource,
'address'
),
}))
),
take(1)
Expand Down Expand Up @@ -385,17 +389,19 @@ export class WorkspaceService {
const itemsArray = coerceArray(items);

this.workspaceStore.ui.upsert(workspaceId, () => ({
resourceActions: itemsArray.map((i) => i.id),
resourceActions: itemsArray.map((i) => i.address),
resourceAction: action,
}));

return itemsArray.map((i) => i.address);
}

finishResourceAction(workspaceId: string, result: ResourceCommandResult) {
this.workspaceStore.upsert(workspaceId, () => ({
resources: result.resources,
}));
if (result) {
this.workspaceStore.upsert(workspaceId, () => ({
resources: result.resources,
}));
}

this.workspaceStore.ui.upsert(workspaceId, () => ({
resourceActions: [],
Expand All @@ -406,7 +412,7 @@ export class WorkspaceService {
expandResource(expand, workspaceId, resource) {
this.workspaceStore.ui.upsert(workspaceId, (w) => ({
expandedResources: isUpdate<WorkspaceEntityUi>(w)
? arrayToggle(w.expandedResources, resource.id)
? arrayToggle(w.expandedResources, resource.address)
: undefined,
}));
}
Expand Down

0 comments on commit 455e66d

Please sign in to comment.