PromucFlow_constructor/app/client/src/reducers/entityReducers/datasourceReducer.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import { createReducer } from "../../utils/AppsmithUtils";
import {
ReduxActionTypes,
ReduxAction,
} from "../../constants/ReduxActionConstants";
2019-11-07 09:32:38 +00:00
import { Datasource } from "../../api/DatasourcesApi";
import { REST_PLUGIN_ID } from "../../constants/ApiEditorConstants";
2019-11-07 09:32:38 +00:00
export interface DatasourceDataState {
list: Datasource[];
loading: boolean;
}
2019-11-07 09:32:38 +00:00
const initialState: DatasourceDataState = {
list: [],
loading: false,
};
2019-11-07 09:32:38 +00:00
const datasourceReducer = createReducer(initialState, {
[ReduxActionTypes.FETCH_DATASOURCES_INIT]: (state: DatasourceDataState) => {
return { ...state, loading: true };
},
2019-11-07 09:32:38 +00:00
[ReduxActionTypes.CREATE_DATASOURCE_INIT]: (state: DatasourceDataState) => {
return { ...state, loading: true };
},
2019-11-07 09:32:38 +00:00
[ReduxActionTypes.FETCH_DATASOURCES_SUCCESS]: (
state: DatasourceDataState,
action: ReduxAction<Datasource[]>,
) => {
return {
...state,
loading: false,
// TODO(hetu) Once plugins are being pulled get Ids from there
list: action.payload.filter(r => r.pluginId === REST_PLUGIN_ID),
};
},
2019-11-07 09:32:38 +00:00
[ReduxActionTypes.CREATE_DATASOURCE_SUCCESS]: (
state: DatasourceDataState,
action: ReduxAction<Datasource>,
) => {
return {
...state,
loading: false,
list: state.list.concat(action.payload),
};
},
});
2019-11-07 09:32:38 +00:00
export default datasourceReducer;