PromucFlow_constructor/app/client/src/reducers/uiReducers/apiPaneReducer.ts

161 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-11-25 05:07:27 +00:00
import { createReducer } from "utils/AppsmithUtils";
2019-11-13 07:34:59 +00:00
import {
ReduxActionTypes,
ReduxActionErrorTypes,
2019-11-25 09:15:11 +00:00
ReduxAction,
2019-11-25 05:07:27 +00:00
} from "constants/ReduxActionConstants";
2019-11-25 09:15:11 +00:00
import { RestAction } from "api/ActionAPI";
import _ from "lodash";
2019-11-13 07:34:59 +00:00
const initialState: ApiPaneReduxState = {
2019-11-25 09:15:11 +00:00
lastUsed: "",
2019-11-13 07:34:59 +00:00
isFetching: false,
2019-12-11 15:14:38 +00:00
drafts: {},
isRunning: {},
isSaving: {},
isDeleting: {},
2019-11-13 07:34:59 +00:00
};
export interface ApiPaneReduxState {
2019-11-25 09:15:11 +00:00
lastUsed: string;
2019-11-13 07:34:59 +00:00
isFetching: boolean;
2019-12-11 15:14:38 +00:00
drafts: Record<string, RestAction>;
isRunning: Record<string, boolean>;
isSaving: Record<string, boolean>;
isDeleting: Record<string, boolean>;
2019-11-13 07:34:59 +00:00
}
const apiPaneReducer = createReducer(initialState, {
[ReduxActionTypes.FETCH_ACTIONS_INIT]: (state: ApiPaneReduxState) => ({
...state,
isFetching: true,
}),
[ReduxActionTypes.FETCH_ACTIONS_SUCCESS]: (state: ApiPaneReduxState) => ({
...state,
isFetching: false,
}),
[ReduxActionErrorTypes.FETCH_ACTIONS_ERROR]: (state: ApiPaneReduxState) => ({
...state,
isFetching: false,
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.RUN_API_REQUEST]: (
state: ApiPaneReduxState,
action: ReduxAction<string>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isRunning: {
...state.isRunning,
[action.payload]: true,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.RUN_API_SUCCESS]: (
state: ApiPaneReduxState,
action: ReduxAction<{ [id: string]: any }>,
) => {
const actionId = Object.keys(action.payload)[0];
return {
...state,
isRunning: {
...state.isRunning,
[actionId]: false,
},
};
},
[ReduxActionErrorTypes.RUN_API_ERROR]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isRunning: {
...state.isRunning,
[action.payload.id]: false,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.UPDATE_ACTION_INIT]: (
state: ApiPaneReduxState,
action: ReduxAction<{ data: RestAction }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isSaving: {
...state.isSaving,
[action.payload.data.id]: true,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.UPDATE_ACTION_SUCCESS]: (
state: ApiPaneReduxState,
action: ReduxAction<{ data: RestAction }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isSaving: {
...state.isSaving,
[action.payload.data.id]: false,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionErrorTypes.UPDATE_ACTION_ERROR]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isSaving: {
...state.isSaving,
[action.payload.id]: false,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.DELETE_ACTION_INIT]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isDeleting: {
...state.isDeleting,
[action.payload.id]: true,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionTypes.DELETE_ACTION_SUCCESS]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isDeleting: {
...state.isDeleting,
[action.payload.id]: false,
},
2019-11-13 07:34:59 +00:00
}),
2019-12-11 15:14:38 +00:00
[ReduxActionErrorTypes.DELETE_ACTION_ERROR]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
2019-11-13 07:34:59 +00:00
...state,
2019-12-11 15:14:38 +00:00
isDeleting: {
...state.isDeleting,
[action.payload.id]: false,
},
2019-11-13 07:34:59 +00:00
}),
2019-11-25 09:15:11 +00:00
[ReduxActionTypes.UPDATE_API_DRAFT]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string; draft: Partial<RestAction> }>,
) => ({
...state,
drafts: {
...state.drafts,
[action.payload.id]: action.payload.draft,
},
}),
[ReduxActionTypes.DELETE_API_DRAFT]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
...state,
drafts: _.omit(state.drafts, action.payload.id),
}),
[ReduxActionTypes.API_PANE_CHANGE_API]: (
state: ApiPaneReduxState,
action: ReduxAction<{ id: string }>,
) => ({
...state,
lastUsed: action.payload.id,
}),
2019-11-13 07:34:59 +00:00
});
export default apiPaneReducer;