-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Style the "back to cluster button" in ChecksResult view The style of back to cluster CTA changes * Add filters to ChecksResult screen
- Loading branch information
Showing
3 changed files
with
110 additions
and
12 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
81 changes: 81 additions & 0 deletions
81
assets/js/components/ClusterDetails/ChecksResultFilters.jsx
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,81 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import Filter from '@components/Table/Filter'; | ||
|
||
export const RESULT_FILTER_FIELD = 'result'; | ||
|
||
export const useFilteredChecks = (cluster) => { | ||
const [filtersPredicates, setFiltersPredicates] = useState([]); | ||
const [filteredChecks, setFilteredChecks] = useState([]); | ||
|
||
const filterChecks = (checks, predicates) => { | ||
if (predicates.length === 0) return checks; | ||
|
||
return checks.filter((check) => | ||
predicates.some((predicate) => predicate(check)) | ||
); | ||
}; | ||
|
||
const checksForHost = (hostID) => | ||
filteredChecks | ||
.filter((result) => result.host_id === hostID) | ||
.map((result) => result.check_id); | ||
|
||
useEffect(() => { | ||
if (cluster?.checks_results.length > 0) { | ||
const selectedCheckResults = cluster?.checks_results.filter((result) => | ||
cluster?.selected_checks.includes(result?.check_id) | ||
); | ||
|
||
setFilteredChecks(filterChecks(selectedCheckResults, filtersPredicates)); | ||
} | ||
}, [cluster?.checks_results, cluster?.selected_checks, filtersPredicates]); | ||
|
||
return { | ||
setFiltersPredicates, | ||
filteredChecksyByHost: checksForHost, | ||
}; | ||
}; | ||
|
||
const ChecksResultFilters = ({ onChange }) => { | ||
const [filtersForField, setFiltersForField] = useState({}); | ||
|
||
// This structure is the foundation for a multi field filters | ||
// we can reuse later this structure in other parts of the application | ||
|
||
useEffect(() => { | ||
if (Object.keys(filtersForField).length >= 0) { | ||
const filtersToApply = Object.keys(filtersForField).reduce( | ||
(acc, curr) => { | ||
return [...acc, ...filtersForField[curr].predicates]; | ||
}, | ||
[] | ||
); | ||
|
||
onChange(filtersToApply); | ||
} | ||
}, [filtersForField]); | ||
|
||
return ( | ||
<div className="flex"> | ||
<Filter | ||
key={RESULT_FILTER_FIELD} | ||
title={'checks result'} | ||
options={['passing', 'warning', 'critical', 'unknown']} | ||
value={filtersForField[RESULT_FILTER_FIELD]?.values || []} | ||
onChange={(list) => { | ||
setFiltersForField((existingFilters) => ({ | ||
...existingFilters, | ||
[RESULT_FILTER_FIELD]: { | ||
predicates: list.map( | ||
(value) => (checks) => checks[RESULT_FILTER_FIELD] === value | ||
), | ||
values: list, | ||
}, | ||
})); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ChecksResultFilters; |
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