Skip to content

Commit

Permalink
[D&D] Fix index pattern state and loading (opensearch-project#1949)
Browse files Browse the repository at this point in the history
Remove useIndexPattern hook in favor of useIndexPatterns

fixes opensearch-project#1917

Signed-off-by: Josh Romero <[email protected]>
  • Loading branch information
joshuarrrr authored and kavilla committed Jul 28, 2022
1 parent f4fbd31 commit f40e8e5
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { FieldSelectorField } from './field_selector_field';

import './field_selector.scss';
import { useTypedSelector } from '../../utils/state_management';
import { useIndexPattern } from '../../utils/use';
import { useIndexPatterns } from '../../utils/use';
import { getAvailableFields } from './utils';

interface IFieldCategories {
Expand All @@ -33,7 +33,7 @@ const META_FIELDS: string[] = [
];

export const FieldSelector = () => {
const indexPattern = useIndexPattern();
const indexPattern = useIndexPatterns().selected;
const fieldSearchValue = useTypedSelector((state) => state.visualization.searchField);
const [filteredFields, setFilteredFields] = useState<IndexPatternField[]>([]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { cloneDeep } from 'lodash';
import { useTypedDispatch, useTypedSelector } from '../../utils/state_management';
import { DefaultEditorAggParams } from '../../../../../vis_default_editor/public';
import { Title } from './title';
import { useIndexPattern, useVisualizationType } from '../../utils/use';
import { useIndexPatterns, useVisualizationType } from '../../utils/use';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { WizardServices } from '../../../types';
import { IAggType } from '../../../../../data/public';
Expand All @@ -20,7 +20,7 @@ export function SecondaryPanel() {
const [touched, setTouched] = useState(false);
const dispatch = useTypedDispatch();
const vizType = useVisualizationType();
const indexPattern = useIndexPattern();
const indexPattern = useIndexPatterns().selected;
const {
services: {
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
reorderAgg,
updateAggConfigParams,
} from '../../../utils/state_management/visualization_slice';
import { useIndexPattern } from '../../../utils/use/use_index_pattern';
import { useIndexPatterns } from '../../../utils/use/use_index_pattern';
import { useOpenSearchDashboards } from '../../../../../../opensearch_dashboards_react/public';
import { WizardServices } from '../../../../types';

Expand All @@ -31,7 +31,7 @@ export const useDropbox = (props: UseDropboxProps): DropboxProps => {
const { id: dropboxId, label, schema } = props;
const [validAggTypes, setValidAggTypes] = useState<string[]>([]);
const dispatch = useTypedDispatch();
const indexPattern = useIndexPattern();
const indexPattern = useIndexPatterns().selected;
const {
services: {
data: {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/wizard/public/application/components/top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getTopNavConfig } from '../utils/get_top_nav_config';
import { WizardServices } from '../../types';

import './top_nav.scss';
import { useIndexPattern, useSavedWizardVis } from '../utils/use';
import { useIndexPatterns, useSavedWizardVis } from '../utils/use';
import { useTypedSelector } from '../utils/state_management';

export const TopNav = () => {
Expand Down Expand Up @@ -48,7 +48,7 @@ export const TopNav = () => {
);
}, [hasUnappliedChanges, rootState, savedWizardVis, services, visualizationIdFromUrl]);

const indexPattern = useIndexPattern();
const indexPattern = useIndexPatterns().selected;

return (
<div className="wizTopNav">
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/wizard/public/application/utils/use/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
*/

export { useVisualizationType } from './use_visualization_type';
export { useIndexPattern, useIndexPatterns } from './use_index_pattern';
export { useIndexPatterns } from './use_index_pattern';
export { useSavedWizardVis } from './use_saved_wizard_vis';
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@ import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_re
import { WizardServices } from '../../../types';
import { useTypedSelector } from '../state_management';

export const useIndexPattern = (): IndexPattern | undefined => {
const { indexPattern: indexId = '' } = useTypedSelector((state) => state.visualization);
const [indexPattern, setIndexPattern] = useState<IndexPattern>();
const {
services: {
data: { indexPatterns },
},
} = useOpenSearchDashboards<WizardServices>();

useEffect(() => {
const handleIndexUpdate = async () => {
const currentIndex = await indexPatterns.get(indexId);
setIndexPattern(currentIndex);
};

handleIndexUpdate();
}, [indexId, indexPatterns]);

return indexPattern;
};

export const useIndexPatterns = () => {
const { indexPattern: indexId = '' } = useTypedSelector((state) => state.visualization);
const [indexPatterns, setIndexPatterns] = useState<IndexPattern[]>([]);
Expand Down

0 comments on commit f40e8e5

Please sign in to comment.