Skip to content

Commit

Permalink
[SIEM][Detections Engine] Fixed minor UI bug on all rules table pagin…
Browse files Browse the repository at this point in the history
…ation (elastic#59094) (elastic#59206)

* Fixed minor UI bug on all rules table pagination
  • Loading branch information
yctercero authored Mar 3, 2020
1 parent e29a53f commit 76480da
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface UseRules {
pagination: PaginationOptions;
filterOptions: FilterOptions;
refetchPrePackagedRulesStatus?: () => void;
dispatchRulesInReducer?: (rules: Rule[]) => void;
dispatchRulesInReducer?: (rules: Rule[], pagination: Partial<PaginationOptions>) => void;
}

/**
Expand Down Expand Up @@ -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([], {});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CreatePreBuiltRules,
FilterOptions,
Rule,
PaginationOptions,
} from '../../../../containers/detection_engine/rules';
import { HeaderSection } from '../../../../components/header_section';
import {
Expand Down Expand Up @@ -118,10 +119,11 @@ export const AllRules = React.memo<AllRulesProps>(
const history = useHistory();
const [, dispatchToaster] = useStateToaster();

const setRules = useCallback((newRules: Rule[]) => {
const setRules = useCallback((newRules: Rule[], newPagination: Partial<PaginationOptions>) => {
dispatch({
type: 'setRules',
rules: newRules,
pagination: newPagination,
});
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaginationOptions> }
| { type: 'updateRules'; rules: Rule[] }
| { type: 'updatePagination'; pagination: Partial<PaginationOptions> }
| {
type: 'updateFilterOptions';
filterOptions: Partial<FilterOptions>;
Expand Down Expand Up @@ -76,6 +75,10 @@ export const allRulesReducer = (
selectedRuleIds: [],
loadingRuleIds: [],
loadingRulesAction: null,
pagination: {
...state.pagination,
...action.pagination,
},
};
}
case 'updateRules': {
Expand All @@ -101,15 +104,6 @@ export const allRulesReducer = (
}
return state;
}
case 'updatePagination': {
return {
...state,
pagination: {
...state.pagination,
...action.pagination,
},
};
}
case 'updateFilterOptions': {
return {
...state,
Expand Down

0 comments on commit 76480da

Please sign in to comment.