diff --git a/app/client/src/api/PageApi.tsx b/app/client/src/api/PageApi.tsx index 9071a854e5..db81058b90 100644 --- a/app/client/src/api/PageApi.tsx +++ b/app/client/src/api/PageApi.tsx @@ -135,10 +135,12 @@ export interface ClonePageRequest { } export interface UpdateWidgetNameRequest { - pageId: string; + pageId?: string; layoutId: string; newName: string; oldName: string; + moduleId?: string; + contextType?: "MODULE" | "PAGE"; } export interface GenerateTemplatePageRequest { diff --git a/app/client/src/ce/sagas/PageSagas.tsx b/app/client/src/ce/sagas/PageSagas.tsx index e4e2dbbe4a..bbadd0ab49 100644 --- a/app/client/src/ce/sagas/PageSagas.tsx +++ b/app/client/src/ce/sagas/PageSagas.tsx @@ -153,6 +153,11 @@ import { } from "selectors/gitModSelectors"; import captureException from "instrumentation/sendFaroErrors"; +export interface HandleWidgetNameUpdatePayload { + newName: string; + widgetName: string; +} + export const checkIfMigrationIsNeeded = ( fetchPageResponse?: FetchPageResponse, ) => { @@ -954,6 +959,96 @@ export function* clonePageSaga( } } +export class WidgetNameUpdateExtension { + // Singleton instance + private static instance = new WidgetNameUpdateExtension(); + + // The extension function storage + private extensionFunction: + | ((params: HandleWidgetNameUpdatePayload) => Generator) + | null = null; + + // Private constructor + private constructor() {} + + // Get the instance + static getInstance() { + return this.instance; + } + + // Set the extension function + setExtension(fn: (params: HandleWidgetNameUpdatePayload) => Generator) { + this.extensionFunction = fn; + } + + // Get the extension function + getExtension() { + return this.extensionFunction; + } +} + +export function* updateWidgetNameAPISaga( + requestParams: UpdateWidgetNameRequest, +) { + const response: UpdateWidgetNameResponse = yield call( + PageApi.updateWidgetName, + requestParams, + ); + + const isValidResponse: boolean = yield validateResponse(response); + + return { response, isValidResponse }; +} + +export function* handleWidgetNameUpdateDefault( + params: HandleWidgetNameUpdatePayload, +) { + const { newName, widgetName } = params; + + const layoutId: string | undefined = yield select(getCurrentLayoutId); + const pageId: string | undefined = yield select(getCurrentPageId); + + const request: UpdateWidgetNameRequest = { + newName: newName, + oldName: widgetName, + pageId, + // @ts-expect-error: layoutId can be undefined + layoutId, + }; + const { isValidResponse, response } = yield call( + updateWidgetNameAPISaga, + request, + ); + + if (isValidResponse) { + // @ts-expect-error: pageId can be undefined + yield updateCanvasWithDSL(response.data, pageId, layoutId); + yield put(updateWidgetNameSuccess()); + // Add this to the page DSLs for entity explorer + yield put({ + type: ReduxActionTypes.FETCH_PAGE_DSL_SUCCESS, + payload: { + pageId: pageId, + dsl: response.data.dsl, + layoutId, + }, + }); + checkAndLogErrorsIfCyclicDependency( + (response.data as PageLayout).layoutOnLoadActionErrors, + ); + } +} + +export function* handleWidgetNameUpdate(params: HandleWidgetNameUpdatePayload) { + const extension = WidgetNameUpdateExtension.getInstance().getExtension(); + + if (extension) { + yield call(extension, params); + } else { + yield call(handleWidgetNameUpdateDefault, params); + } +} + /** * this saga do two things * @@ -967,8 +1062,6 @@ export function* updateWidgetNameSaga( ) { try { const { widgetName } = yield select(getWidgetName, action.payload.id); - const layoutId: string | undefined = yield select(getCurrentLayoutId); - const pageId: string | undefined = yield select(getCurrentPageId); const getUsedNames: Record = yield select( getUsedActionNames, "", @@ -1058,37 +1151,10 @@ export function* updateWidgetNameSaga( // check if name is not conflicting with any // existing entity/api/queries/reserved words if (isNameValid(action.payload.newName, getUsedNames)) { - const request: UpdateWidgetNameRequest = { + yield call(handleWidgetNameUpdate, { newName: action.payload.newName, - oldName: widgetName, - // @ts-expect-error: pageId can be undefined - pageId, - // @ts-expect-error: layoutId can be undefined - layoutId, - }; - const response: UpdateWidgetNameResponse = yield call( - PageApi.updateWidgetName, - request, - ); - const isValidResponse: boolean = yield validateResponse(response); - - if (isValidResponse) { - // @ts-expect-error: pageId can be undefined - yield updateCanvasWithDSL(response.data, pageId, layoutId); - yield put(updateWidgetNameSuccess()); - // Add this to the page DSLs for entity explorer - yield put({ - type: ReduxActionTypes.FETCH_PAGE_DSL_SUCCESS, - payload: { - pageId: pageId, - dsl: response.data.dsl, - layoutId, - }, - }); - checkAndLogErrorsIfCyclicDependency( - (response.data as PageLayout).layoutOnLoadActionErrors, - ); - } + widgetName, + }); } else { yield put({ type: ReduxActionErrorTypes.UPDATE_WIDGET_NAME_ERROR, diff --git a/app/client/src/store.ts b/app/client/src/store.ts index b8b11911f0..3e96c83cb8 100644 --- a/app/client/src/store.ts +++ b/app/client/src/store.ts @@ -8,6 +8,7 @@ import { composeWithDevTools } from "redux-devtools-extension/logOnlyInProductio import * as Sentry from "@sentry/react"; import { ReduxActionTypes } from "ee/constants/ReduxActionConstants"; import routeParamsMiddleware from "ee/middlewares/RouteParamsMiddleware"; +import packageMiddleware from "ee/middlewares/PackageMiddleware"; const sagaMiddleware = createSagaMiddleware(); const ignoredSentryActionTypes = [ @@ -30,7 +31,7 @@ export default createStore( appReducer, composeWithDevTools( reduxBatch, - applyMiddleware(sagaMiddleware, routeParamsMiddleware), + applyMiddleware(packageMiddleware, sagaMiddleware, routeParamsMiddleware), reduxBatch, sentryReduxEnhancer, ),