From 84a3c6e134ad71cab50e3d999cee3b4aed9f03d2 Mon Sep 17 00:00:00 2001 From: gillespi314 <73313222+gillespi314@users.noreply.github.com> Date: Thu, 7 Oct 2021 14:30:14 -0500 Subject: [PATCH] Fix teams dropdown default value for hosts page filtered by policy; fix policy button text (#2439) --- frontend/interfaces/policy.ts | 1 + .../hosts/ManageHostsPage/ManageHostsPage.tsx | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/frontend/interfaces/policy.ts b/frontend/interfaces/policy.ts index caa94fe139..a691dcabde 100644 --- a/frontend/interfaces/policy.ts +++ b/frontend/interfaces/policy.ts @@ -2,6 +2,7 @@ export interface IPolicy { id: number; query_id: number; query_name: string; + team_id: number; passing_host_count: number; failing_host_count: number; last_run_time?: string; diff --git a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx index 3516139c21..1006e9425e 100644 --- a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx +++ b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx @@ -83,7 +83,7 @@ interface IManageHostsProps { interface ILabelsResponse { labels: ILabel[]; } -interface IPolicyResponse { +interface IPolicyAPIResponse { policy: IPolicy; } @@ -176,7 +176,7 @@ const ManageHostsPage = ({ const [isHostsLoading, setIsHostsLoading] = useState(false); const [hasHostErrors, setHasHostErrors] = useState(false); const [sortBy, setSortBy] = useState(initialSortBy); - const [policyName, setPolicyName] = useState(); + const [policy, setPolicy] = useState(); const [tableQueryData, setTableQueryData] = useState(); // ======== end states @@ -233,18 +233,19 @@ const ManageHostsPage = ({ } ); - useQuery( + useQuery( ["policy"], () => { - const request = currentTeam - ? teamPoliciesAPI.load(currentTeam.id, policyId) + const teamId = parseInt(queryParams?.team_id, 10) || 0; + const request = teamId + ? teamPoliciesAPI.load(teamId, policyId) : globalPoliciesAPI.load(policyId); return request; }, { enabled: !!policyId, - onSuccess: ({ policy }) => { - setPolicyName(policy.query_name); + onSuccess: ({ policy: policyAPIResponse }) => { + setPolicy(policyAPIResponse); }, } ); @@ -771,7 +772,7 @@ const ManageHostsPage = ({ ); const selectedTeamId = getValidatedTeamId( teams || [], - currentTeam?.id as number, + (policyId && policy?.team_id) || (currentTeam?.id as number), currentUser, isOnGlobalTeam as boolean ); @@ -796,7 +797,7 @@ const ManageHostsPage = ({ const buttonText = ( <> Policy - {policyName} + {policy?.query_name} Remove policy filter ); @@ -810,7 +811,7 @@ const ManageHostsPage = ({ className={`${baseClass}__clear-policies-filter`} onClick={handleClearPoliciesFilter} variant={"small-text-icon"} - title={policyName} + title={policy?.query_name} > {buttonText}