Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sniping mode for module to bind to existing and new widgets #35072

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export const NoResponse = (props: NoResponseProps) => (
function ApiResponseView(props: Props) {
const {
actionResponse = EMPTY_RESPONSE,
apiName,
currentActionConfig,
disabled,
isRunning,
Expand Down Expand Up @@ -335,7 +336,7 @@ function ApiResponseView(props: Props) {
panelComponent: (
<ResponseTabWrapper>
<ApiResponseMeta
actionName={currentActionConfig?.name}
actionName={apiName || currentActionConfig?.name}
actionResponse={actionResponse}
/>
{Array.isArray(messages) && messages.length > 0 && (
Expand Down
5 changes: 4 additions & 1 deletion app/client/src/pages/Editor/QueryEditor/BindDataButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ function BindDataButton(props: BindDataButtonProps) {
pageId: string;
apiId?: string;
queryId?: string;
moduleInstanceId?: string;
}>();

const isFeatureEnabled = useFeatureFlag(FEATURE_FLAG.license_gac_enabled);
Expand Down Expand Up @@ -338,7 +339,9 @@ function BindDataButton(props: BindDataButtonProps) {
}
dispatch(
bindDataOnCanvas({
queryId: (params.apiId || params.queryId) as string,
queryId: (params.apiId ||
params.queryId ||
params.moduleInstanceId) as string,
applicationId: applicationId as string,
pageId: params.pageId,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export function EditorJSONtoForm(props: Props) {
title: createMessage(DEBUGGER_RESPONSE),
panelComponent: (
<QueryResponseTab
actionName={actionName}
actionSource={actionSource}
currentActionConfig={currentActionConfig}
isRunning={isRunning}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function QueryDebuggerTabs({
title: createMessage(DEBUGGER_RESPONSE),
panelComponent: (
<QueryResponseTab
actionName={actionName}
actionSource={actionSource}
currentActionConfig={currentActionConfig}
isRunning={isRunning}
Expand Down
4 changes: 3 additions & 1 deletion app/client/src/pages/Editor/QueryEditor/QueryResponseTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ interface Props {
onRunClick: () => void;
currentActionConfig: Action;
runErrorMessage?: string;
actionName: string;
}

const QueryResponseTab = (props: Props) => {
const {
actionName,
actionSource,
currentActionConfig,
isRunning,
Expand Down Expand Up @@ -270,7 +272,7 @@ const QueryResponseTab = (props: Props) => {
value={selectedControl}
/>
<BindDataButton
actionName={currentActionConfig.name}
actionName={actionName || currentActionConfig.name}
hasResponse={!!actionResponse}
suggestedWidgets={actionResponse?.suggestedWidgets}
/>
Expand Down
104 changes: 104 additions & 0 deletions app/client/src/sagas/SnipingModeSaga.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import type { ModuleInstance } from "@appsmith/constants/ModuleInstanceConstants";
import { keyBy } from "lodash";
import { testStore } from "store";
import { PostgresFactory } from "test/factories/Actions/Postgres";
import type { Saga } from "redux-saga";
import { runSaga } from "redux-saga";
import { bindDataToWidgetSaga } from "./SnipingModeSagas";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import { getModuleInstanceById } from "@appsmith/selectors/moduleInstanceSelectors";
import WidgetFactory from "WidgetProvider/factory";
import TableWidget from "widgets/TableWidget/widget";
import { InputFactory } from "test/factories/Widgets/InputFactory";

jest.mock("@appsmith/selectors/moduleInstanceSelectors", () => ({
...jest.requireActual("@appsmith/selectors/moduleInstanceSelectors"),
getModuleInstanceById: jest.fn(),
}));

describe("SnipingModeSaga", () => {
beforeEach(() => {
jest.restoreAllMocks();
});

it("should check for moduleInstance and use when action is missing", async () => {
const widget = InputFactory.build();
const action = PostgresFactory.build();
const moduleInstance = {
id: "module-instance-id",
name: "ModuleInstance1",
} as ModuleInstance;

(getModuleInstanceById as jest.Mock).mockReturnValue(moduleInstance);
const spy = jest
.spyOn(WidgetFactory, "getWidgetMethods")
.mockImplementation(TableWidget.getMethods);

const store = testStore({
entities: {
...({} as any),
actions: [
{
config: action,
},
],
canvasWidgets: keyBy([widget], "widgetId"),
},
ui: {
...({} as any),
editor: {
snipModeBindTo: "module-instance-id",
},
},
});
const dispatched: any[] = [];

await runSaga(
{
dispatch: (action) => dispatched.push(action),
getState: () => store.getState(),
},
bindDataToWidgetSaga as Saga,
{ payload: { widgetId: widget.widgetId, bindingQuery: "data" } },
).toPromise();

expect(dispatched).toEqual([
{
payload: {
updates: [
{
isDynamic: true,
propertyPath: "tableData",
skipValidation: true,
},
],
widgetId: widget.widgetId,
},
type: ReduxActionTypes.BATCH_SET_WIDGET_DYNAMIC_PROPERTY,
},
{
payload: {
shouldReplay: true,
updates: { modify: { tableData: `{{${moduleInstance.name}.data}}` } },
widgetId: widget.widgetId,
},
type: ReduxActionTypes.BATCH_UPDATE_WIDGET_PROPERTY,
},
{
payload: { bindTo: undefined, isActive: false },
type: ReduxActionTypes.SET_SNIPING_MODE,
},
{
payload: {
invokedBy: undefined,
pageId: undefined,
payload: [widget.widgetId],
selectionRequestType: "One",
},
type: ReduxActionTypes.SELECT_WIDGET_INIT,
},
]);

spy.mockRestore();
});
});
23 changes: 15 additions & 8 deletions app/client/src/sagas/SnipingModeSagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { selectWidgetInitAction } from "actions/widgetSelectionActions";
import { SelectionRequestType } from "sagas/WidgetSelectUtils";
import { toast } from "design-system";
import type { PropertyUpdates } from "WidgetProvider/constants";
import type { ModuleInstance } from "@appsmith/constants/ModuleInstanceConstants";
import { getModuleInstanceById } from "@appsmith/selectors/moduleInstanceSelectors";

export function* bindDataToWidgetSaga(
action: ReduxAction<{
Expand All @@ -35,6 +37,14 @@ export function* bindDataToWidgetSaga(
(action: ActionData) => action.config.id === queryId,
),
);
const currentModuleInstance: ModuleInstance | undefined = yield select(
getModuleInstanceById,
queryId,
);

const actionName =
currentAction?.config.name || currentModuleInstance?.name || "";

const widgetState: CanvasWidgetsReduxState = yield select(getCanvasWidgets);
const selectedWidget = widgetState[action.payload.widgetId];

Expand All @@ -47,22 +57,19 @@ export function* bindDataToWidgetSaga(
const { widgetId } = action.payload;

let isValidProperty = true;

// Pranav has an Open PR for this file so just returning for now
if (!currentAction) return;

if (!actionName) return;
const { getSnipingModeUpdates } = WidgetFactory.getWidgetMethods(
selectedWidget.type,
);

let updates: Array<PropertyUpdates> = [];

const oneClickBindingQuery = `{{${currentAction.config.name}.data}}`;
const oneClickBindingQuery = `{{${actionName}.data}}`;

const bindingQuery = action.payload.bindingQuery
? `{{${currentAction.config.name}.${action.payload.bindingQuery}}}`
? `{{${actionName}.${action.payload.bindingQuery}}}`
: oneClickBindingQuery;

let isDynamicPropertyPath = true;

if (bindingQuery === oneClickBindingQuery) {
Expand All @@ -72,13 +79,13 @@ export function* bindDataToWidgetSaga(
if (getSnipingModeUpdates) {
updates = getSnipingModeUpdates?.({
data: bindingQuery,
run: `{{${currentAction.config.name}.run()}}`,
run: `{{${actionName}.run()}}`,
isDynamicPropertyPath,
});

AnalyticsUtil.logEvent("WIDGET_SELECTED_VIA_SNIPING_MODE", {
widgetType: selectedWidget.type,
actionName: currentAction.config.name,
actionName: actionName,
apiId: queryId,
propertyPath: updates?.map((update) => update.propertyPath).toString(),
propertyValue: updates?.map((update) => update.propertyPath).toString(),
Expand Down
Loading