-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert Topology filters localStorage to userSettings
- Loading branch information
Showing
10 changed files
with
92 additions
and
100 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
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
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,70 @@ | ||
import * as React from 'react'; | ||
import { useUserSettings } from '@console/shared'; | ||
import { getDefaultAppliedFilters } from '../redux/reducer'; | ||
import { DEFAULT_TOPOLOGY_FILTERS } from './const'; | ||
import { getAppliedFilters } from '../redux/action'; | ||
import { DisplayFilters } from '../topology-types'; | ||
|
||
const TOPOLOGY_DISPLAY_FILTERS_CONFIG_MAP_KEY = `devcosole.topology.filters`; | ||
|
||
const getTopologyFilters = (appliedFilters) => { | ||
const filters = [...DEFAULT_TOPOLOGY_FILTERS]; | ||
filters.forEach((filter) => { | ||
if (appliedFilters[filter.id] !== undefined) { | ||
filter.value = appliedFilters[filter.id]; | ||
} | ||
}); | ||
|
||
return filters; | ||
}; | ||
|
||
type AppliedFilters = { [filterKey: string]: boolean }; | ||
type SetTopologyFilters = (filters: DisplayFilters) => void; | ||
|
||
const useFilterContextValues = (): [ | ||
DisplayFilters, | ||
AppliedFilters, | ||
boolean, | ||
SetTopologyFilters, | ||
] => { | ||
const [appliedFilters, setAppliedFilters, appliedFiltersLoaded] = useUserSettings( | ||
TOPOLOGY_DISPLAY_FILTERS_CONFIG_MAP_KEY, | ||
getDefaultAppliedFilters(), | ||
); | ||
const [filtersLoaded, setFiltersLoaded] = React.useState<boolean>(false); | ||
const [filters, setFilters] = React.useState<DisplayFilters>([]); | ||
|
||
React.useEffect(() => { | ||
if (appliedFiltersLoaded && !filtersLoaded) { | ||
setFilters(getTopologyFilters(appliedFilters)); | ||
setFiltersLoaded(true); | ||
} | ||
}, [appliedFilters, appliedFiltersLoaded, filtersLoaded]); | ||
|
||
const setTopologyFilters = React.useCallback( | ||
(displayFilters: DisplayFilters) => { | ||
setFilters(displayFilters); | ||
setAppliedFilters(getAppliedFilters(displayFilters)); | ||
}, | ||
[setAppliedFilters], | ||
); | ||
|
||
return [filters, appliedFilters, appliedFilters && filtersLoaded, setTopologyFilters]; | ||
}; | ||
|
||
type FilterContextType = { | ||
filters?: DisplayFilters; | ||
appliedFilters?: AppliedFilters; | ||
setTopologyFilters?: SetTopologyFilters; | ||
}; | ||
|
||
export const FilterContext = React.createContext<FilterContextType>({}); | ||
|
||
export const FilterProvider: React.FC = ({ children }) => { | ||
const [filters, appliedFilters, loaded, setTopologyFilters] = useFilterContextValues(); | ||
return ( | ||
<FilterContext.Provider value={{ filters, appliedFilters, setTopologyFilters }}> | ||
{loaded ? children : null} | ||
</FilterContext.Provider> | ||
); | ||
}; |
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
13 changes: 4 additions & 9 deletions
13
frontend/packages/topology/src/filters/useAppliedDisplayFilters.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 |
---|---|---|
@@ -1,13 +1,8 @@ | ||
// FIXME upgrading redux types is causing many errors at this time | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
import { useSelector } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { DisplayFilters } from '../topology-types'; | ||
import { getAppliedTopologyFilters } from './filter-utils'; | ||
import { useContext } from 'react'; | ||
import { FilterContext } from './FilterProvider'; | ||
|
||
const useAppliedDisplayFilters = (): DisplayFilters => { | ||
return useSelector((state: RootState) => getAppliedTopologyFilters(state)); | ||
const useAppliedDisplayFilters = (): { [filiterKey: string]: boolean } => { | ||
return useContext(FilterContext).appliedFilters; | ||
}; | ||
|
||
export { useAppliedDisplayFilters }; |
10 changes: 3 additions & 7 deletions
10
frontend/packages/topology/src/filters/useDisplayFilters.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
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