PromucFlow_constructor/app/client/src/sagas/BatchSagas.tsx
Favour Ohanekwu 95fa2328a8
fix: reset widget to default value after setter method (#29151)
## Description
This PR ensures that widgets are reset to their default value after the
`setValue` setter method is used to set its value

#### PR fixes following issue(s)
Fixes #27119 

#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Implemented a new reset functionality for widgets, allowing users to
revert to default values after changes.
  - Enhanced widget meta updates with a new reset action.

- **Bug Fixes**
- Added test cases to ensure widget reset functionality works as
expected, even after asynchronous operations.

- **Refactor**
- Refactored evaluation logic to improve handling of widget meta updates
and resets.
  - Improved action execution logic for resetting widget properties.

- **Tests**
- Expanded end-to-end regression tests to cover new reset widget
functionality.

- **Documentation**
- Updated internal documentation to reflect new action types and
evaluation processes related to widget meta updates.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Druthi Polisetty <druthi@appsmith.com>
2023-12-08 11:16:31 +01:00

87 lines
2.5 KiB
TypeScript

/* eslint-disable @typescript-eslint/ban-ts-comment */
import _ from "lodash";
import { put, debounce, takeEvery, all } from "redux-saga/effects";
import type { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import { batchActionSuccess } from "actions/batchActions";
import * as log from "loglevel";
const BATCH_PRIORITY = {
[ReduxActionTypes.META_UPDATE_DEBOUNCED_EVAL]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.SET_META_PROP_AND_EVAL]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.RESET_WIDGET_META]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.RESET_WIDGET_META_UPDATES]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.UPDATE_WIDGET_PROPERTY]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.EXECUTE_TRIGGER_REQUEST]: {
priority: 1,
needsSaga: true,
},
[ReduxActionTypes.EXECUTE_PAGE_LOAD_ACTIONS]: {
priority: 1,
needsSaga: true,
},
[ReduxActionTypes.UPDATE_ACTION_PROPERTY]: {
priority: 0,
needsSaga: false,
},
[ReduxActionTypes.UPDATE_ACTION_INIT]: {
priority: 1,
needsSaga: true,
},
};
const batches: ReduxAction<any>[][] = [];
function* storeUpdatesSaga(action: ReduxAction<ReduxAction<any>>) {
try {
const priority = BATCH_PRIORITY[action.payload.type].priority;
const currentPriorityBatch = batches[priority] || [];
currentPriorityBatch.push(action.payload);
_.set(batches, `[${priority}]`, currentPriorityBatch);
yield put({ type: ReduxActionTypes.EXECUTE_BATCH });
} catch (e) {
log.error(`${action.payload.type} action priority not set`);
}
}
function* executeBatchSaga() {
for (let priority = 0; priority < batches.length; priority++) {
const batch = batches[priority];
if (Array.isArray(batch) && batch.length) {
const needsSaga = batch.filter((b) => BATCH_PRIORITY[b.type].needsSaga);
const canBatch = batch.filter((b) => !BATCH_PRIORITY[b.type].needsSaga);
batches[priority] = [];
// @ts-expect-error: Types are not available
yield put(canBatch);
if (needsSaga.length) {
for (const sagaAction of needsSaga) {
yield put(sagaAction);
}
}
yield put(batchActionSuccess(batch));
}
}
}
export default function* root() {
yield all([
debounce(20, ReduxActionTypes.EXECUTE_BATCH, executeBatchSaga),
takeEvery(ReduxActionTypes.BATCHED_UPDATE, storeUpdatesSaga),
]);
}