diff --git a/superset-frontend/src/dashboard/actions/datasources.ts b/superset-frontend/src/dashboard/actions/datasources.ts index 17bffa3934d3..062eba17ed9a 100644 --- a/superset-frontend/src/dashboard/actions/datasources.ts +++ b/superset-frontend/src/dashboard/actions/datasources.ts @@ -16,18 +16,26 @@ * specific language governing permissions and limitations * under the License. */ - -import { Datasource } from 'src/dashboard/types'; +import { Dispatch } from 'redux'; +import { SupersetClient } from '@superset-ui/core'; +import { Datasource, RootState } from 'src/dashboard/types'; // update datasources index for Dashboard export enum DatasourcesAction { SET_DATASOURCES = 'SET_DATASOURCES', + SET_DATASOURCE = 'SET_DATASOURCE', } -export type DatasourcesActionPayload = { - type: DatasourcesAction.SET_DATASOURCES; - datasources: Datasource[] | null; -}; +export type DatasourcesActionPayload = + | { + type: DatasourcesAction.SET_DATASOURCES; + datasources: Datasource[] | null; + } + | { + type: DatasourcesAction.SET_DATASOURCE; + key: Datasource['uid']; + datasource: Datasource; + }; export function setDatasources(datasources: Datasource[] | null) { return { @@ -35,3 +43,26 @@ export function setDatasources(datasources: Datasource[] | null) { datasources, }; } + +export function setDatasource(datasource: Datasource, key: string) { + return { + type: DatasourcesAction.SET_DATASOURCES, + key, + datasource, + }; +} + +export function fetchDatasourceMetadata(key: string) { + return (dispatch: Dispatch, getState: () => RootState) => { + const { datasources } = getState(); + const datasource = datasources[key]; + + if (datasource) { + return dispatch(setDatasource(datasource, key)); + } + + return SupersetClient.get({ + endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`, + }).then(({ json }) => dispatch(setDatasource(json as Datasource, key))); + }; +} diff --git a/superset-frontend/src/dashboard/reducers/datasources.ts b/superset-frontend/src/dashboard/reducers/datasources.ts index fd93bc638848..864641645ed7 100644 --- a/superset-frontend/src/dashboard/reducers/datasources.ts +++ b/superset-frontend/src/dashboard/reducers/datasources.ts @@ -24,14 +24,20 @@ import { } from '../actions/datasources'; export default function datasourcesReducer( - state: DatasourcesState | undefined, + datasources: DatasourcesState | undefined, action: DatasourcesActionPayload, ) { if (action.type === DatasourcesAction.SET_DATASOURCES) { return { - ...state, + ...datasources, ...keyBy(action.datasources, 'uid'), }; } - return state || {}; + if (action.type === DatasourcesAction.SET_DATASOURCE) { + return { + ...datasources, + [action.key]: action.datasource, + }; + } + return datasources || {}; } diff --git a/superset-frontend/src/dashboard/types.ts b/superset-frontend/src/dashboard/types.ts index 37669e16d6b2..c2b316b9549b 100644 --- a/superset-frontend/src/dashboard/types.ts +++ b/superset-frontend/src/dashboard/types.ts @@ -75,7 +75,9 @@ export type DashboardInfo = { }; export type ChartsState = { [key: string]: Chart }; + export type Datasource = DatasourceMeta & { + uid: string; column_types: GenericDataType[]; table_name: string; };