forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Refactor attach alert to new case flyout (elastic#125505)
Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
ffc3351
commit 2c15756
Showing
28 changed files
with
530 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/cases/public/components/cases_context/cases_context_reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { CreateCaseFlyoutProps } from '../create/flyout'; | ||
|
||
export const getInitialCasesContextState = (): CasesContextState => { | ||
return { | ||
createCaseFlyout: { | ||
isFlyoutOpen: false, | ||
}, | ||
}; | ||
}; | ||
|
||
export interface CasesContextState { | ||
createCaseFlyout: { | ||
isFlyoutOpen: boolean; | ||
props?: CreateCaseFlyoutProps; | ||
}; | ||
} | ||
|
||
export enum CasesContextStoreActionsList { | ||
OPEN_CREATE_CASE_FLYOUT, | ||
CLOSE_CREATE_CASE_FLYOUT, | ||
} | ||
export type CasesContextStoreAction = | ||
| { | ||
type: CasesContextStoreActionsList.OPEN_CREATE_CASE_FLYOUT; | ||
payload: CreateCaseFlyoutProps; | ||
} | ||
| { type: CasesContextStoreActionsList.CLOSE_CREATE_CASE_FLYOUT }; | ||
|
||
export const casesContextReducer: React.Reducer<CasesContextState, CasesContextStoreAction> = ( | ||
state: CasesContextState, | ||
action: CasesContextStoreAction | ||
): CasesContextState => { | ||
switch (action.type) { | ||
case CasesContextStoreActionsList.OPEN_CREATE_CASE_FLYOUT: { | ||
return { ...state, createCaseFlyout: { isFlyoutOpen: true, props: action.payload } }; | ||
} | ||
case CasesContextStoreActionsList.CLOSE_CREATE_CASE_FLYOUT: { | ||
return { ...state, createCaseFlyout: { isFlyoutOpen: false } }; | ||
} | ||
default: | ||
return state; | ||
} | ||
}; |
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/cases/public/components/cases_context/cases_global_components.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { AppMockRenderer, createAppMockRenderer } from '../../common/mock'; | ||
import { getCreateCaseFlyoutLazyNoProvider } from '../../methods/get_create_case_flyout'; | ||
import { CasesGlobalComponents } from './cases_global_components'; | ||
|
||
jest.mock('../../methods/get_create_case_flyout'); | ||
|
||
const getCreateCaseFlyoutLazyNoProviderMock = getCreateCaseFlyoutLazyNoProvider as jest.Mock; | ||
|
||
describe('Cases context UI', () => { | ||
let appMock: AppMockRenderer; | ||
|
||
beforeEach(() => { | ||
appMock = createAppMockRenderer(); | ||
getCreateCaseFlyoutLazyNoProviderMock.mockClear(); | ||
}); | ||
|
||
describe('create case flyout', () => { | ||
it('should render the create case flyout when isFlyoutOpen is true', async () => { | ||
const state = { | ||
createCaseFlyout: { | ||
isFlyoutOpen: true, | ||
props: { | ||
attachments: [], | ||
}, | ||
}, | ||
}; | ||
appMock.render(<CasesGlobalComponents state={state} />); | ||
expect(getCreateCaseFlyoutLazyNoProviderMock).toHaveBeenCalledWith({ attachments: [] }); | ||
}); | ||
it('should not render the create case flyout when isFlyoutOpen is false', async () => { | ||
const state = { | ||
createCaseFlyout: { | ||
isFlyoutOpen: false, | ||
}, | ||
}; | ||
appMock.render(<CasesGlobalComponents state={state} />); | ||
expect(getCreateCaseFlyoutLazyNoProviderMock).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/cases/public/components/cases_context/cases_global_components.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { getCreateCaseFlyoutLazyNoProvider } from '../../methods'; | ||
import { CasesContextState } from './cases_context_reducer'; | ||
|
||
export const CasesGlobalComponents = React.memo(({ state }: { state: CasesContextState }) => { | ||
return ( | ||
<> | ||
{state.createCaseFlyout.isFlyoutOpen && state.createCaseFlyout.props !== undefined | ||
? getCreateCaseFlyoutLazyNoProvider(state.createCaseFlyout.props) | ||
: null} | ||
</> | ||
); | ||
}); | ||
CasesGlobalComponents.displayName = 'CasesContextUi'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...k/plugins/cases/public/components/create/flyout/use_cases_add_to_new_case_flyout.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/* eslint-disable react/display-name */ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
import { CasesContext } from '../../cases_context'; | ||
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer'; | ||
import { useCasesAddToNewCaseFlyout } from './use_cases_add_to_new_case_flyout'; | ||
|
||
describe('use cases add to new case flyout hook', () => { | ||
const dispatch = jest.fn(); | ||
let wrapper: React.FC; | ||
beforeEach(() => { | ||
dispatch.mockReset(); | ||
wrapper = ({ children }) => { | ||
return ( | ||
<CasesContext.Provider | ||
value={{ | ||
owner: ['test'], | ||
userCanCrud: true, | ||
appId: 'test', | ||
appTitle: 'jest', | ||
basePath: '/jest', | ||
dispatch, | ||
features: { alerts: { sync: true }, metrics: [] }, | ||
}} | ||
> | ||
{children} | ||
</CasesContext.Provider> | ||
); | ||
}; | ||
}); | ||
|
||
it('should throw if called outside of a cases context', () => { | ||
const { result } = renderHook(() => { | ||
useCasesAddToNewCaseFlyout({}); | ||
}); | ||
expect(result.error?.message).toContain( | ||
'useCasesContext must be used within a CasesProvider and have a defined value' | ||
); | ||
}); | ||
|
||
it('should dispatch the open action when invoked', () => { | ||
const { result } = renderHook( | ||
() => { | ||
return useCasesAddToNewCaseFlyout({}); | ||
}, | ||
{ wrapper } | ||
); | ||
result.current.open(); | ||
expect(dispatch).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
type: CasesContextStoreActionsList.OPEN_CREATE_CASE_FLYOUT, | ||
}) | ||
); | ||
}); | ||
|
||
it('should dispatch the close action when invoked', () => { | ||
const { result } = renderHook( | ||
() => { | ||
return useCasesAddToNewCaseFlyout({}); | ||
}, | ||
{ wrapper } | ||
); | ||
result.current.close(); | ||
expect(dispatch).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
type: CasesContextStoreActionsList.CLOSE_CREATE_CASE_FLYOUT, | ||
}) | ||
); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/cases/public/components/create/flyout/use_cases_add_to_new_case_flyout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useCallback } from 'react'; | ||
import { CasesContextStoreActionsList } from '../../cases_context/cases_context_reducer'; | ||
import { useCasesContext } from '../../cases_context/use_cases_context'; | ||
import { CreateCaseFlyoutProps } from './create_case_flyout'; | ||
|
||
export const useCasesAddToNewCaseFlyout = (props: CreateCaseFlyoutProps) => { | ||
const context = useCasesContext(); | ||
|
||
const closeFlyout = useCallback(() => { | ||
context.dispatch({ | ||
type: CasesContextStoreActionsList.CLOSE_CREATE_CASE_FLYOUT, | ||
}); | ||
}, [context]); | ||
|
||
const openFlyout = useCallback(() => { | ||
context.dispatch({ | ||
type: CasesContextStoreActionsList.OPEN_CREATE_CASE_FLYOUT, | ||
payload: { | ||
...props, | ||
onClose: () => { | ||
closeFlyout(); | ||
if (props.onClose) { | ||
return props.onClose(); | ||
} | ||
}, | ||
afterCaseCreated: async (...args) => { | ||
closeFlyout(); | ||
if (props.afterCaseCreated) { | ||
return props.afterCaseCreated(...args); | ||
} | ||
}, | ||
}, | ||
}); | ||
}, [closeFlyout, context, props]); | ||
return { | ||
open: openFlyout, | ||
close: closeFlyout, | ||
}; | ||
}; | ||
|
||
export type UseCasesAddToNewCaseFlyout = typeof useCasesAddToNewCaseFlyout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.