-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 useGetCases to use react-query #133505
Changes from 20 commits
5691e40
181a3b5
9280332
9cd7cb6
b63fc1f
08a17a0
9c83ab4
8ed85c8
c02acac
4b5dab6
74cbacc
82bca36
c8ee55a
b63e63b
9e6c917
1c91720
2453d45
20df8b9
c35f5e2
15e167a
64abe42
24ee910
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,11 @@ import { | |
Case, | ||
CaseStatusWithAllStatus, | ||
FilterOptions, | ||
QueryParams, | ||
SortFieldCase, | ||
StatusAll, | ||
} from '../../../common/ui/types'; | ||
import { CaseStatuses, caseStatuses } from '../../../common/api'; | ||
import { useGetCases } from '../../containers/use_get_cases'; | ||
|
||
import { useAvailableCasesOwners } from '../app/use_available_owners'; | ||
import { useCasesColumns } from './columns'; | ||
|
@@ -28,6 +29,12 @@ import { CasesTable } from './table'; | |
import { useCasesContext } from '../cases_context/use_cases_context'; | ||
import { CasesMetrics } from './cases_metrics'; | ||
import { useGetConnectors } from '../../containers/configure/use_connectors'; | ||
import { | ||
DEFAULT_FILTER_OPTIONS, | ||
DEFAULT_QUERY_PARAMS, | ||
initialData, | ||
useGetCases, | ||
} from '../../containers/use_get_cases'; | ||
|
||
const ProgressLoader = styled(EuiProgress)` | ||
${({ $isShow }: { $isShow: boolean }) => | ||
|
@@ -65,19 +72,21 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
...(!isEmpty(hiddenStatuses) && firstAvailableStatus && { status: firstAvailableStatus }), | ||
owner: hasOwner ? owner : availableSolutions, | ||
}; | ||
const [filterOptions, setFilterOptions] = useState<FilterOptions>({ | ||
...DEFAULT_FILTER_OPTIONS, | ||
...initialFilterOptions, | ||
}); | ||
const [queryParams, setQueryParams] = useState<QueryParams>(DEFAULT_QUERY_PARAMS); | ||
const [selectedCases, setSelectedCases] = useState<Case[]>([]); | ||
|
||
const { | ||
data, | ||
dispatchUpdateCaseProperty, | ||
data = initialData, | ||
isFetching: isLoadingCases, | ||
refetch: refetchCases, | ||
} = useGetCases({ | ||
filterOptions, | ||
loading, | ||
queryParams, | ||
selectedCases, | ||
refetchCases, | ||
setFilters, | ||
setQueryParams, | ||
setSelectedCases, | ||
} = useGetCases({ initialFilterOptions }); | ||
}); | ||
|
||
const { data: connectors = [] } = useGetConnectors(); | ||
|
||
|
@@ -147,30 +156,28 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
const onFilterChangedCallback = useCallback( | ||
(newFilterOptions: Partial<FilterOptions>) => { | ||
if (newFilterOptions.status && newFilterOptions.status === CaseStatuses.closed) { | ||
setQueryParams({ sortField: SortFieldCase.closedAt }); | ||
setQueryParams({ ...DEFAULT_QUERY_PARAMS, sortField: SortFieldCase.closedAt }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use the previous state?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct! thanks :D |
||
} else if (newFilterOptions.status && newFilterOptions.status === CaseStatuses.open) { | ||
setQueryParams({ sortField: SortFieldCase.createdAt }); | ||
setQueryParams({ ...DEFAULT_QUERY_PARAMS, sortField: SortFieldCase.createdAt }); | ||
} else if ( | ||
newFilterOptions.status && | ||
newFilterOptions.status === CaseStatuses['in-progress'] | ||
) { | ||
setQueryParams({ sortField: SortFieldCase.createdAt }); | ||
setQueryParams({ ...DEFAULT_QUERY_PARAMS, sortField: SortFieldCase.createdAt }); | ||
} | ||
|
||
deselectCases(); | ||
setFilters(newFilterOptions); | ||
setFilterOptions({ ...DEFAULT_FILTER_OPTIONS, ...newFilterOptions }); | ||
refreshCases(false); | ||
}, | ||
[deselectCases, setFilters, refreshCases, setQueryParams] | ||
[deselectCases, setFilterOptions, refreshCases, setQueryParams] | ||
); | ||
|
||
const showActions = userCanCrud && !isSelectorView; | ||
|
||
const columns = useCasesColumns({ | ||
dispatchUpdateCaseProperty, | ||
filterStatus: filterOptions.status, | ||
filterStatus: filterOptions.status ?? StatusAll, | ||
handleIsLoading, | ||
isLoadingCases: loading, | ||
refreshCases, | ||
isSelectorView, | ||
userCanCrud, | ||
|
@@ -181,9 +188,9 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
|
||
const pagination = useMemo( | ||
() => ({ | ||
pageIndex: queryParams.page - 1, | ||
pageSize: queryParams.perPage, | ||
totalItemCount: data.total, | ||
pageIndex: (queryParams?.page ?? DEFAULT_QUERY_PARAMS.page) - 1, | ||
pageSize: queryParams?.perPage ?? DEFAULT_QUERY_PARAMS.perPage, | ||
totalItemCount: data.total ?? 0, | ||
pageSizeOptions: [5, 10, 15, 20, 25], | ||
}), | ||
[data, queryParams] | ||
|
@@ -196,7 +203,6 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
}), | ||
[selectedCases, setSelectedCases] | ||
); | ||
const isCasesLoading = useMemo(() => loading.indexOf('cases') > -1, [loading]); | ||
const isDataEmpty = useMemo(() => data.total === 0, [data]); | ||
|
||
const tableRowProps = useCallback( | ||
|
@@ -212,7 +218,7 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
size="xs" | ||
color="accent" | ||
className="essentialAnimation" | ||
$isShow={(isCasesLoading || isLoading) && !isDataEmpty} | ||
$isShow={isLoading || isLoadingCases} | ||
/> | ||
{!isSelectorView ? <CasesMetrics refresh={refresh} /> : null} | ||
<CasesTableFilters | ||
|
@@ -240,8 +246,8 @@ export const AllCasesList = React.memo<AllCasesListProps>( | |
filterOptions={filterOptions} | ||
goToCreateCase={onRowClick} | ||
handleIsLoading={handleIsLoading} | ||
isCasesLoading={isCasesLoading} | ||
isCommentUpdating={isCasesLoading} | ||
isCasesLoading={isLoadingCases && data.cases.length === 0} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will happen if the are no cases in the response? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually a mistake. I removed the length check. |
||
isCommentUpdating={isLoadingCases} | ||
isDataEmpty={isDataEmpty} | ||
isSelectorView={isSelectorView} | ||
onChange={tableOnChangeCallback} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ import { | |
import { RIGHT_ALIGNMENT } from '@elastic/eui/lib/services'; | ||
import styled from 'styled-components'; | ||
|
||
import { Case, DeleteCase } from '../../../common/ui/types'; | ||
import { Case, DeleteCase, UpdateByKey } from '../../../common/ui/types'; | ||
import { CaseStatuses, ActionConnector, CaseSeverity } from '../../../common/api'; | ||
import { OWNER_INFO } from '../../../common/constants'; | ||
import { getEmptyTagValue } from '../empty_value'; | ||
|
@@ -33,7 +33,6 @@ import { CaseDetailsLink } from '../links'; | |
import * as i18n from './translations'; | ||
import { ALERTS } from '../../common/translations'; | ||
import { getActions } from './actions'; | ||
import { UpdateCase } from '../../containers/use_get_cases'; | ||
import { useDeleteCases } from '../../containers/use_delete_cases'; | ||
import { ConfirmDeleteCaseModal } from '../confirm_delete_case'; | ||
import { useApplicationCapabilities, useKibana } from '../../common/lib/kibana'; | ||
|
@@ -43,6 +42,7 @@ import { getConnectorIcon } from '../utils'; | |
import type { CasesOwners } from '../../client/helpers/can_use_cases'; | ||
import { useCasesFeatures } from '../cases_context/use_cases_features'; | ||
import { severities } from '../severity/config'; | ||
import { useUpdateCase } from '../../containers/use_update_case'; | ||
|
||
export type CasesColumns = | ||
| EuiTableActionsColumnType<Case> | ||
|
@@ -57,10 +57,8 @@ const renderStringField = (field: string, dataTestSubj: string) => | |
field != null ? <span data-test-subj={dataTestSubj}>{field}</span> : getEmptyTagValue(); | ||
|
||
export interface GetCasesColumn { | ||
dispatchUpdateCaseProperty: (u: UpdateCase) => void; | ||
filterStatus: string; | ||
handleIsLoading: (a: boolean) => void; | ||
isLoadingCases: string[]; | ||
refreshCases?: (a?: boolean) => void; | ||
isSelectorView: boolean; | ||
userCanCrud: boolean; | ||
|
@@ -70,10 +68,8 @@ export interface GetCasesColumn { | |
showSolutionColumn?: boolean; | ||
} | ||
export const useCasesColumns = ({ | ||
dispatchUpdateCaseProperty, | ||
filterStatus, | ||
handleIsLoading, | ||
isLoadingCases, | ||
refreshCases, | ||
isSelectorView, | ||
userCanCrud, | ||
|
@@ -98,6 +94,8 @@ export const useCasesColumns = ({ | |
title: '', | ||
}); | ||
|
||
const { updateCaseProperty, isLoading: isLoadingUpdateCase } = useUpdateCase(); | ||
|
||
const toggleDeleteModal = useCallback( | ||
(deleteCase: Case) => { | ||
handleToggleModal(); | ||
|
@@ -107,15 +105,17 @@ export const useCasesColumns = ({ | |
); | ||
|
||
const handleDispatchUpdate = useCallback( | ||
(args: Omit<UpdateCase, 'refetchCasesStatus'>) => { | ||
dispatchUpdateCaseProperty({ | ||
...args, | ||
refetchCasesStatus: () => { | ||
({ updateKey, updateValue, caseData }: UpdateByKey) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of the main logic-wise changes in this PR. The previous useGetCases hook was providing a "sub-hook" to update a case and I had to remove that. Instead I reused the existing updateCaseproperty hook. |
||
updateCaseProperty({ | ||
updateKey, | ||
updateValue, | ||
caseData, | ||
onSuccess: () => { | ||
if (refreshCases != null) refreshCases(); | ||
}, | ||
}); | ||
}, | ||
[dispatchUpdateCaseProperty, refreshCases] | ||
[refreshCases, updateCaseProperty] | ||
); | ||
|
||
const actions = useMemo( | ||
|
@@ -136,8 +136,8 @@ export const useCasesColumns = ({ | |
); | ||
|
||
useEffect(() => { | ||
handleIsLoading(isDeleting || isLoadingCases.indexOf('caseUpdate') > -1); | ||
}, [handleIsLoading, isDeleting, isLoadingCases]); | ||
handleIsLoading(isDeleting || isLoadingUpdateCase); | ||
}, [handleIsLoading, isDeleting, isLoadingUpdateCase]); | ||
|
||
useEffect(() => { | ||
if (isDeleted) { | ||
|
@@ -319,13 +319,12 @@ export const useCasesColumns = ({ | |
return ( | ||
<StatusContextMenu | ||
currentStatus={theCase.status} | ||
disabled={!userCanCrud || isLoadingCases.length > 0} | ||
disabled={!userCanCrud || isLoadingUpdateCase} | ||
onStatusChanged={(status) => | ||
handleDispatchUpdate({ | ||
updateKey: 'status', | ||
updateValue: status, | ||
caseId: theCase.id, | ||
version: theCase.version, | ||
caseData: theCase, | ||
}) | ||
} | ||
/> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the second logic change in the PR. the previous hook was working both as a fetch data hook and state management. I removed the state management part from it and move it here.