From f2616294e6939cc11e8a6d13864caed0be076a4e Mon Sep 17 00:00:00 2001 From: Jesse Yang Date: Tue, 2 Mar 2021 13:58:30 -1000 Subject: [PATCH] chore: convert chartReducer to TypeScript (#13374) --- .../{chartReducer.js => chartReducer.ts} | 17 +++++-- .../components/ControlPanelsContainer.tsx | 27 +++++++---- .../src/explore/reducers/getInitialState.ts | 36 +++++++------- superset-frontend/src/explore/types.ts | 47 +++++++++++++++++++ 4 files changed, 97 insertions(+), 30 deletions(-) rename superset-frontend/src/chart/{chartReducer.js => chartReducer.ts} (92%) create mode 100644 superset-frontend/src/explore/types.ts diff --git a/superset-frontend/src/chart/chartReducer.js b/superset-frontend/src/chart/chartReducer.ts similarity index 92% rename from superset-frontend/src/chart/chartReducer.js rename to superset-frontend/src/chart/chartReducer.ts index e502bb58dd..e517db28ae 100644 --- a/superset-frontend/src/chart/chartReducer.js +++ b/superset-frontend/src/chart/chartReducer.ts @@ -18,11 +18,12 @@ */ /* eslint camelcase: 0 */ import { t } from '@superset-ui/core'; +import { ChartState } from 'src/explore/types'; import { getFormDataFromControls } from 'src/explore/controlUtils'; import { now } from '../modules/dates'; import * as actions from './chartAction'; -export const chart = { +export const chart: ChartState = { id: 0, chartAlert: null, chartStatus: 'loading', @@ -30,14 +31,22 @@ export const chart = { chartUpdateEndTime: null, chartUpdateStartTime: 0, latestQueryFormData: {}, + sliceFormData: null, queryController: null, queriesResponse: null, triggerQuery: true, lastRendered: 0, }; -export default function chartReducer(charts = {}, action) { - const actionHandlers = { +type ChartActionHandler = (state: ChartState) => ChartState; + +type AnyChartAction = Record; + +export default function chartReducer( + charts: Record = {}, + action: AnyChartAction, +) { + const actionHandlers: Record = { [actions.ADD_CHART]() { return { ...chart, @@ -134,7 +143,7 @@ export default function chartReducer(charts = {}, action) { } const annotationQuery = { ...state.annotationQuery, - [action.annotation.name]: action.queryRequest, + [action.annotation.name]: action.queryController, }; return { ...state, diff --git a/superset-frontend/src/explore/components/ControlPanelsContainer.tsx b/superset-frontend/src/explore/components/ControlPanelsContainer.tsx index fc738e9d21..a53395aa4d 100644 --- a/superset-frontend/src/explore/components/ControlPanelsContainer.tsx +++ b/superset-frontend/src/explore/components/ControlPanelsContainer.tsx @@ -27,11 +27,6 @@ import { QueryFormData, DatasourceType, } from '@superset-ui/core'; - -import Tabs from 'src/common/components/Tabs'; -import Collapse from 'src/common/components/Collapse'; -import { PluginContext } from 'src/components/DynamicPlugins'; -import Loading from 'src/components/Loading'; import { ControlPanelSectionConfig, ControlState, @@ -39,11 +34,21 @@ import { ExpandedControlItem, InfoTooltipWithTrigger, } from '@superset-ui/chart-controls'; + +import Tabs from 'src/common/components/Tabs'; +import Collapse from 'src/common/components/Collapse'; +import { PluginContext } from 'src/components/DynamicPlugins'; +import Loading from 'src/components/Loading'; + +import { sectionsToRender } from 'src/explore/controlUtils'; +import { + ExploreActions, + exploreActions, +} from 'src/explore/actions/exploreActions'; +import { ExplorePageState } from 'src/explore/reducers/getInitialState'; + import ControlRow from './ControlRow'; import Control from './Control'; -import { sectionsToRender } from '../controlUtils'; -import { ExploreActions, exploreActions } from '../actions/exploreActions'; -import { ExploreState } from '../reducers/getInitialState'; export type ControlPanelsContainerProps = { actions: ExploreActions; @@ -312,8 +317,12 @@ class ControlPanelsContainer extends React.Component; +export type ExplorePageState = ReturnType; diff --git a/superset-frontend/src/explore/types.ts b/superset-frontend/src/explore/types.ts new file mode 100644 index 0000000000..37c46fe770 --- /dev/null +++ b/superset-frontend/src/explore/types.ts @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { QueryData, QueryFormData, AnnotationData } from '@superset-ui/core'; + +export { Slice, Chart } from 'src/types/Chart'; + +export type ChartStatus = + | 'loading' + | 'rendered' + | 'failed' + | 'stopped' + | 'success'; + +export interface ChartState { + id: number; + annotationData?: AnnotationData; + annotationError?: Record; + annotationQuery?: Record; + chartAlert: string | null; + chartStatus: ChartStatus | null; + chartStackTrace?: string | null; + chartUpdateEndTime: number | null; + chartUpdateStartTime: number; + lastRendered: number; + latestQueryFormData: Partial; + sliceFormData: QueryFormData | null; + queryController: AbortController | null; + queriesResponse: QueryData | null; + triggerQuery: boolean; + asyncJobId?: string; +}