ocket8888 commented on code in PR #7486: URL: https://github.com/apache/trafficcontrol/pull/7486#discussion_r1186298119
########## experimental/traffic-portal/src/app/core/users/roles/table/roles-table.component.ts: ########## @@ -0,0 +1,112 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { Component, type OnInit } from "@angular/core"; +import { FormControl } from "@angular/forms"; +import {ActivatedRoute, type Params} from "@angular/router"; +import { BehaviorSubject } from "rxjs"; +import type { ResponseRole } from "trafficops-types"; + +import { UserService } from "src/app/api"; +import { CurrentUserService } from "src/app/shared/current-user/current-user.service"; +import type { ContextMenuActionEvent, ContextMenuItem } from "src/app/shared/generic-table/generic-table.component"; +import { NavigationService } from "src/app/shared/navigation/navigation.service"; +/** + * RolesTableComponent is the controller for the "Roles" table. + */ +@Component({ + selector: "tp-roles", + styleUrls: ["./roles-table.component.scss"], + templateUrl: "./roles-table.component.html" +}) +export class RolesTableComponent implements OnInit { + /** List of roles */ + public roles: Promise<Array<ResponseRole>>; + constructor(private readonly route: ActivatedRoute, private readonly headerSvc: NavigationService, + private readonly api: UserService, public readonly auth: CurrentUserService) { + this.fuzzySubject = new BehaviorSubject<string>(""); + this.roles = this.api.getRoles(); + this.headerSvc.headerTitle.next("Roles"); + } + + /** Initializes table data, loading it from Traffic Ops. */ + public ngOnInit(): void { + this.route.queryParamMap.subscribe( + m => { + const search = m.get("search"); + if (search) { + this.fuzzControl.setValue(decodeURIComponent(search)); + this.updateURL(); + } + }, + e => { + console.error("Failed to get query parameters:", e); + } + ); + } + + /** Definitions of the table's columns according to the ag-grid API */ + public columnDefs = [ + { + field: "name", + headerName: "Name" + }, + { + field: "description", + headerName: "Description", + }, + { + field: "lastUpdated", + headerName: "Last Updated" + } + ]; + + /** Definitions for the context menu items (which act on augmented roles data). */ + public contextMenuItems: Array<ContextMenuItem<ResponseRole>> = [ + { + href: (selectedRow: ResponseRole): string => `${selectedRow.name}`, + name: "View Role" + }, + { + href: (selectedRow: ResponseRole): string => `${selectedRow.name}`, + name: "Open in New Tab", + newTab: true + }, + { + href: "/core/users", + name: "View Users", + queryParams: (selectedRow: ResponseRole): Params => ({role: selectedRow.name}) + }, + ]; + + /** A subject that child components can subscribe to for access to the fuzzy search query text */ + public fuzzySubject: BehaviorSubject<string>; + + /** Form controller for the user search input. */ + public fuzzControl = new FormControl<string>(""); + + /** Update the URL's 'search' query parameter for the user's search input. */ + public updateURL(): void { + this.fuzzySubject.next(this.fuzzControl.value ?? ""); + } + + /** + * Handles a context menu event. + * + * @param a The action selected from the context menu. + */ + public handleContextMenu(a: ContextMenuActionEvent<Readonly<ResponseRole>>): void { + console.log("action:", a); + } Review Comment: Since this doesn't do anything, I think we can do without it ########## experimental/traffic-portal/src/app/core/users/roles/table/roles-table.component.spec.ts: ########## @@ -0,0 +1,68 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { ComponentFixture, fakeAsync, TestBed, tick } from "@angular/core/testing"; +import { MatDialogModule } from "@angular/material/dialog"; +import { RouterTestingModule } from "@angular/router/testing"; + +import { APITestingModule } from "src/app/api/testing"; +import { RolesTableComponent } from "src/app/core/users/roles/table/roles-table.component"; + +describe("RolesTableComponent", () => { + let component: RolesTableComponent; + let fixture: ComponentFixture<RolesTableComponent>; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ RolesTableComponent ], + imports: [ APITestingModule, RouterTestingModule, MatDialogModule ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(RolesTableComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it("should create", () => { + expect(component).toBeTruthy(); + }); + + it("updates the fuzzy search output", fakeAsync(() => { + let called = false; + const text = "testquest"; + const spy = jasmine.createSpy("subscriber", (txt: string): void =>{ + if (!called) { + expect(txt).toBe(""); + called = true; + } else { + expect(txt).toBe(text); + } + }); + component.fuzzySubject.subscribe(spy); + tick(); + expect(spy).toHaveBeenCalled(); + component.fuzzControl.setValue(text); + component.updateURL(); + tick(); + expect(spy).toHaveBeenCalledTimes(2); + })); + + it("handles contextmenu events", () => { + expect(async () => component.handleContextMenu({ + action: component.contextMenuItems[0].name, + data: {description: "Can only read", lastUpdated: new Date(), name: "test"} + })).not.toThrow(); + }); +}); Review Comment: I'd love it if you could add tests for correct construction of context menu links ########## experimental/traffic-portal/src/app/core/users/roles/table/roles-table.component.ts: ########## @@ -0,0 +1,112 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { Component, type OnInit } from "@angular/core"; +import { FormControl } from "@angular/forms"; +import {ActivatedRoute, type Params} from "@angular/router"; +import { BehaviorSubject } from "rxjs"; +import type { ResponseRole } from "trafficops-types"; + +import { UserService } from "src/app/api"; +import { CurrentUserService } from "src/app/shared/current-user/current-user.service"; +import type { ContextMenuActionEvent, ContextMenuItem } from "src/app/shared/generic-table/generic-table.component"; +import { NavigationService } from "src/app/shared/navigation/navigation.service"; +/** + * RolesTableComponent is the controller for the "Roles" table. + */ +@Component({ + selector: "tp-roles", + styleUrls: ["./roles-table.component.scss"], + templateUrl: "./roles-table.component.html" +}) +export class RolesTableComponent implements OnInit { + /** List of roles */ + public roles: Promise<Array<ResponseRole>>; + constructor(private readonly route: ActivatedRoute, private readonly headerSvc: NavigationService, + private readonly api: UserService, public readonly auth: CurrentUserService) { + this.fuzzySubject = new BehaviorSubject<string>(""); + this.roles = this.api.getRoles(); + this.headerSvc.headerTitle.next("Roles"); + } + + /** Initializes table data, loading it from Traffic Ops. */ + public ngOnInit(): void { + this.route.queryParamMap.subscribe( + m => { + const search = m.get("search"); + if (search) { + this.fuzzControl.setValue(decodeURIComponent(search)); + this.updateURL(); + } + }, + e => { + console.error("Failed to get query parameters:", e); + } + ); + } + + /** Definitions of the table's columns according to the ag-grid API */ + public columnDefs = [ + { + field: "name", + headerName: "Name" + }, + { + field: "description", + headerName: "Description", + }, + { + field: "lastUpdated", + headerName: "Last Updated" + } Review Comment: This should use a date filter -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
