From bb39e4ff53e4671847116af9434bb381546a5838 Mon Sep 17 00:00:00 2001 From: Rahul Barwal Date: Wed, 29 May 2024 09:39:39 +0530 Subject: [PATCH] fix: Handles button binding during buildingblock pasting (#33674) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description This pull request adds the handleButtonDynamicTriggerPathList function to the BuildingBlockAdditionSagas.ts file. This function is responsible for handling the dynamic trigger path list for button widgets. It iterates over the widgetNameMap and updates the dynamicTriggerPathList of each button widget with the newWidgetName. Additionally, unit tests have been added to ensure the correct functionality of the handleButtonDynamicTriggerPathList function. This change improves the functionality of button widgets when pasting building block widgets. Fixes #33658 _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Widget" ### :mag: Cypress test results > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: > Commit: a675a5c8a752fea76c130e8bdc1b76e44453ee98 > Cypress dashboard url: Click here! ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No --------- Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> --- .../BuildingBlockAdditionSagas.ts | 4 +++ .../PasteWidgetUtils/PasteWidgetUtils.test.ts | 26 +++++++++++++++++++ .../src/sagas/PasteWidgetUtils/index.ts | 11 ++++++++ 3 files changed, 41 insertions(+) diff --git a/app/client/src/sagas/BuildingBlockSagas/BuildingBlockAdditionSagas.ts b/app/client/src/sagas/BuildingBlockSagas/BuildingBlockAdditionSagas.ts index 4aec06f0539d..99a9dc2c6f8b 100644 --- a/app/client/src/sagas/BuildingBlockSagas/BuildingBlockAdditionSagas.ts +++ b/app/client/src/sagas/BuildingBlockSagas/BuildingBlockAdditionSagas.ts @@ -55,6 +55,7 @@ import { updateWidgetPositions } from "layoutSystems/autolayout/utils/positionUt import type { FlexLayer } from "layoutSystems/autolayout/utils/types"; import { getNewPositions, + handleButtonDynamicTriggerPathList, handleImageWidgetWhenPasting, handleJSONFormPropertiesListedInDynamicBindingPath, handleJSONFormWidgetWhenPasting, @@ -835,6 +836,9 @@ function handleOtherWidgetReferencesWhilePastingBuildingBlockWidget( case "IMAGE_WIDGET": handleImageWidgetWhenPasting(widgetNameMap, widget); break; + case "BUTTON_WIDGET": + handleButtonDynamicTriggerPathList(widgetNameMap, widget); + break; default: widgets = handleSpecificCasesWhilePasting( widget, diff --git a/app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts b/app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts index 72ba433251bf..7b87b09647b9 100644 --- a/app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts +++ b/app/client/src/sagas/PasteWidgetUtils/PasteWidgetUtils.test.ts @@ -5,6 +5,7 @@ import { handleJSONFormWidgetWhenPasting, handleTextWidgetWhenPasting, handleJSONFormPropertiesListedInDynamicBindingPath, + handleButtonDynamicTriggerPathList, } from "."; import { widget, @@ -17,6 +18,7 @@ import { const widgetNameMap = { table1: "table1Copy", }; +import { klona } from "klona"; function testIndividualWidgetPasting( widgetNameMap: Record, @@ -229,3 +231,27 @@ describe("handleJSONFormPropertiesListedInDynamicBindingPath", () => { ]); }); }); + +describe("handleButtonDynamicTriggerPathList", () => { + const widget = { + dynamicTriggerPathList: [{ key: "onClick" }], + onClick: "{{oldName.val}}", + } as any as FlattenedWidgetProps; + it("1. should replace old widget names with new widget names in dynamic trigger paths", () => { + const widgetNameMap = { + oldName: "newName", + }; + const button = klona(widget); + handleButtonDynamicTriggerPathList(widgetNameMap, button); + expect(button.onClick).toEqual("{{newName.val}}"); + }); + + it("2. should do nothing if the widgetNameMap does not contain names in dynamic trigger paths", () => { + const widgetNameMap = { + oldWidget1: "newWidget1", + }; + const button = klona(widget); + handleButtonDynamicTriggerPathList(widgetNameMap, button); + expect(button.onClick).toEqual("{{oldName.val}}"); + }); +}); diff --git a/app/client/src/sagas/PasteWidgetUtils/index.ts b/app/client/src/sagas/PasteWidgetUtils/index.ts index afe709e7acd3..c8551e329f94 100644 --- a/app/client/src/sagas/PasteWidgetUtils/index.ts +++ b/app/client/src/sagas/PasteWidgetUtils/index.ts @@ -443,3 +443,14 @@ export function accessNestedObjectValue( ), ); } + +export function handleButtonDynamicTriggerPathList( + widgetNameMap: Record, + widget: FlattenedWidgetProps, +) { + Object.entries(widgetNameMap).forEach(([oldWidgetName, newWidgetName]) => { + widget.dynamicTriggerPathList?.forEach((path: { key: string }) => { + accessNestedObjectValue(widget, path.key, oldWidgetName, newWidgetName); + }); + }); +}