From 35a0c1c1a096dc68ab07d3e683138c8b3d438e1e Mon Sep 17 00:00:00 2001 From: Gabriel Hernandez Date: Tue, 9 Aug 2022 12:25:49 +0100 Subject: [PATCH] clean up host entity loadHosts method (#7083) --- .../hosts/ManageHostsPage/ManageHostsPage.tsx | 5 +- frontend/services/entities/hosts.ts | 169 ++++++++++-------- frontend/utilities/helpers.ts | 4 +- 3 files changed, 101 insertions(+), 77 deletions(-) diff --git a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx index f355452f91..029789ff33 100644 --- a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx +++ b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx @@ -258,7 +258,10 @@ const ManageHostsPage = ({ const routeTemplate = route && route.path ? route.path : ""; const policyId = queryParams?.policy_id; const policyResponse: PolicyResponse = queryParams?.policy_response; - const softwareId = parseInt(queryParams?.software_id, 10); + const softwareId = + queryParams?.software_id !== undefined + ? parseInt(queryParams?.software_id, 10) + : undefined; const { active_label: activeLabel, label_id: labelID } = routeParams; // ===== filter matching diff --git a/frontend/services/entities/hosts.ts b/frontend/services/entities/hosts.ts index 3980368447..a44ba15945 100644 --- a/frontend/services/entities/hosts.ts +++ b/frontend/services/entities/hosts.ts @@ -2,6 +2,7 @@ import sendRequest from "services"; import endpoints from "utilities/endpoints"; import { IHost } from "interfaces/host"; +import { buildQueryStringFromParams } from "utilities/url"; export interface ISortOption { key: string; @@ -40,6 +41,69 @@ export interface IExportHostsOptions { export type ILoadHostDetailsExtension = "device_mapping" | "macadmins"; +const LABEL_PREFIX = "labels/"; + +const getLabel = (selectedLabels?: string[]) => { + if (selectedLabels === undefined) return undefined; + return selectedLabels.find((filter) => filter.includes(LABEL_PREFIX)); +}; + +const getHostEndpoint = (selectedLabels?: string[]) => { + const { HOSTS, LABEL_HOSTS } = endpoints; + if (selectedLabels === undefined) return endpoints.HOSTS; + + const label = getLabel(selectedLabels); + if (label) { + const labelId = label.substr(LABEL_PREFIX.length); + return LABEL_HOSTS(parseInt(labelId, 10)); + } + + return HOSTS; +}; + +const getSortParams = (sortOptions?: ISortOption[]) => { + if (sortOptions === undefined || sortOptions.length === 0) { + return {}; + } + + const sortItem = sortOptions[0]; + return { + order_key: sortItem.key, + order_direction: sortItem.direction, + }; +}; + +const getStatusParam = (selectedLabels?: string[]) => { + if (selectedLabels === undefined) return undefined; + + const status = selectedLabels.find((f) => !f.includes(LABEL_PREFIX)); + if (status === undefined) return undefined; + + const statusFilterList = ["new", "online", "offline"]; + return statusFilterList.includes(status) ? status : undefined; +}; + +const getPolicyParams = ( + label?: string, + policyId?: number, + policyResponse?: string +) => { + if (label !== undefined || policyId === undefined) return {}; + + return { + policy_id: policyId, + policy_response: policyResponse, + }; +}; + +const getSoftwareParam = ( + label?: string, + policyId?: number, + softwareId?: number +) => { + return label === undefined && policyId === undefined ? softwareId : undefined; +}; + export default { destroy: (host: IHost) => { const { HOSTS } = endpoints; @@ -130,82 +194,39 @@ export default { return sendRequest("GET", path); }, - loadHosts: (options: ILoadHostsOptions | undefined) => { - const { HOSTS, LABEL_HOSTS } = endpoints; - const page = options?.page || 0; - const perPage = options?.perPage || 100; - const selectedLabels = options?.selectedLabels || []; - const globalFilter = options?.globalFilter || ""; - const sortBy = options?.sortBy || []; - const teamId = options?.teamId || null; - const policyId = options?.policyId || null; - const policyResponse = options?.policyResponse || null; - const softwareId = options?.softwareId || null; - const device_mapping = options?.device_mapping || null; - const columns = options?.columns || null; + loadHosts: ({ + page = 0, + perPage = 100, + globalFilter, + teamId, + policyId, + policyResponse = "passing", + softwareId, + device_mapping, + selectedLabels, + sortBy, + }: ILoadHostsOptions) => { + const label = getLabel(selectedLabels); + const sortParams = getSortParams(sortBy); + const policyParams = getPolicyParams(label, policyId, policyResponse); - // TODO: add this query param logic to client class - const pagination = `page=${page}&per_page=${perPage}`; - - let orderKeyParam = ""; - let orderDirection = ""; - if (sortBy.length !== 0) { - const sortItem = sortBy[0]; - orderKeyParam += `&order_key=${sortItem.key}`; - orderDirection = `&order_direction=${sortItem.direction}`; - } - - let searchQuery = ""; - if (globalFilter !== "") { - searchQuery = `&query=${globalFilter}`; - } - - let path = ""; - const labelPrefix = "labels/"; - - // Handle multiple filters - const label = selectedLabels.find((f) => f.includes(labelPrefix)); - const status = selectedLabels.find((f) => !f.includes(labelPrefix)); - const isValidStatus = - status === "new" || status === "online" || status === "offline"; - - if (label) { - const lid = label.substr(labelPrefix.length); - path = `${LABEL_HOSTS( - parseInt(lid, 10) - )}?${pagination}${searchQuery}${orderKeyParam}${orderDirection}`; - - // connect status if applicable - if (status && isValidStatus) { - path += `&status=${status}`; - } - } else if (status && isValidStatus) { - path = `${HOSTS}?${pagination}&status=${status}${searchQuery}${orderKeyParam}${orderDirection}`; - } else { - path = `${HOSTS}?${pagination}${searchQuery}${orderKeyParam}${orderDirection}`; - } - - if (teamId) { - path += `&team_id=${teamId}`; - } - - if (!label && policyId) { - path += `&policy_id=${policyId}`; - path += `&policy_response=${policyResponse || "passing"}`; // TODO: confirm whether there should be a default if there is an id but no response sepcified - } - // TODO: consider how to check for mutually exclusive scenarios with label, policy and software - if (!label && !policyId && softwareId) { - path += `&software_id=${softwareId}`; - } - - if (device_mapping) { - path += "&device_mapping=true"; - } - - if (columns) { - path += `&columns=${columns}`; - } + const queryParams = { + page, + per_page: perPage, + query: globalFilter, + team_id: teamId, + device_mapping, + order_key: sortParams.order_key, + order_direction: sortParams.order_direction, + policy_id: policyParams.policy_id, + policy_response: policyParams.policy_response, + software_id: getSoftwareParam(label, policyId, softwareId), + status: getStatusParam(selectedLabels), + }; + const queryString = buildQueryStringFromParams(queryParams); + const endpoint = getHostEndpoint(selectedLabels); + const path = `${endpoint}?${queryString}`; return sendRequest("GET", path); }, loadHostDetails: (hostID: number) => { diff --git a/frontend/utilities/helpers.ts b/frontend/utilities/helpers.ts index 487969f025..5e209fb647 100644 --- a/frontend/utilities/helpers.ts +++ b/frontend/utilities/helpers.ts @@ -749,7 +749,7 @@ export const getValidatedTeamId = ( teamId: number, currentUser: IUser | null, isOnGlobalTeam: boolean -): number => { +) => { let currentUserTeams: ITeamSummary[] = []; if (isOnGlobalTeam) { currentUserTeams = teams; @@ -761,7 +761,7 @@ export const getValidatedTeamId = ( const validatedTeamId = !isNaN(teamId) && teamId > 0 && currentUserTeamIds.includes(teamId) ? teamId - : 0; + : undefined; return validatedTeamId; };