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

[Cases] Refactor: AllCasesList component in selection mode should only select #128915

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -18,19 +18,15 @@ const AllCasesSelectorModalLazy: React.FC<AllCasesSelectorModalProps> = lazy(
export const getAllCasesSelectorModalLazy = ({
owner,
userCanCrud,
alertData,
hiddenStatuses,
onRowClick,
updateCase,
onClose,
}: GetAllCasesSelectorModalProps) => (
<CasesProvider value={{ owner, userCanCrud }}>
<Suspense fallback={<EuiLoadingSpinner />}>
<AllCasesSelectorModalLazy
alertData={alertData}
hiddenStatuses={hiddenStatuses}
onRowClick={onRowClick}
updateCase={updateCase}
onClose={onClose}
/>
</Suspense>
Expand All @@ -42,20 +38,14 @@ export const getAllCasesSelectorModalLazy = ({
* cases provider. to be further refactored https://github.com/elastic/kibana/issues/123183
*/
export const getAllCasesSelectorModalNoProviderLazy = ({
alertData,
attachments,
hiddenStatuses,
onRowClick,
updateCase,
onClose,
}: AllCasesSelectorModalProps) => (
<Suspense fallback={<EuiLoadingSpinner />}>
<AllCasesSelectorModalLazy
alertData={alertData}
attachments={attachments}
hiddenStatuses={hiddenStatuses}
onRowClick={onRowClick}
updateCase={updateCase}
onClose={onClose}
/>
</Suspense>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { TestProviders } from '../../common/mock';
import { casesStatus, useGetCasesMockState, mockCase, connectorsMock } from '../../containers/mock';

import { StatusAll } from '../../../common/ui/types';
import { CaseStatuses, CommentType } from '../../../common/api';
import { CaseStatuses } from '../../../common/api';
import { SECURITY_SOLUTION_OWNER } from '../../../common/constants';
import { getEmptyTagValue } from '../empty_value';
import { useDeleteCases } from '../../containers/use_delete_cases';
Expand Down Expand Up @@ -515,46 +515,6 @@ describe('AllCasesListGeneric', () => {
});
});

it('should call postComment when a case is selected in isSelectorView=true and has attachments', async () => {
const postCommentMockedValue = { status: { isLoading: false }, postComment: jest.fn() };
usePostCommentMock.mockReturnValueOnce(postCommentMockedValue);
const wrapper = mount(
<TestProviders>
<AllCasesList
isSelectorView={true}
attachments={[
{
type: CommentType.alert,
alertId: 'alert-id-201',
owner: 'test',
index: 'index-id-1',
rule: {
id: 'rule-id-1',
name: 'Awesome myrule',
},
},
]}
/>
</TestProviders>
);
wrapper.find('[data-test-subj="cases-table-row-select-1"]').first().simulate('click');
await waitFor(() => {
expect(postCommentMockedValue.postComment).toHaveBeenCalledWith({
caseId: '1',
data: {
alertId: 'alert-id-201',
index: 'index-id-1',
owner: 'test',
rule: {
id: 'rule-id-1',
name: 'Awesome myrule',
},
type: 'alert',
},
});
});
});

it('should call onRowClick with no cases and isSelectorView=true', async () => {
useGetCasesMock.mockReturnValue({
...defaultGetCases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ import {
FilterOptions,
SortFieldCase,
} from '../../../common/ui/types';
import {
CaseStatuses,
CommentRequestAlertType,
caseStatuses,
CommentType,
} from '../../../common/api';
import { CaseStatuses, caseStatuses } from '../../../common/api';
import { useGetCases } from '../../containers/use_get_cases';
import { usePostComment } from '../../containers/use_post_comment';

import { useAvailableCasesOwners } from '../app/use_available_owners';
import { useCasesColumns } from './columns';
Expand All @@ -33,7 +27,6 @@ import { EuiBasicTableOnChange } from './types';
import { CasesTable } from './table';
import { useConnectors } from '../../containers/configure/use_connectors';
import { useCasesContext } from '../cases_context/use_cases_context';
import { CaseAttachments } from '../../types';

const ProgressLoader = styled(EuiProgress)`
${({ $isShow }: { $isShow: boolean }) =>
Expand All @@ -52,28 +45,14 @@ const getSortField = (field: string): SortFieldCase =>
field === SortFieldCase.closedAt ? SortFieldCase.closedAt : SortFieldCase.createdAt;

export interface AllCasesListProps {
/**
* @deprecated Use the attachments prop instead
*/
alertData?: Omit<CommentRequestAlertType, 'type'>;
hiddenStatuses?: CaseStatusWithAllStatus[];
isSelectorView?: boolean;
onRowClick?: (theCase?: Case) => void;
updateCase?: (newCase: Case) => void;
doRefresh?: () => void;
attachments?: CaseAttachments;
}

export const AllCasesList = React.memo<AllCasesListProps>(
({
alertData,
attachments,
hiddenStatuses = [],
isSelectorView = false,
onRowClick,
updateCase,
doRefresh,
}) => {
({ hiddenStatuses = [], isSelectorView = false, onRowClick, doRefresh }) => {
const { owner, userCanCrud } = useCasesContext();
const hasOwner = !!owner.length;
const availableSolutions = useAvailableCasesOwners();
Expand All @@ -97,8 +76,6 @@ export const AllCasesList = React.memo<AllCasesListProps>(
setSelectedCases,
} = useGetCases({ initialFilterOptions });

// Post Comment to Case
const { postComment, isLoading: isCommentUpdating } = usePostComment();
const { connectors } = useConnectors();

const sorting = useMemo(
Expand Down Expand Up @@ -181,19 +158,6 @@ export const AllCasesList = React.memo<AllCasesListProps>(

const showActions = userCanCrud && !isSelectorView;

// TODO remove the deprecated alertData field when cleaning up
// code https://github.com/elastic/kibana/issues/123183
// This code is to support the deprecated alertData prop
const toAttach = useMemo((): CaseAttachments | undefined => {
if (attachments !== undefined || alertData !== undefined) {
const _toAttach = attachments ?? [];
if (alertData !== undefined) {
_toAttach.push({ ...alertData, type: CommentType.alert });
}
return _toAttach;
}
}, [alertData, attachments]);

const columns = useCasesColumns({
dispatchUpdateCaseProperty,
filterStatus: filterOptions.status,
Expand All @@ -204,9 +168,6 @@ export const AllCasesList = React.memo<AllCasesListProps>(
userCanCrud,
connectors,
onRowClick,
attachments: toAttach,
postComment,
updateCase,
showSolutionColumn: !hasOwner && availableSolutions.length > 1,
});

Expand Down Expand Up @@ -243,7 +204,7 @@ export const AllCasesList = React.memo<AllCasesListProps>(
size="xs"
color="accent"
className="essentialAnimation"
$isShow={(isCasesLoading || isLoading || isCommentUpdating) && !isDataEmpty}
$isShow={(isCasesLoading || isLoading) && !isDataEmpty}
/>
<CasesTableFilters
countClosedCases={data.countClosedCases}
Expand All @@ -270,7 +231,7 @@ export const AllCasesList = React.memo<AllCasesListProps>(
goToCreateCase={onRowClick}
handleIsLoading={handleIsLoading}
isCasesLoading={isCasesLoading}
isCommentUpdating={isCommentUpdating}
isCommentUpdating={isCasesLoading}
isDataEmpty={isDataEmpty}
isSelectorView={isSelectorView}
onChange={tableOnChangeCallback}
Expand Down
23 changes: 1 addition & 22 deletions x-pack/plugins/cases/public/components/all_cases/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import { useApplicationCapabilities, useKibana } from '../../common/lib/kibana';
import { StatusContextMenu } from '../case_action_bar/status_context_menu';
import { TruncatedText } from '../truncated_text';
import { getConnectorIcon } from '../utils';
import { PostComment } from '../../containers/use_post_comment';
import { CaseAttachments } from '../../types';
import type { CasesOwners } from '../../client/helpers/can_use_cases';
import { useCasesFeatures } from '../cases_context/use_cases_features';

Expand Down Expand Up @@ -73,9 +71,6 @@ export interface GetCasesColumn {
userCanCrud: boolean;
connectors?: ActionConnector[];
onRowClick?: (theCase: Case) => void;
attachments?: CaseAttachments;
postComment?: (args: PostComment) => Promise<void>;
updateCase?: (newCase: Case) => void;

showSolutionColumn?: boolean;
}
Expand All @@ -89,9 +84,6 @@ export const useCasesColumns = ({
userCanCrud,
connectors = [],
onRowClick,
attachments,
postComment,
updateCase,
showSolutionColumn,
}: GetCasesColumn): CasesColumns[] => {
// Delete case
Expand Down Expand Up @@ -141,24 +133,11 @@ export const useCasesColumns = ({

const assignCaseAction = useCallback(
async (theCase: Case) => {
// TODO currently the API only supports to add a comment at the time
// once the API is updated we should use bulk post comment #124814
// this operation is intentionally made in sequence
if (attachments !== undefined && attachments.length > 0) {
for (const attachment of attachments) {
await postComment?.({
caseId: theCase.id,
data: attachment,
});
}
updateCase?.(theCase);
}

if (onRowClick) {
onRowClick(theCase);
}
},
[attachments, onRowClick, postComment, updateCase]
[onRowClick]
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import { mount } from 'enzyme';
import { AllCasesSelectorModal } from '.';
import { TestProviders } from '../../../common/mock';
import { AllCasesList } from '../all_cases_list';
import { SECURITY_SOLUTION_OWNER } from '../../../../common/constants';

jest.mock('../all_cases_list');

const onRowClick = jest.fn();
const defaultProps = {
onRowClick,
};
const updateCase = jest.fn();

describe('AllCasesSelectorModal', () => {
beforeEach(() => {
Expand Down Expand Up @@ -50,17 +48,7 @@ describe('AllCasesSelectorModal', () => {
it('pass the correct props to getAllCases method', () => {
const fullProps = {
...defaultProps,
alertData: {
rule: {
id: 'rule-id',
name: 'rule',
},
index: 'index-id',
alertId: 'alert-id',
owner: SECURITY_SOLUTION_OWNER,
},
hiddenStatuses: [],
updateCase,
};

mount(
Expand All @@ -72,10 +60,8 @@ describe('AllCasesSelectorModal', () => {
// @ts-ignore idk what this mock style is but it works ¯\_(ツ)_/¯
expect(AllCasesList.type.mock.calls[0][0]).toEqual(
expect.objectContaining({
alertData: fullProps.alertData,
hiddenStatuses: fullProps.hiddenStatuses,
isSelectorView: true,
updateCase,
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ import {
} from '@elastic/eui';
import styled from 'styled-components';
import { Case, CaseStatusWithAllStatus } from '../../../../common/ui/types';
import { CommentRequestAlertType } from '../../../../common/api';
import * as i18n from '../../../common/translations';
import { AllCasesList } from '../all_cases_list';
import { CaseAttachments } from '../../../types';

export interface AllCasesSelectorModalProps {
/**
* @deprecated Use the attachments prop instead
*/
alertData?: Omit<CommentRequestAlertType, 'type'>;
hiddenStatuses?: CaseStatusWithAllStatus[];
onRowClick?: (theCase?: Case) => void;
updateCase?: (newCase: Case) => void;
onClose?: () => void;
attachments?: CaseAttachments;
}

const Modal = styled(EuiModal)`
Expand All @@ -40,7 +33,7 @@ const Modal = styled(EuiModal)`
`;

export const AllCasesSelectorModal = React.memo<AllCasesSelectorModalProps>(
({ alertData, attachments, hiddenStatuses, onRowClick, updateCase, onClose }) => {
({ hiddenStatuses, onRowClick, onClose }) => {
const [isModalOpen, setIsModalOpen] = useState<boolean>(true);
const closeModal = useCallback(() => {
if (onClose) {
Expand All @@ -66,12 +59,9 @@ export const AllCasesSelectorModal = React.memo<AllCasesSelectorModalProps>(
</EuiModalHeader>
<EuiModalBody>
<AllCasesList
alertData={alertData}
attachments={attachments}
hiddenStatuses={hiddenStatuses}
isSelectorView={true}
onRowClick={onClick}
updateCase={updateCase}
/>
</EuiModalBody>
<EuiModalFooter>
Expand Down
Loading