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

126 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-04-23 13:50:55 +00:00
import { createReducer } from "utils/AppsmithUtils";
import { Message, Severity } from "entities/AppsmithConsole";
import { ReduxAction, ReduxActionTypes } from "constants/ReduxActionConstants";
import { get, merge, isEmpty, omit, isUndefined } from "lodash";
import LOG_TYPE from "entities/AppsmithConsole/logtype";
const initialState: DebuggerReduxState = {
logs: [],
errorCount: 0,
isOpen: false,
errors: {},
expandId: "",
};
const debuggerReducer = createReducer(initialState, {
[ReduxActionTypes.DEBUGGER_LOG]: (
state: DebuggerReduxState,
action: ReduxAction<Message>,
) => {
const isError = action.payload.severity === Severity.ERROR;
return {
...state,
logs: [...state.logs, action.payload],
errorCount: isError ? state.errorCount + 1 : state.errorCount,
};
},
[ReduxActionTypes.CLEAR_DEBUGGER_LOGS]: (state: DebuggerReduxState) => {
return {
...state,
logs: [],
errorCount: 0,
};
},
[ReduxActionTypes.SHOW_DEBUGGER]: (
state: DebuggerReduxState,
action: ReduxAction<boolean | undefined>,
) => {
return {
...state,
isOpen: isUndefined(action.payload) ? !state.isOpen : action.payload,
};
},
[ReduxActionTypes.DEBUGGER_ERROR_LOG]: (
state: DebuggerReduxState,
action: ReduxAction<Message>,
) => {
if (!action.payload.source) return state;
const entityId = action.payload.source.id;
const id =
action.payload.logType === LOG_TYPE.WIDGET_PROPERTY_VALIDATION_ERROR ||
action.payload.logType === LOG_TYPE.EVAL_ERROR
2021-04-23 13:50:55 +00:00
? `${entityId}-${action.payload.source.propertyPath}`
: entityId;
const previousState = get(state.errors, id, {});
return {
...state,
errors: {
...state.errors,
[id]: {
...merge(previousState, action.payload),
},
},
expandId: id,
};
},
[ReduxActionTypes.DEBUGGER_UPDATE_ERROR_LOG]: (
state: DebuggerReduxState,
action: ReduxAction<Message>,
) => {
if (!action.payload.source) return state;
const entityId = action.payload.source.id;
const isWidgetErrorLog =
action.payload.logType === LOG_TYPE.WIDGET_PROPERTY_VALIDATION_ERROR ||
action.payload.logType === LOG_TYPE.EVAL_ERROR;
2021-04-23 13:50:55 +00:00
const id = isWidgetErrorLog
? `${entityId}-${action.payload.source.propertyPath}`
: entityId;
if (isEmpty(action.payload.state)) {
return {
...state,
errors: omit(state.errors, id),
};
}
return {
...state,
errors: {
...state.errors,
[id]: {
...action.payload,
},
},
expandId: id,
};
},
[ReduxActionTypes.DEBUGGER_UPDATE_ERROR_LOGS]: (
state: DebuggerReduxState,
action: ReduxAction<Message>,
) => {
return {
...state,
errors: { ...action.payload },
};
},
[ReduxActionTypes.INIT_CANVAS_LAYOUT]: () => {
return {
...initialState,
};
},
2021-04-23 13:50:55 +00:00
});
export interface DebuggerReduxState {
logs: Message[];
errorCount: number;
isOpen: boolean;
errors: Record<string, Message>;
expandId: string;
}
export default debuggerReducer;