Skip to content

Commit

Permalink
refactor: split resource- and resource-type-edit-page
Browse files Browse the repository at this point in the history
  • Loading branch information
StephGit committed Dec 23, 2024
1 parent f0ffa1e commit 6d89c35
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<app-loading-indicator [isLoading]="isLoading()"></app-loading-indicator>
<app-page>
<div class="page-title">Edit {{ this.resource()?.name || this.resourceType()?.name }}</div>
<div class="page-title">Edit {{ this.resource()?.name }}</div>

<div class="page-content">
@if ((this.resource()?.name && !permissions().canEditResource) || this.resourceType()?.name &&
!permissions().canEditResourceType) {
@if ((this.resource()?.name && !permissions().canEditResource)) {
<div class="container">
<span class="text-warning-2">Not Authorized! You are not allowed to Edit-Resources.</span>
</div>
} @else if (id() === 0 && typeId() === 0) {
} @else if (id() === 0) {
<div class="container">
<span>Please provide a resource-id to edit a resource.</span>
</div>
} @else {
<div class="container">
Loaded resource:
{{ this.resource()?.name || this.resourceType()?.name }}
{{ this.resource()?.name }}
</div>
<div>
<app-tile-component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ResourceType } from '../../resource/resource-type';
import { ResourceTypesService } from '../../resource/resource-types.service';

@Component({
selector: 'app-resources-edit-page',
selector: 'app-resource-edit-page',
standalone: true,
imports: [LoadingIndicatorComponent, PageComponent, TileComponent],
templateUrl: './resource-edit-page.component.html',
Expand All @@ -23,35 +23,26 @@ export class ResourceEditPageComponent {
private authService = inject(AuthService);
private modalService = inject(NgbModal);
private resourceService = inject(ResourceService);
private resourceTypeService = inject(ResourceTypesService);
private route = inject(ActivatedRoute);

id = toSignal(this.route.queryParamMap.pipe(map((params) => Number(params.get('id')))), { initialValue: 0 });
typeId = toSignal(this.route.queryParamMap.pipe(map((params) => Number(params.get('resTypeId')))), {
initialValue: 0,
});
contextId = toSignal(this.route.queryParamMap.pipe(map((params) => Number(params.get('ctx')))), { initialValue: 1 });
resource: Signal<Resource> = this.resourceService.resource;
resourceType: Signal<ResourceType> = this.resourceTypeService.resourceType;

isLoading = computed(() => {
if (this.id()) {
this.resourceService.setIdForResource(this.id());
return false;
} else if (this.typeId()) {
this.resourceTypeService.setIdForResourceType(this.typeId());
return false;
} else return false;
});

permissions = computed(() => {
if (this.authService.restrictions().length > 0) {
return {
canEditResource: this.authService.hasPermission('RESOURCE', 'READ'),
canEditResourceType: this.authService.hasPermission('RESOURCETYPE', 'READ'),
};
} else {
return { canEditResource: false, canEditResourceType: false };
return { canEditResource: false };
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<app-loading-indicator [isLoading]="isLoading()"></app-loading-indicator>
<app-page>
<div class="page-title">Edit {{ this.resourceType()?.name }}</div>

<div class="page-content">
@if (this.resourceType()?.name && !permissions().canEditResourceType) {
<div class="container">
<span class="text-warning-2">Not Authorized! You are not allowed to Edit-ResourceTypes.</span>
</div>
} @else if (id() === 0 ) {
<div class="container">
<span>Please provide a resourceType-id to edit a resource.</span>
</div>
} @else {
<div class="container">
Loaded resourceType:
{{ this.resourceType()?.name }}
</div>
<div>
<app-tile-component
[title]="'Templates'"
[actionName]="'New Template'"
[canAction]="true"
[isVisible]="false"
[lists]="templatesData()"
(tileAction)="add()"
(listAction)="doListAction($event)"
></app-tile-component>
</div>
}
</div>
</app-page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { ResourceTypeEditPageComponent } from './resource-type-edit-page.component';
import { ActivatedRoute } from '@angular/router';
import { of, Subject } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';

describe('ResourceEditPageComponent', () => {
let component: ResourceTypeEditPageComponent;
let fixture: ComponentFixture<ResourceTypeEditPageComponent>;

const mockRoute: any = { queryParamMap: of() };
mockRoute.queryParamMap = new Subject<Map<string, number>>();
mockRoute.queryParamMap.next({
id: 42,
});

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ResourceTypeEditPageComponent, RouterTestingModule.withRoutes([])],
providers: [
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
{ provide: ActivatedRoute, useValue: mockRoute },
],
}).compileComponents();

fixture = TestBed.createComponent(ResourceTypeEditPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Component, computed, inject, Signal, signal } from '@angular/core';
import { LoadingIndicatorComponent } from '../../shared/elements/loading-indicator.component';
import { PageComponent } from '../../layout/page/page.component';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs/operators';
import { toSignal } from '@angular/core/rxjs-interop';
import { ResourceService } from '../../resource/resource.service';
import { Resource } from '../../resource/resource';
import { EntryAction, TileListEntry, TileListEntryOutput } from '../../shared/tile/tile-list/tile-list.component';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { TileComponent } from '../../shared/tile/tile.component';
import { AuthService } from '../../auth/auth.service';
import { ResourceType } from '../../resource/resource-type';
import { ResourceTypesService } from '../../resource/resource-types.service';

@Component({
selector: 'app-resource-type-edit-page',
standalone: true,
imports: [LoadingIndicatorComponent, PageComponent, TileComponent],
templateUrl: './resource-type-edit-page.component.html',
})
export class ResourceTypeEditPageComponent {
private authService = inject(AuthService);
private modalService = inject(NgbModal);
private resourceTypeService = inject(ResourceTypesService);
private route = inject(ActivatedRoute);

id = toSignal(this.route.queryParamMap.pipe(map((params) => Number(params.get('id')))), { initialValue: 0 });
contextId = toSignal(this.route.queryParamMap.pipe(map((params) => Number(params.get('ctx')))), { initialValue: 1 });
resourceType: Signal<ResourceType> = this.resourceTypeService.resourceType;

isLoading = computed(() => {
if (this.id()) {
this.resourceTypeService.setIdForResourceType(this.id());
return false;
} else return false;
});

permissions = computed(() => {
if (this.authService.restrictions().length > 0) {
return {
canEditResourceType: this.authService.hasPermission('RESOURCETYPE', 'READ'),
};
} else {
return { canEditResourceType: false };
}
});

templatesData = signal([
{
title: 'Instance Templates',
entries: [
{ name: 'startJob_0.sh', description: 'startJob_0.sh', id: 0 },
{ name: 'startJob_1.sh', description: 'job 2 again', id: 1 },
] as TileListEntry[],
canEdit: true,
canDelete: true,
},
{
title: 'Resource Type Templates',
entries: [{ name: 'seg', description: 'segmentation', id: 666 }] as TileListEntry[],
canOverwrite: false,
},
]);

add() {
this.modalService.open('This would open a modal to add something');
}

doListAction($event: TileListEntryOutput) {
switch ($event.action) {
case EntryAction.edit:
this.editFunction($event.id);
return;
case EntryAction.delete:
this.deleteFunction($event.id);
return;
case EntryAction.overwrite:
this.overwriteFunction($event.id);
return;
}
}

private editFunction(id: number) {
this.modalService.open('This would open a modal to edit function with id:' + id);
}

private deleteFunction(id: number) {
this.modalService.open('This would open a modal to delete function with id:' + id);
}

private overwriteFunction(id: number) {
this.modalService.open('This would open a modal to overwrite function with id:' + id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ <h1>
<app-icon icon="pencil"></app-icon>
</app-button>
</a>
<a [routerLink]="['/resourceType/edit']" [queryParams]="{ ctx: 1, id: resourceType().id }">
<app-button [variant]="'link'">
<app-icon icon="rocket-takeoff"></app-icon>
</app-button>
</a>
@if (permissions().canDeleteResourceType && !resourceType().isDefaultResourceType) {
<app-button [variant]="'link'" [additionalClasses]="'link-danger'" (click)="deleteResourceType()">
<app-icon icon="trash"></app-icon>
Expand Down
2 changes: 2 additions & 0 deletions AMW_angular/io/src/app/resources/resources.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ResourcesPageComponent } from './resources-page.component';
import { ResourceEditPageComponent } from './resource-edit-page/resource-edit-page.component';
import { ResourceTypeEditPageComponent } from './resource-type-edit-page/resource-type-edit-page.component';

export const resourcesRoute = [
{ path: 'resources', component: ResourcesPageComponent },
{ path: 'resource/edit', component: ResourceEditPageComponent },
{ path: 'resourceType/edit', component: ResourceTypeEditPageComponent },
];

0 comments on commit 6d89c35

Please sign in to comment.