PromucFlow_constructor/app/client/src/sagas/ActionExecution/ResetWidgetActionSaga.ts
Anand Srinivasan be6a96e760
chore: ee clean up (#19475)
Related to #15538

To enable adding a new action only for EE.

Refactored `ActionTriggerType` enum to a union type.
So we can extend this with a new action in EE repo.

Made sure type discrimination is handled in `ActionExecutionSaga`
properly as before.

---

- Introduced `ActionTriggerKeys` union type which can be used for type
checking the values.
- Refactored `ActionDescription` types to accommodate usage of the union
type instead of enum.
- exported required types for usage in EE repo.

---

strings and payload are type checked as follows.


![image](https://user-images.githubusercontent.com/66776129/210963548-46a9a368-653a-428d-bb08-94073d2c42dc.png)


![image](https://user-images.githubusercontent.com/66776129/210963576-6d87ccad-6b0f-443c-9d03-aa9ee9f5103a.png)

Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2023-01-06 17:32:08 +05:30

55 lines
1.8 KiB
TypeScript

import { put, select, take } from "redux-saga/effects";
import { getWidgetByName } from "sagas/selectors";
import {
resetChildrenMetaProperty,
resetWidgetMetaProperty,
} from "actions/metaActions";
import AppsmithConsole from "utils/AppsmithConsole";
import { ResetWidgetDescription } from "@appsmith/entities/DataTree/actionTriggers";
import {
ActionValidationError,
TriggerFailureError,
} from "sagas/ActionExecution/errorUtils";
import { getType, Types } from "utils/TypeHelpers";
import { FlattenedWidgetProps } from "widgets/constants";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import { getDataTree } from "selectors/dataTreeSelectors";
import { DataTree } from "entities/DataTree/dataTreeFactory";
import { isWidget } from "@appsmith/workers/Evaluation/evaluationUtils";
export default function* resetWidgetActionSaga(
payload: ResetWidgetDescription["payload"],
) {
const { widgetName } = payload;
if (getType(widgetName) !== Types.STRING) {
throw new ActionValidationError(
"RESET_WIDGET_META_RECURSIVE_BY_NAME",
"widgetName",
Types.STRING,
getType(widgetName),
);
}
const dataTree: DataTree = yield select(getDataTree);
const widget: FlattenedWidgetProps | undefined = yield select(
getWidgetByName,
widgetName,
);
if (!widget) {
throw new TriggerFailureError(`Widget ${payload.widgetName} not found`);
}
const evaluatedEntity = dataTree[widget.widgetName];
if (isWidget(evaluatedEntity)) {
yield put(resetWidgetMetaProperty(widget.widgetId, evaluatedEntity));
if (payload.resetChildren) {
yield put(resetChildrenMetaProperty(widget.widgetId));
}
}
yield take(ReduxActionTypes.RESET_WIDGET_META_EVALUATED);
AppsmithConsole.info({
text: `resetWidget('${payload.widgetName}', ${payload.resetChildren}) was triggered`,
});
}