Skip to content

Commit

Permalink
[Maps] clean up uses of any in redux actions and kibana services (#79737
Browse files Browse the repository at this point in the history
)

* [Maps] clean up uses of any in redux actions and kibana services

* API doc changes and updated IndexPatternSelect type

* tslint errors in OSS code

* API updates

* remove IndexPatternSelectPublicProps and create IndexPatternSelectInternalProps instead

* include changes to index_pattern_select

* API updates

* remove savedObjectClient from IndexPatternSelectProps

* update types for lazy load component

* remove unused import

* export type

* another API clean-up

* revert changes to import in data/public/types

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored Oct 8, 2020
1 parent 14b02a3 commit 1cefc8e
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<b>Signature:</b>

```typescript
export declare type IndexPatternSelectProps = Required<Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions'>, 'onChange' | 'placeholder'> & {
export declare type IndexPatternSelectProps = Required<Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions' | 'onChange'>, 'placeholder'> & {
onChange: (indexPatternId?: string) => void;
indexPatternId: string;
fieldTypes?: string[];
onNoIndexPatterns?: () => void;
savedObjectsClient: SavedObjectsClientContract;
};
```
1 change: 0 additions & 1 deletion examples/search_examples/public/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ export const SearchExamplesApp = ({
<EuiFlexItem>
<EuiFormLabel>Index Pattern</EuiFormLabel>
<IndexPatternSelect
savedObjectsClient={savedObjectsClient}
placeholder={i18n.translate(
'backgroundSessionExample.selectIndexPatternPlaceholder',
{
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1290,11 +1290,11 @@ export type IndexPatternsContract = PublicMethodsOf<IndexPatternsService>;
// Warning: (ae-missing-release-tag) "IndexPatternSelectProps" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export type IndexPatternSelectProps = Required<Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions'>, 'onChange' | 'placeholder'> & {
export type IndexPatternSelectProps = Required<Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions' | 'onChange'>, 'placeholder'> & {
onChange: (indexPatternId?: string) => void;
indexPatternId: string;
fieldTypes?: string[];
onNoIndexPatterns?: () => void;
savedObjectsClient: SavedObjectsClientContract;
};

// Warning: (ae-missing-release-tag) "IndexPatternSpec" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { IndexPatternSelect, IndexPatternSelectProps } from './';

// Takes in stateful runtime dependencies and pre-wires them to the component
export function createIndexPatternSelect(savedObjectsClient: SavedObjectsClientContract) {
return (props: Omit<IndexPatternSelectProps, 'savedObjectsClient'>) => (
return (props: IndexPatternSelectProps) => (
<IndexPatternSelect {...props} savedObjectsClient={savedObjectsClient} />
);
}
4 changes: 2 additions & 2 deletions src/plugins/data/public/ui/index_pattern_select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import React from 'react';
import { EuiLoadingContent, EuiDelayRender } from '@elastic/eui';
import type { IndexPatternSelectProps } from './index_pattern_select';
import type { IndexPatternSelectInternalProps } from './index_pattern_select';

const Fallback = () => (
<EuiDelayRender>
Expand All @@ -28,7 +28,7 @@ const Fallback = () => (
);

const LazyIndexPatternSelect = React.lazy(() => import('./index_pattern_select'));
export const IndexPatternSelect = (props: IndexPatternSelectProps) => (
export const IndexPatternSelect = (props: IndexPatternSelectInternalProps) => (
<React.Suspense fallback={<Fallback />}>
<LazyIndexPatternSelect {...props} />
</React.Suspense>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ import { SavedObjectsClientContract, SimpleSavedObject } from 'src/core/public';
import { getTitle } from '../../../common/index_patterns/lib';

export type IndexPatternSelectProps = Required<
Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions'>,
'onChange' | 'placeholder'
Omit<
EuiComboBoxProps<any>,
'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions' | 'onChange'
>,
'placeholder'
> & {
onChange: (indexPatternId?: string) => void;
indexPatternId: string;
fieldTypes?: string[];
onNoIndexPatterns?: () => void;
};

export type IndexPatternSelectInternalProps = IndexPatternSelectProps & {
savedObjectsClient: SavedObjectsClientContract;
};

Expand Down Expand Up @@ -60,11 +67,11 @@ const getIndexPatterns = async (

// Needed for React.lazy
// eslint-disable-next-line import/no-default-export
export default class IndexPatternSelect extends Component<IndexPatternSelectProps> {
export default class IndexPatternSelect extends Component<IndexPatternSelectInternalProps> {
private isMounted: boolean = false;
state: IndexPatternSelectState;

constructor(props: IndexPatternSelectProps) {
constructor(props: IndexPatternSelectInternalProps) {
super(props);

this.state = {
Expand All @@ -86,7 +93,7 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp
this.fetchSelectedIndexPattern(this.props.indexPatternId);
}

UNSAFE_componentWillReceiveProps(nextProps: IndexPatternSelectProps) {
UNSAFE_componentWillReceiveProps(nextProps: IndexPatternSelectInternalProps) {
if (nextProps.indexPatternId !== this.props.indexPatternId) {
this.fetchSelectedIndexPattern(nextProps.indexPatternId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ function IndexPatternSelectFormRowUi(props: IndexPatternSelectFormRowUiProps) {
indexPatternId={indexPatternId}
onChange={onChange}
data-test-subj={selectId}
// TODO: supply actual savedObjectsClient here
savedObjectsClient={{} as any}
/>
</EuiFormRow>
);
Expand Down
80 changes: 51 additions & 29 deletions x-pack/plugins/maps/public/actions/data_request_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
/* eslint-disable @typescript-eslint/consistent-type-definitions */

import { Dispatch } from 'redux';
import { AnyAction, Dispatch } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import bbox from '@turf/bbox';
import uuid from 'uuid/v4';
import { multiPoint } from '@turf/helpers';
Expand Down Expand Up @@ -70,9 +71,12 @@ export function clearDataRequests(layer: ILayer) {
}

export function cancelAllInFlightRequests() {
return (dispatch: Dispatch, getState: () => MapStoreState) => {
return (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
getLayerList(getState()).forEach((layer) => {
dispatch<any>(clearDataRequests(layer));
dispatch(clearDataRequests(layer));
});
};
}
Expand Down Expand Up @@ -100,20 +104,20 @@ export function updateStyleMeta(layerId: string | null) {
}

function getDataRequestContext(
dispatch: Dispatch,
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState,
layerId: string
): DataRequestContext {
return {
dataFilters: getDataFilters(getState()),
startLoading: (dataId: string, requestToken: symbol, meta: DataMeta) =>
dispatch<any>(startDataLoad(layerId, dataId, requestToken, meta)),
dispatch(startDataLoad(layerId, dataId, requestToken, meta)),
stopLoading: (dataId: string, requestToken: symbol, data: object, meta: DataMeta) =>
dispatch<any>(endDataLoad(layerId, dataId, requestToken, data, meta)),
dispatch(endDataLoad(layerId, dataId, requestToken, data, meta)),
onLoadError: (dataId: string, requestToken: symbol, errorMessage: string) =>
dispatch<any>(onDataLoadError(layerId, dataId, requestToken, errorMessage)),
dispatch(onDataLoadError(layerId, dataId, requestToken, errorMessage)),
updateSourceData: (newData: unknown) => {
dispatch<any>(updateSourceDataRequest(layerId, newData));
dispatch(updateSourceDataRequest(layerId, newData));
},
isRequestStillActive: (dataId: string, requestToken: symbol) => {
const dataRequest = getDataRequestDescriptor(getState(), layerId, dataId);
Expand All @@ -128,22 +132,28 @@ function getDataRequestContext(
}

export function syncDataForAllLayers() {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
const syncPromises = getLayerList(getState()).map((layer) => {
return dispatch<any>(syncDataForLayer(layer));
return dispatch(syncDataForLayer(layer));
});
await Promise.all(syncPromises);
};
}

function syncDataForAllJoinLayers() {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
const syncPromises = getLayerList(getState())
.filter((layer) => {
return 'hasJoins' in layer ? (layer as IVectorLayer).hasJoins() : false;
})
.map((layer) => {
return dispatch<any>(syncDataForLayer(layer));
return dispatch(syncDataForLayer(layer));
});
await Promise.all(syncPromises);
};
Expand All @@ -160,10 +170,13 @@ export function syncDataForLayer(layer: ILayer) {
}

export function syncDataForLayerId(layerId: string | null) {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
const layer = getLayerById(layerId, getState());
if (layer) {
dispatch<any>(syncDataForLayer(layer));
dispatch(syncDataForLayer(layer));
}
};
}
Expand All @@ -180,10 +193,13 @@ function setLayerDataLoadErrorStatus(layerId: string, errorMessage: string | nul
}

function startDataLoad(layerId: string, dataId: string, requestToken: symbol, meta: DataMeta) {
return (dispatch: Dispatch, getState: () => MapStoreState) => {
return (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
const layer = getLayerById(layerId, getState());
if (layer) {
dispatch<any>(cancelRequest(layer.getPrevRequestToken(dataId)));
dispatch(cancelRequest(layer.getPrevRequestToken(dataId)));
}

const eventHandlers = getEventHandlers(getState());
Expand Down Expand Up @@ -211,7 +227,10 @@ function endDataLoad(
data: object,
meta: DataMeta
) {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
dispatch(unregisterCancelCallback(requestToken));

const features = data && 'features' in data ? (data as FeatureCollection).features : [];
Expand All @@ -231,7 +250,7 @@ function endDataLoad(
});
}

dispatch<any>(cleanTooltipStateForLayer(layerId, features));
dispatch(cleanTooltipStateForLayer(layerId, features));
dispatch({
type: LAYER_DATA_LOAD_ENDED,
layerId,
Expand All @@ -244,9 +263,9 @@ function endDataLoad(
// Clear any data-load errors when there is a succesful data return.
// Co this on end-data-load iso at start-data-load to avoid blipping the error status between true/false.
// This avoids jitter in the warning icon of the TOC when the requests continues to return errors.
dispatch<any>(setLayerDataLoadErrorStatus(layerId, null));
dispatch(setLayerDataLoadErrorStatus(layerId, null));

dispatch<any>(updateStyleMeta(layerId));
dispatch(updateStyleMeta(layerId));
};
}

Expand All @@ -256,7 +275,10 @@ function onDataLoadError(
requestToken: symbol,
errorMessage: string
) {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
dispatch(unregisterCancelCallback(requestToken));

const eventHandlers = getEventHandlers(getState());
Expand All @@ -268,7 +290,7 @@ function onDataLoadError(
});
}

dispatch<any>(cleanTooltipStateForLayer(layerId));
dispatch(cleanTooltipStateForLayer(layerId));
dispatch({
type: LAYER_DATA_LOAD_ERROR,
data: null,
Expand All @@ -277,20 +299,20 @@ function onDataLoadError(
requestToken,
});

dispatch<any>(setLayerDataLoadErrorStatus(layerId, errorMessage));
dispatch(setLayerDataLoadErrorStatus(layerId, errorMessage));
};
}

export function updateSourceDataRequest(layerId: string, newData: unknown) {
return (dispatch: Dispatch) => {
return (dispatch: ThunkDispatch<MapStoreState, void, AnyAction>) => {
dispatch({
type: UPDATE_SOURCE_DATA_REQUEST,
dataId: SOURCE_DATA_REQUEST_ID,
layerId,
newData,
});

dispatch<any>(updateStyleMeta(layerId));
dispatch(updateStyleMeta(layerId));
};
}

Expand Down Expand Up @@ -385,7 +407,7 @@ export function fitToDataBounds(onNoBounds?: () => void) {

let lastSetQueryCallId: string = '';
export function autoFitToBounds() {
return async (dispatch: Dispatch) => {
return async (dispatch: ThunkDispatch<MapStoreState, void, AnyAction>) => {
// Method can be triggered before async actions complete
// Use localSetQueryCallId to only continue execution path if method has not been re-triggered.
const localSetQueryCallId = uuid();
Expand All @@ -394,17 +416,17 @@ export function autoFitToBounds() {
// Joins are performed on the client.
// As a result, bounds for join layers must also be performed on the client.
// Therefore join layers need to fetch data prior to auto fitting bounds.
await dispatch<any>(syncDataForAllJoinLayers());
await dispatch(syncDataForAllJoinLayers());

if (localSetQueryCallId === lastSetQueryCallId) {
// In cases where there are no bounds, such as no matching documents, fitToDataBounds does not trigger setGotoWithBounds.
// Ensure layer syncing occurs when setGotoWithBounds is not triggered.
function onNoBounds() {
if (localSetQueryCallId === lastSetQueryCallId) {
dispatch<any>(syncDataForAllLayers());
dispatch(syncDataForAllLayers());
}
}
dispatch<any>(fitToDataBounds(onNoBounds));
dispatch(fitToDataBounds(onNoBounds));
}
};
}
Expand Down
Loading

0 comments on commit 1cefc8e

Please sign in to comment.