ocket8888 commented on code in PR #7362:
URL: https://github.com/apache/trafficcontrol/pull/7362#discussion_r1124736965


##########
experimental/traffic-portal/src/app/core/cache-groups/asns/table/asns-table.component.ts:
##########
@@ -0,0 +1,133 @@
+/*
+* 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 { MatDialog } from "@angular/material/dialog";
+import { ActivatedRoute, type Params } from "@angular/router";
+import { BehaviorSubject } from "rxjs";
+import type { ResponseASN } from "trafficops-types";
+
+import { CacheGroupService } from "src/app/api";
+import { CurrentUserService } from 
"src/app/shared/currentUser/current-user.service";
+import { DecisionDialogComponent } from 
"src/app/shared/dialogs/decision-dialog/decision-dialog.component";
+import type { ContextMenuActionEvent, ContextMenuItem } from 
"src/app/shared/generic-table/generic-table.component";
+import { NavigationService } from 
"src/app/shared/navigation/navigation.service";
+
+/**
+ * AsnsTableComponent is the controller for the "Asns" table.
+ */
+@Component({
+       selector: "tp-asns",
+       styleUrls: ["./asns-table.component.scss"],
+       templateUrl: "./asns-table.component.html"
+})
+export class AsnsTableComponent implements OnInit {
+       /** List of asns */
+       public asns: Promise<Array<ResponseASN>>;
+
+       constructor(private readonly route: ActivatedRoute, private readonly 
headerSvc: NavigationService,
+               private readonly api: CacheGroupService, private readonly 
dialog: MatDialog, public readonly auth: CurrentUserService) {
+               this.fuzzySubject = new BehaviorSubject<string>("");
+               this.asns = this.api.getASNs();
+               this.headerSvc.headerTitle.next("ASNs");
+       }
+
+       /** 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: "asn",
+                       headerName: "ASN"
+               },
+               {
+                       field: "cachegroup",
+                       headerName: "Cache Group",
+               },
+               {
+                       field: "lastUpdated",
+                       headerName: "Last Updated"
+               }
+       ];
+
+       /** Definitions for the context menu items (which act on augmented asn 
data). */
+       public contextMenuItems: Array<ContextMenuItem<ResponseASN>> = [
+               {
+                       href: (selectedRow: ResponseASN): string => 
`${selectedRow.id}`,
+                       name: "Edit"
+               },
+               {
+                       href: (selectedRow: ResponseASN): string => 
`${selectedRow.id}`,
+                       name: "Open in New Tab",
+                       newTab: true
+               },
+               {
+                       action: "delete",
+                       multiRow: false,
+                       name: "Delete"
+               },
+               {
+                       href: "/core/cache-groups",
+                       name: "View Cache Group",
+                       queryParams: (selectedRow: ResponseASN): Params => 
({name: selectedRow.cachegroup}),
+               }
+       ];
+
+       /** 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 evt The action selected from the context menu.
+        */
+       public async handleContextMenu(evt: 
ContextMenuActionEvent<ResponseASN>): Promise<void> {
+               const data = evt.data as ResponseASN;
+               switch(evt.action) {
+                       case "delete":
+                               const ref = 
this.dialog.open(DecisionDialogComponent, {
+                                       data: {message: `Are you sure you want 
to delete asn ${data.asn}?`, title: "Confirm Delete"}

Review Comment:
   nit: "ASN" should be capitalized, as it's an initialism.



##########
experimental/traffic-portal/src/app/api/testing/cache-group.service.ts:
##########
@@ -584,4 +593,39 @@ export class CacheGroupService {
                }
                return this.regions.splice(index, 1)[0];
        }
+
+       public async getASNs(): Promise<Array<ResponseASN>>;
+       public async getASNs(id: number): Promise<ResponseASN>;
+
+       /**
+        * Gets an array of ASNs from Traffic Ops.
+        *
+        * @param id If given, returns only the asn with the given id (number).
+        * @returns An Array of ASNs objects - or a single ASN object if 'id'
+        * was given.
+        */
+       public async getASNs(id?: number): Promise<Array<ResponseASN> | 
ResponseASN> {
+               if(id) {
+                       const asn = this.asns.find(a=>a.id === id);
+                       if (!asn) {
+                               throw new Error(`no such asn with id: ${id}`);
+                       }
+                       return asn;
+               }
+               return this.asns;
+       }
+
+       /**
+        * Deletes an existing asn.
+        *
+        * @param asn Id of the asn to delete.
+        * @returns The deleted asn.
+        */
+       public async deleteASN(asn: number): Promise<ResponseASN> {

Review Comment:
   This call signature is incompatible with the concrete service; it needs to 
also be able to accept a `ResponseASN` because things that are using the real 
service should be able to use this in the same way.
   
   I'm working on a changeset that will enforce this at compile-time, which 
should hopefully make that easier to line up in the future.



##########
experimental/traffic-portal/src/app/core/cache-groups/cache-group-table/cache-group-table.component.ts:
##########
@@ -15,7 +15,7 @@
 import { Component, type OnInit } from "@angular/core";
 import { FormControl } from "@angular/forms";
 import { MatDialog } from "@angular/material/dialog";
-import { ActivatedRoute } from "@angular/router";
+import {ActivatedRoute, type Params} from "@angular/router";

Review Comment:
   should keep the spaces with those braces



##########
experimental/traffic-portal/src/app/core/cache-groups/asns/table/asns-table.component.ts:
##########
@@ -0,0 +1,133 @@
+/*
+* 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 { MatDialog } from "@angular/material/dialog";
+import { ActivatedRoute, type Params } from "@angular/router";
+import { BehaviorSubject } from "rxjs";
+import type { ResponseASN } from "trafficops-types";
+
+import { CacheGroupService } from "src/app/api";
+import { CurrentUserService } from 
"src/app/shared/currentUser/current-user.service";
+import { DecisionDialogComponent } from 
"src/app/shared/dialogs/decision-dialog/decision-dialog.component";
+import type { ContextMenuActionEvent, ContextMenuItem } from 
"src/app/shared/generic-table/generic-table.component";
+import { NavigationService } from 
"src/app/shared/navigation/navigation.service";
+
+/**
+ * AsnsTableComponent is the controller for the "Asns" table.
+ */
+@Component({
+       selector: "tp-asns",
+       styleUrls: ["./asns-table.component.scss"],
+       templateUrl: "./asns-table.component.html"
+})
+export class AsnsTableComponent implements OnInit {
+       /** List of asns */
+       public asns: Promise<Array<ResponseASN>>;
+
+       constructor(private readonly route: ActivatedRoute, private readonly 
headerSvc: NavigationService,
+               private readonly api: CacheGroupService, private readonly 
dialog: MatDialog, public readonly auth: CurrentUserService) {
+               this.fuzzySubject = new BehaviorSubject<string>("");
+               this.asns = this.api.getASNs();
+               this.headerSvc.headerTitle.next("ASNs");
+       }
+
+       /** 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: "asn",
+                       headerName: "ASN"
+               },
+               {
+                       field: "cachegroup",
+                       headerName: "Cache Group",
+               },
+               {
+                       field: "lastUpdated",
+                       headerName: "Last Updated"
+               }
+       ];
+
+       /** Definitions for the context menu items (which act on augmented asn 
data). */
+       public contextMenuItems: Array<ContextMenuItem<ResponseASN>> = [
+               {
+                       href: (selectedRow: ResponseASN): string => 
`${selectedRow.id}`,
+                       name: "Edit"
+               },
+               {
+                       href: (selectedRow: ResponseASN): string => 
`${selectedRow.id}`,
+                       name: "Open in New Tab",
+                       newTab: true
+               },
+               {
+                       action: "delete",
+                       multiRow: false,
+                       name: "Delete"
+               },
+               {
+                       href: "/core/cache-groups",
+                       name: "View Cache Group",
+                       queryParams: (selectedRow: ResponseASN): Params => 
({name: selectedRow.cachegroup}),
+               }
+       ];
+
+       /** 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 evt The action selected from the context menu.
+        */
+       public async handleContextMenu(evt: 
ContextMenuActionEvent<ResponseASN>): Promise<void> {
+               const data = evt.data as ResponseASN;

Review Comment:
   When you use `as`, it'd be nice to have a comment explaining why that's 
necessary/safe. The idea being that `as` should almost never be used (except in 
`as const`). In this case, we know that there are no multi-row actions, so this 
is a safe assumption to make - if you want to do that; personally I'd just 
check if it's an array and then throw an error because then you wouldn't need 
to use `as`.



##########
experimental/traffic-portal/src/app/core/cache-groups/cache-group-table/cache-group-table.component.spec.ts:
##########
@@ -35,6 +35,7 @@ import { isAction } from 
"src/app/shared/generic-table/generic-table.component";
 import { NavigationService } from 
"src/app/shared/navigation/navigation.service";
 
 import { CacheGroupTableComponent } from "./cache-group-table.component";
+// import {ResponseASN} from "trafficops-types";

Review Comment:
   can we remove this comment?



-- 
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]

Reply via email to