From 588a0682829e5b49973f00eabd8bbe36d4186322 Mon Sep 17 00:00:00 2001 From: Yara Tercero Date: Tue, 3 Mar 2020 14:00:43 -0500 Subject: [PATCH] [SIEM][Detections Engine] Fixed minor UI bug on all rules table pagination (#59094) * Fixed minor UI bug on all rules table pagination --- .../detection_engine/rules/use_rules.tsx | 10 +++-- .../rules/all/helpers.test.tsx | 44 ++++++++++++++++++- .../detection_engine/rules/all/index.tsx | 4 +- .../detection_engine/rules/all/reducer.ts | 16 +++---- 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/use_rules.tsx b/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/use_rules.tsx index d05d59d15802d..81b8b04ed6648 100644 --- a/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/use_rules.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/use_rules.tsx @@ -23,7 +23,7 @@ export interface UseRules { pagination: PaginationOptions; filterOptions: FilterOptions; refetchPrePackagedRulesStatus?: () => void; - dispatchRulesInReducer?: (rules: Rule[]) => void; + dispatchRulesInReducer?: (rules: Rule[], pagination: Partial) => void; } /** @@ -59,14 +59,18 @@ export const useRules = ({ if (isSubscribed) { setRules(fetchRulesResult); if (dispatchRulesInReducer != null) { - dispatchRulesInReducer(fetchRulesResult.data); + dispatchRulesInReducer(fetchRulesResult.data, { + page: fetchRulesResult.page, + perPage: fetchRulesResult.perPage, + total: fetchRulesResult.total, + }); } } } catch (error) { if (isSubscribed) { errorToToaster({ title: i18n.RULE_FETCH_FAILURE, error, dispatchToaster }); if (dispatchRulesInReducer != null) { - dispatchRulesInReducer([]); + dispatchRulesInReducer([], {}); } } } diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/helpers.test.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/helpers.test.tsx index c60933733587d..062d7967bf301 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/helpers.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/helpers.test.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { bucketRulesResponse } from './helpers'; +import { bucketRulesResponse, showRulesTable } from './helpers'; import { mockRule, mockRuleError } from './__mocks__/mock'; import uuid from 'uuid'; import { Rule, RuleError } from '../../../../containers/detection_engine/rules'; @@ -44,4 +44,46 @@ describe('AllRulesTable Helpers', () => { }); }); }); + + describe('showRulesTable', () => { + test('returns false when rulesCustomInstalled and rulesInstalled are null', () => { + const result = showRulesTable({ + rulesCustomInstalled: null, + rulesInstalled: null, + }); + expect(result).toBeFalsy(); + }); + + test('returns false when rulesCustomInstalled and rulesInstalled are 0', () => { + const result = showRulesTable({ + rulesCustomInstalled: 0, + rulesInstalled: 0, + }); + expect(result).toBeFalsy(); + }); + + test('returns false when both rulesCustomInstalled and rulesInstalled checks return false', () => { + const result = showRulesTable({ + rulesCustomInstalled: 0, + rulesInstalled: null, + }); + expect(result).toBeFalsy(); + }); + + test('returns true if rulesCustomInstalled is not null or 0', () => { + const result = showRulesTable({ + rulesCustomInstalled: 5, + rulesInstalled: null, + }); + expect(result).toBeTruthy(); + }); + + test('returns true if rulesInstalled is not null or 0', () => { + const result = showRulesTable({ + rulesCustomInstalled: null, + rulesInstalled: 5, + }); + expect(result).toBeTruthy(); + }); + }); }); diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/index.tsx b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/index.tsx index 79fec526faf48..9676b83a26f55 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/index.tsx @@ -21,6 +21,7 @@ import { CreatePreBuiltRules, FilterOptions, Rule, + PaginationOptions, } from '../../../../containers/detection_engine/rules'; import { HeaderSection } from '../../../../components/header_section'; import { @@ -118,10 +119,11 @@ export const AllRules = React.memo( const history = useHistory(); const [, dispatchToaster] = useStateToaster(); - const setRules = useCallback((newRules: Rule[]) => { + const setRules = useCallback((newRules: Rule[], newPagination: Partial) => { dispatch({ type: 'setRules', rules: newRules, + pagination: newPagination, }); }, []); diff --git a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/reducer.ts b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/reducer.ts index 54da43efd66d9..0a4d169d13154 100644 --- a/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/reducer.ts +++ b/x-pack/legacy/plugins/siem/public/pages/detection_engine/rules/all/reducer.ts @@ -26,9 +26,8 @@ export type Action = | { type: 'exportRuleIds'; ids: string[] } | { type: 'loadingRuleIds'; ids: string[]; actionType: LoadingRuleAction } | { type: 'selectedRuleIds'; ids: string[] } - | { type: 'setRules'; rules: Rule[] } + | { type: 'setRules'; rules: Rule[]; pagination: Partial } | { type: 'updateRules'; rules: Rule[] } - | { type: 'updatePagination'; pagination: Partial } | { type: 'updateFilterOptions'; filterOptions: Partial; @@ -76,6 +75,10 @@ export const allRulesReducer = ( selectedRuleIds: [], loadingRuleIds: [], loadingRulesAction: null, + pagination: { + ...state.pagination, + ...action.pagination, + }, }; } case 'updateRules': { @@ -101,15 +104,6 @@ export const allRulesReducer = ( } return state; } - case 'updatePagination': { - return { - ...state, - pagination: { - ...state.pagination, - ...action.pagination, - }, - }; - } case 'updateFilterOptions': { return { ...state,