diff --git a/app/client/src/actions/apiPaneActions.ts b/app/client/src/actions/apiPaneActions.ts index f1b870d7ab..6fbcf0b41c 100644 --- a/app/client/src/actions/apiPaneActions.ts +++ b/app/client/src/actions/apiPaneActions.ts @@ -48,6 +48,11 @@ export const createNewApiAction = ( payload: { pageId }, }); +export const createNewQueryAction = (pageId: string): ReduxAction<{}> => ({ + type: ReduxActionTypes.CREATE_NEW_QUERY_ACTION, + payload: { pageId }, +}); + export const setExtraFormData = ( apiId: string, extraformData: {}, diff --git a/app/client/src/components/editorComponents/DynamicAutocompleteInput.tsx b/app/client/src/components/editorComponents/DynamicAutocompleteInput.tsx index b2a4e2aa7a..1fb03fbe0f 100644 --- a/app/client/src/components/editorComponents/DynamicAutocompleteInput.tsx +++ b/app/client/src/components/editorComponents/DynamicAutocompleteInput.tsx @@ -23,9 +23,6 @@ import { Theme } from "constants/DefaultTheme"; import AnalyticsUtil from "utils/AnalyticsUtil"; import TernServer from "utils/autocomplete/TernServer"; import KeyboardShortcuts from "constants/KeyboardShortcuts"; -import { createNewApiAction } from "actions/apiPaneActions"; -import { RestAction } from "api/ActionAPI"; -import { createActionRequest } from "actions/actionActions"; const LightningMenu = lazy(() => import("components/editorComponents/LightningMenu"), ); @@ -288,10 +285,6 @@ const AUTOCOMPLETE_CLOSE_KEY_CODES = ["Enter", "Tab", "Escape"]; interface ReduxStateProps { dynamicData: DataTree; } -interface ReduxDispatchProps { - createNewApiAction: (pageId: string) => void; - createAction: (data: Partial) => void; -} export type DynamicAutocompleteInputProps = { placeholder?: string; @@ -315,7 +308,6 @@ export type DynamicAutocompleteInputProps = { }; type Props = ReduxStateProps & - ReduxDispatchProps & DynamicAutocompleteInputProps & { input: Partial; }; @@ -592,8 +584,6 @@ class DynamicAutocompleteInput extends Component { )} @@ -655,14 +645,4 @@ const mapStateToProps = (state: AppState): ReduxStateProps => ({ dynamicData: getDataTreeForAutocomplete(state), }); -const mapDispatchToProps = (dispatch: Function): ReduxDispatchProps => ({ - createNewApiAction: (pageId: string) => dispatch(createNewApiAction(pageId)), - createAction: (data: Partial) => { - dispatch(createActionRequest(data)); - }, -}); - -export default connect( - mapStateToProps, - mapDispatchToProps, -)(DynamicAutocompleteInput); +export default connect(mapStateToProps)(DynamicAutocompleteInput); diff --git a/app/client/src/components/editorComponents/LightningMenu/helpers.tsx b/app/client/src/components/editorComponents/LightningMenu/helpers.tsx index d413f6fcf9..a450a4bddf 100644 --- a/app/client/src/components/editorComponents/LightningMenu/helpers.tsx +++ b/app/client/src/components/editorComponents/LightningMenu/helpers.tsx @@ -6,15 +6,10 @@ import CustomizedDropdown, { CustomizedDropdownOption, } from "pages/common/CustomizedDropdown"; import Button from "components/editorComponents/Button"; -import { getNextEntityName } from "utils/AppsmithUtils"; -import { ActionData } from "reducers/entityReducers/actionsReducer"; -import { Datasource } from "api/DatasourcesApi"; -import history from "utils/history"; import { - QUERY_EDITOR_URL_WITH_SELECTED_PAGE_ID, - DATA_SOURCES_EDITOR_URL, -} from "constants/routes"; - + createNewApiAction, + createNewQueryAction, +} from "actions/apiPaneActions"; import { LIGHTNING_MENU_DATA_API, LIGHTNING_MENU_DATA_QUERY, @@ -30,7 +25,7 @@ export const getApiOptions = ( skin: string, apis: RestAction[], pageId: string, - createNewApiAction: (pageId: string) => void, + dispatch: Function, updatePropertyValue: (value: string, cursor?: number) => void, ) => ({ sections: [ @@ -48,7 +43,7 @@ export const getApiOptions = ( /> ), onSelect: () => { - createNewApiAction(pageId); + dispatch(createNewApiAction(pageId)); }, }, ], @@ -74,11 +69,7 @@ export const getQueryOptions = ( skin: string, queries: RestAction[], pageId: string, - applicationId: string, - actions: ActionData[], - pluginIds: string[], - dataSources: Datasource[], - createAction: (data: Partial) => void, + dispatch: Function, updatePropertyValue: (value: string, cursor?: number) => void, ) => ({ sections: [ @@ -96,36 +87,7 @@ export const getQueryOptions = ( /> ), onSelect: () => { - const pageApiNames = actions - .filter(a => a.config.pageId === pageId) - .map(a => a.config.name); - const validDataSources: Array = []; - dataSources.forEach(dataSource => { - if (pluginIds?.includes(dataSource.pluginId)) { - validDataSources.push(dataSource); - } - }); - if (validDataSources.length) { - const newQueryName = getNextEntityName("Query", pageApiNames); - const dataSourceId = validDataSources[0].id; - createAction({ - name: newQueryName, - pageId, - datasource: { - id: dataSourceId, - }, - actionConfiguration: {}, - }); - history.push( - QUERY_EDITOR_URL_WITH_SELECTED_PAGE_ID( - applicationId, - pageId, - pageId, - ), - ); - } else { - history.push(DATA_SOURCES_EDITOR_URL(applicationId, pageId)); - } + dispatch(createNewQueryAction(pageId)); }, }, ], @@ -177,12 +139,7 @@ export const getLightningMenuOptions = ( queries: RestAction[], widgets: WidgetProps[], pageId: string, - applicationId: string, - actions: ActionData[], - pluginIds: string[], - dataSources: Datasource[], - createNewApiAction: (pageId: string) => void, - createAction: (data: Partial) => void, + dispatch: Function, skin: string, updatePropertyValue: (value: string, cursor?: number) => void, ) => { @@ -198,13 +155,7 @@ export const getLightningMenuOptions = ( { content: ( ), disabled: false, @@ -217,11 +168,7 @@ export const getLightningMenuOptions = ( skin, queries, pageId, - applicationId, - actions, - pluginIds, - dataSources, - createAction, + dispatch, updatePropertyValue, )} /> diff --git a/app/client/src/components/editorComponents/LightningMenu/hooks.ts b/app/client/src/components/editorComponents/LightningMenu/hooks.ts index cecdd8072e..8eb4b0f4ed 100644 --- a/app/client/src/components/editorComponents/LightningMenu/hooks.ts +++ b/app/client/src/components/editorComponents/LightningMenu/hooks.ts @@ -1,8 +1,7 @@ -import { useSelector, useDispatch } from "react-redux"; +import { useSelector } from "react-redux"; import { AppState } from "reducers"; import { WidgetProps } from "widgets/BaseWidget"; import { RestAction } from "api/ActionAPI"; -import { PLUGIN_PACKAGE_DBS } from "constants/QueryEditorConstants"; export const useWidgets = () => { return useSelector((state: AppState) => { @@ -39,37 +38,3 @@ export const usePageId = () => { }); return pageId || ""; }; - -export const useApplicationId = () => { - const applicationId = useSelector((state: AppState) => { - return state.entities.pageList.applicationId; - }); - return applicationId || ""; -}; - -export const useAllActions = () => { - const actions = useSelector((state: AppState) => { - const currentPageId = state.entities.pageList.currentPageId; - return state.entities.actions.filter( - action => action.config.pageId === currentPageId, - ); - }); - return actions; -}; - -export const useDataSources = () => { - const datasources = useSelector((state: AppState) => { - return state.entities.datasources.list; - }); - return datasources; -}; - -export const usePluginIdsOfPackageNames = () => { - const plugins = useSelector((state: AppState) => { - return state.entities.plugins.list.filter(plugin => - PLUGIN_PACKAGE_DBS.includes(plugin.packageName), - ); - }); - const pluginIds = plugins.map(plugin => plugin.id); - return pluginIds; -}; diff --git a/app/client/src/components/editorComponents/LightningMenu/index.tsx b/app/client/src/components/editorComponents/LightningMenu/index.tsx index 170c8af058..b23e3a828f 100644 --- a/app/client/src/components/editorComponents/LightningMenu/index.tsx +++ b/app/client/src/components/editorComponents/LightningMenu/index.tsx @@ -1,6 +1,5 @@ import React from "react"; import { Tooltip } from "@blueprintjs/core"; - import CustomizedDropdown, { CustomizedDropdownProps, } from "pages/common/CustomizedDropdown"; @@ -12,19 +11,10 @@ import { ControlIcons } from "icons/ControlIcons"; import { LIGHTNING_MENU_DATA_TOOLTIP } from "constants/messages"; import { getLightningMenuOptions } from "./helpers"; -import { - useActions, - useWidgets, - usePageId, - useAllActions, - useDataSources, - useApplicationId, - usePluginIdsOfPackageNames, -} from "./hooks"; -import { ActionData } from "reducers/entityReducers/actionsReducer"; -import { Datasource } from "api/DatasourcesApi"; +import { useActions, useWidgets, usePageId } from "./hooks"; import { Theme } from "constants/DefaultTheme"; import { withTheme } from "styled-components"; +import { useDispatch } from "react-redux"; const LightningIcon = ControlIcons.LIGHTNING_CONTROL; const lightningMenuOptions = ( @@ -33,12 +23,7 @@ const lightningMenuOptions = ( queries: RestAction[], widgets: WidgetProps[], pageId: string, - applicationId: string, - actions: ActionData[], - pluginIds: string[], - dataSources: Datasource[], - createNewApiAction: (pageId: string) => void, - createAction: (data: Partial) => void, + dispatch: Function, updatePropertyValue: (value: string, cursor?: number) => void, trigger: React.ReactNode, ): CustomizedDropdownProps => { @@ -47,12 +32,7 @@ const lightningMenuOptions = ( queries, widgets, pageId, - applicationId, - actions, - pluginIds, - dataSources, - createNewApiAction, - createAction, + dispatch, skin, updatePropertyValue, ); @@ -78,8 +58,6 @@ const lightningMenuOptions = ( type LightningMenuProps = { onSelect?: (value: string) => void; updatePropertyValue: (value: string, cursor?: number) => void; - createNewApiAction: (pageId: string) => void; - createAction: (data: Partial) => void; skin: string; theme: Theme; }; @@ -88,10 +66,7 @@ export const LightningMenu = (props: LightningMenuProps) => { const widgets = useWidgets(); const { apis, queries } = useActions(); const pageId = usePageId(); - const actions = useAllActions(); - const dataSources = useDataSources(); - const applicationId = useApplicationId(); - const pluginIds = usePluginIdsOfPackageNames(); + const dispatch = useDispatch(); const lightningMenuTrigger = ( { queries, widgets, pageId, - applicationId, - actions, - pluginIds, - dataSources, - props.createNewApiAction, - props.createAction, + dispatch, props.updatePropertyValue, lightningMenuTrigger, )} diff --git a/app/client/src/constants/ReduxActionConstants.tsx b/app/client/src/constants/ReduxActionConstants.tsx index 099d26176b..511a937532 100644 --- a/app/client/src/constants/ReduxActionConstants.tsx +++ b/app/client/src/constants/ReduxActionConstants.tsx @@ -205,6 +205,7 @@ export const ReduxActionTypes: { [key: string]: string } = { EXECUTE_BATCH: "EXECUTE_BATCH", FETCH_ALL_PUBLISHED_PAGES: "FETCH_ALL_PUBLISHED_PAGES", CREATE_NEW_API_ACTION: "CREATE_NEW_API_ACTION", + CREATE_NEW_QUERY_ACTION: "CREATE_NEW_QUERY_ACTION", SET_CURRENT_CATEGORY: "SET_CURRENT_CATEGORY", SET_LAST_USED_EDITOR_PAGE: "SET_LAST_USED_EDITOR_PAGE", SET_LAST_SELECTED_PAGE_PAGE: "SET_LAST_SELECTED_PAGE_PAGE", diff --git a/app/client/src/sagas/ApiPaneSagas.ts b/app/client/src/sagas/ApiPaneSagas.ts index f5603f5a0b..d7017f8067 100644 --- a/app/client/src/sagas/ApiPaneSagas.ts +++ b/app/client/src/sagas/ApiPaneSagas.ts @@ -23,12 +23,15 @@ import { API_EDITOR_ID_URL, API_EDITOR_URL, getProviderTemplatesURL, + QUERY_EDITOR_URL_WITH_SELECTED_PAGE_ID, + DATA_SOURCES_EDITOR_URL, } from "constants/routes"; import { getCurrentApplicationId, getCurrentPageId, getIsEditorInitialized, getLastSelectedPage, + getDataSources, } from "selectors/editorSelectors"; import { initialize, autofill, change } from "redux-form"; import { getAction } from "./ActionSagas"; @@ -41,11 +44,14 @@ import { UNIQUE_NAME_ERROR, VALID_FUNCTION_NAME_ERROR, } from "constants/messages"; -import { createNewApiName } from "utils/AppsmithUtils"; +import { createNewApiName, getNextEntityName } from "utils/AppsmithUtils"; import { getPluginIdOfPackageName } from "sagas/selectors"; -import { getActions } from "selectors/entitiesSelector"; +import { getActions, getPlugins } from "selectors/entitiesSelector"; import { ActionData } from "reducers/entityReducers/actionsReducer"; import { createActionRequest } from "actions/actionActions"; +import { Datasource } from "api/DatasourcesApi"; +import { Plugin } from "api/PluginApi"; +import { PLUGIN_PACKAGE_DBS } from "constants/QueryEditorConstants"; const getApiDraft = (state: AppState, id: string) => { const drafts = state.ui.apiPane.drafts; @@ -484,6 +490,47 @@ function* handleCreateNewApiActionSaga( } } +function* handleCreateNewQueryActionSaga( + action: ReduxAction<{ pageId: string }>, +) { + const { pageId } = action.payload; + const applicationId = yield select(getCurrentApplicationId); + const actions = yield select(getActions); + const dataSources = yield select(getDataSources); + const plugins = yield select(getPlugins); + const pluginIds = plugins + .filter((plugin: Plugin) => PLUGIN_PACKAGE_DBS.includes(plugin.packageName)) + .map((plugin: Plugin) => plugin.id); + const validDataSources: Array = []; + dataSources.forEach((dataSource: Datasource) => { + if (pluginIds?.includes(dataSource.pluginId)) { + validDataSources.push(dataSource); + } + }); + if (validDataSources.length) { + const pageApiNames = actions + .filter((a: ActionData) => a.config.pageId === pageId) + .map((a: ActionData) => a.config.name); + const newQueryName = getNextEntityName("Query", pageApiNames); + const dataSourceId = validDataSources[0].id; + yield put( + createActionRequest({ + name: newQueryName, + pageId, + datasource: { + id: dataSourceId, + }, + actionConfiguration: {}, + }), + ); + history.push( + QUERY_EDITOR_URL_WITH_SELECTED_PAGE_ID(applicationId, pageId, pageId), + ); + } else { + history.push(DATA_SOURCES_EDITOR_URL(applicationId, pageId)); + } +} + export default function* root() { yield all([ takeEvery(ReduxActionTypes.INIT_API_PANE, initApiPaneSaga), @@ -497,6 +544,10 @@ export default function* root() { ReduxActionTypes.CREATE_NEW_API_ACTION, handleCreateNewApiActionSaga, ), + takeEvery( + ReduxActionTypes.CREATE_NEW_QUERY_ACTION, + handleCreateNewQueryActionSaga, + ), // Intercepting the redux-form change actionType takeEvery(ReduxFormActionTypes.VALUE_CHANGE, formValueChangeSaga), takeEvery(ReduxFormActionTypes.ARRAY_REMOVE, formValueChangeSaga),