diff --git a/changes/1269-refresh-matching-hosts b/changes/1269-refresh-matching-hosts new file mode 100644 index 0000000000..14868a7af6 --- /dev/null +++ b/changes/1269-refresh-matching-hosts @@ -0,0 +1 @@ +* Fixed refreshing manage hosts table to include current label and query after changing a host's team. \ No newline at end of file diff --git a/frontend/fleet/entities/hosts.tests.js b/frontend/fleet/entities/hosts.tests.js index dd572bd357..3066295893 100644 --- a/frontend/fleet/entities/hosts.tests.js +++ b/frontend/fleet/entities/hosts.tests.js @@ -46,7 +46,13 @@ describe("Kolide - API client (hosts)", () => { Fleet.setBearerToken(bearerToken); return Fleet.hosts - .loadAll(page, perPage, selectedFilter, query, sortBy) + .loadAll({ + page, + perPage, + selectedLabel: selectedFilter, + globalFilter: query, + sortBy, + }) .then(() => { expect(request.isDone()).toEqual(true); }); @@ -61,9 +67,15 @@ describe("Kolide - API client (hosts)", () => { }); Fleet.setBearerToken(bearerToken); - return Fleet.hosts.loadAll(2, 50, "labels/6").then(() => { - expect(request.isDone()).toEqual(true); - }); + return Fleet.hosts + .loadAll({ + page: 2, + perPage: 50, + selectedLabel: "labels/6", + }) + .then(() => { + expect(request.isDone()).toEqual(true); + }); }); }); }); diff --git a/frontend/fleet/entities/hosts.ts b/frontend/fleet/entities/hosts.ts index 8c16653964..91e8ca654b 100644 --- a/frontend/fleet/entities/hosts.ts +++ b/frontend/fleet/entities/hosts.ts @@ -6,6 +6,14 @@ interface ISortOption { direction: string; } +interface IHostLoadOptions { + page: number; + perPage: number; + selectedLabel: string; + globalFilter: string; + sortBy: ISortOption[]; +} + export default (client: any) => { return { destroy: (host: IHost) => { @@ -30,14 +38,13 @@ export default (client: any) => { .authenticatedGet(endpoint) .then((response: any) => response.host); }, - loadAll: ( - page = 0, - perPage = 100, - selected = "", - globalFilter = "", - sortBy: ISortOption[] = [] - ) => { + loadAll: (options: IHostLoadOptions | undefined) => { const { HOSTS, LABEL_HOSTS } = endpoints; + const page = options?.page || 0; + const perPage = options?.perPage || 100; + const selectedLabel = options?.selectedLabel || ""; + const globalFilter = options?.globalFilter || ""; + const sortBy = options?.sortBy || []; // TODO: add this query param logic to client class const pagination = `page=${page}&per_page=${perPage}`; @@ -57,20 +64,20 @@ export default (client: any) => { let endpoint = ""; const labelPrefix = "labels/"; - if (selected.startsWith(labelPrefix)) { - const lid = selected.substr(labelPrefix.length); + if (selectedLabel.startsWith(labelPrefix)) { + const lid = selectedLabel.substr(labelPrefix.length); endpoint = `${LABEL_HOSTS( parseInt(lid, 10) )}?${pagination}${searchQuery}${orderKeyParam}${orderDirection}`; } else { let selectedFilter = ""; if ( - selected === "new" || - selected === "online" || - selected === "offline" || - selected === "mia" + selectedLabel === "new" || + selectedLabel === "online" || + selectedLabel === "offline" || + selectedLabel === "mia" ) { - selectedFilter = `&status=${selected}`; + selectedFilter = `&status=${selectedLabel}`; } endpoint = `${HOSTS}?${pagination}${selectedFilter}${searchQuery}${orderKeyParam}${orderDirection}`; } diff --git a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.jsx b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.jsx index a3da6000a5..c5879d6c0c 100644 --- a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.jsx +++ b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.jsx @@ -198,7 +198,13 @@ export class ManageHostsPage extends PureComponent { this.setState({ searchQuery }); dispatch( - getHosts(pageIndex, pageSize, selectedFilter, searchQuery, sortBy) + getHosts({ + page: pageIndex, + perPage: pageSize, + selectedLabel: selectedFilter, + globalFilter: searchQuery, + sortBy, + }) ); }; @@ -309,7 +315,7 @@ export class ManageHostsPage extends PureComponent { ? `Hosts successfully removed from teams.` : `Hosts successfully transferred to ${team.name}.`; dispatch(renderFlash("success", successMessage)); - dispatch(getHosts()); + dispatch(getHosts({ selectedLabel: selectedFilter, searchQuery })); }) .catch(() => { dispatch( diff --git a/frontend/redux/nodes/components/ManageHostsPage/actions.js b/frontend/redux/nodes/components/ManageHostsPage/actions.js index 878d006233..e2cff1c4ed 100644 --- a/frontend/redux/nodes/components/ManageHostsPage/actions.js +++ b/frontend/redux/nodes/components/ManageHostsPage/actions.js @@ -66,15 +66,15 @@ export const getLabels = () => (dispatch) => { dispatch(silentGetStatusLabelCounts); }; -export const getHosts = ( +export const getHosts = ({ page, perPage, selectedLabel, globalFilter, - sortBy -) => (dispatch) => { + sortBy, +}) => (dispatch) => { dispatch( - hostActions.loadAll(page, perPage, selectedLabel, globalFilter, sortBy) + hostActions.loadAll({ page, perPage, selectedLabel, globalFilter, sortBy }) ); };