Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIEM][Detections Engine] Fixed minor UI bug on all rules table pagination #59094

Merged
merged 4 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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