2019-10-22 14:59:58 +00:00
|
|
|
import { createReducer } from "../../utils/AppsmithUtils";
|
|
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxAction,
|
|
|
|
|
} from "../../constants/ReduxActionConstants";
|
2019-11-07 09:32:38 +00:00
|
|
|
import { Datasource } from "../../api/DatasourcesApi";
|
2019-10-22 14:59:58 +00:00
|
|
|
import { REST_PLUGIN_ID } from "../../constants/ApiEditorConstants";
|
|
|
|
|
|
2019-11-07 09:32:38 +00:00
|
|
|
export interface DatasourceDataState {
|
|
|
|
|
list: Datasource[];
|
2019-10-22 14:59:58 +00:00
|
|
|
loading: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 09:32:38 +00:00
|
|
|
const initialState: DatasourceDataState = {
|
2019-10-22 14:59:58 +00:00
|
|
|
list: [],
|
|
|
|
|
loading: false,
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-07 09:32:38 +00:00
|
|
|
const datasourceReducer = createReducer(initialState, {
|
|
|
|
|
[ReduxActionTypes.FETCH_DATASOURCES_INIT]: (state: DatasourceDataState) => {
|
2019-10-22 14:59:58 +00:00
|
|
|
return { ...state, loading: true };
|
|
|
|
|
},
|
2019-11-07 09:32:38 +00:00
|
|
|
[ReduxActionTypes.CREATE_DATASOURCE_INIT]: (state: DatasourceDataState) => {
|
2019-10-22 14:59:58 +00:00
|
|
|
return { ...state, loading: true };
|
|
|
|
|
},
|
2019-11-07 09:32:38 +00:00
|
|
|
[ReduxActionTypes.FETCH_DATASOURCES_SUCCESS]: (
|
|
|
|
|
state: DatasourceDataState,
|
|
|
|
|
action: ReduxAction<Datasource[]>,
|
2019-10-22 14:59:58 +00:00
|
|
|
) => {
|
|
|
|
|
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>,
|
2019-10-22 14:59:58 +00:00
|
|
|
) => {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
list: state.list.concat(action.payload),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2019-11-07 09:32:38 +00:00
|
|
|
export default datasourceReducer;
|