From 01e2e6881033e7f8fbe6d6cadd3c64d8b2a1d0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Tue, 31 Mar 2020 12:06:48 +0100 Subject: [PATCH] feat(ui): toggle all grids with alt+click --- ui/src/Components/Grid/AlertGrid/Grid.js | 40 ++++++- .../Components/Grid/AlertGrid/index.test.js | 101 ++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) diff --git a/ui/src/Components/Grid/AlertGrid/Grid.js b/ui/src/Components/Grid/AlertGrid/Grid.js index caed266ab..5485e7bc5 100644 --- a/ui/src/Components/Grid/AlertGrid/Grid.js +++ b/ui/src/Components/Grid/AlertGrid/Grid.js @@ -93,13 +93,42 @@ const Grid = observer( toggle() { this.show = !this.show; }, + set(value) { + this.show = value; + }, }, { toggle: action.bound, + set: action.bound, } ); } + onCollapseClick = (event) => { + // left click => toggle current grid + // left click + alt => toggle all grids + + this.gridToggle.toggle(); + + if (event.altKey === true) { + const toggleEvent = new CustomEvent("alertGridCollapse", { + detail: this.gridToggle.show, + }); + window.dispatchEvent(toggleEvent); + } + }; + + onAlertGridCollapseEvent = (event) => { + this.gridToggle.set(event.detail); + }; + + componentDidMount() { + window.addEventListener( + "alertGridCollapse", + this.onAlertGridCollapseEvent + ); + } + componentDidUpdate() { const { grid } = this.props; @@ -112,6 +141,13 @@ const Grid = observer( } } + componentWillUnmount() { + window.removeEventListener( + "alertGridCollapse", + this.onAlertGridCollapseEvent + ); + } + render() { const { alertStore, @@ -164,9 +200,9 @@ const Grid = observer( - + diff --git a/ui/src/Components/Grid/AlertGrid/index.test.js b/ui/src/Components/Grid/AlertGrid/index.test.js index 51d0519ea..28c5a9512 100644 --- a/ui/src/Components/Grid/AlertGrid/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/index.test.js @@ -36,6 +36,16 @@ afterEach(() => { clear(); }); +const MountedAlertGrid = () => { + return mount( + + ); +}; + const ShallowAlertGrid = () => { return shallow( ", () => { labelName: "foo", labelValue: "bar", alertGroups: groups, + stateCount: { + unprocessed: 1, + suppressed: 2, + active: 3, + }, }, { labelName: "foo", labelValue: "", alertGroups: [], + stateCount: { + unprocessed: 1, + suppressed: 2, + active: 3, + }, }, ]; const tree = MountedGrid(); @@ -320,6 +340,12 @@ describe("", () => { expect(tree.find("AlertGroup").at(i).find("Alert")).toHaveLength(0); } }); + + it("doesn't crash on unmount", () => { + MockGroupList(5, 3); + const tree = MountedGrid(); + tree.unmount(); + }); }); describe("", () => { @@ -507,6 +533,81 @@ describe("", () => { ]); }); + it("alt+click on a grid toggle toggles all grid groups", () => { + MockGroupList(10, 3); + const groups = alertStore.data.grids[0].alertGroups; + alertStore.data.grids = [ + { + labelName: "foo", + labelValue: "bar", + alertGroups: groups, + stateCount: { + unprocessed: 1, + suppressed: 2, + active: 3, + }, + }, + { + labelName: "foo", + labelValue: "", + alertGroups: groups, + stateCount: { + unprocessed: 1, + suppressed: 2, + active: 3, + }, + }, + ]; + const tree = MountedAlertGrid(); + expect(tree.find("Grid")).toHaveLength(2); + expect(tree.find("AlertGroup")).toHaveLength(20); + + // toggle all grids to hide all groups + tree + .find("Grid") + .at(0) + .find("span.cursor-pointer") + .at(0) + .simulate("click", { altKey: true }); + expect(tree.find("AlertGroup")).toHaveLength(0); + + // show first grid + tree + .find("Grid") + .at(0) + .find("span.cursor-pointer") + .at(0) + .simulate("click", { altKey: false }); + expect(tree.find("AlertGroup")).toHaveLength(10); + + // show all grids + tree + .find("Grid") + .at(1) + .find("span.cursor-pointer") + .at(0) + .simulate("click", { altKey: true }); + expect(tree.find("AlertGroup")).toHaveLength(20); + + // hide all grids + tree + .find("Grid") + .at(0) + .find("span.cursor-pointer") + .at(0) + .simulate("click", { altKey: true }); + expect(tree.find("AlertGroup")).toHaveLength(0); + + // show all grids + tree + .find("Grid") + .at(1) + .find("span.cursor-pointer") + .at(0) + .simulate("click", { altKey: true }); + expect(tree.find("AlertGroup")).toHaveLength(20); + }); + it("doesn't crash on unmount", () => { MockGroupList(60, 5); const tree = ShallowAlertGrid();