PromucFlow_constructor/app/client/src/sagas/SaaSPaneSagas.ts
Hetu Nandu 5ab58504a8
feat: Create Query asks for Page (#29253)
## Description

Shows a list of pages to select when creating a query. It will only show
when there are multiple pages in an app. If not, clicking the button
directly creates the query in the only page.


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

#### Media


![Page](https://github-production-user-asset-6210df.s3.amazonaws.com/20532920/285431639-91758301-7b9e-4ee8-a209-afc563b0d96b.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A/20231204/us-east-1/s3/aws4_request&X-Amz-Date=20231204T093109Z&X-Amz-Expires=300&X-Amz-Signature=bfff9c87288f9ca913c14bc3284e9974ad805130462a375adb271936777eec26&X-Amz-SignedHeaders=host&actor_id=12022471&key_id=0&repo_id=275993885)


#### Type of change

- New feature (non-breaking change which adds functionality)

## 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
- [x] Manual
- [ ] JUnit
- [ ] Jest
- [x] 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

- **Refactor**
- Streamlined the process for creating queries from the datasource
editor across various test suites.
- Enhanced the control flow and logic for handling data sources and
queries in test scenarios.

- **Bug Fixes**
- Standardized the method of creating new queries after saving a
datasource to improve test reliability.
- Removed deprecated code and updated test cases to use the new
`CreateQueryForDS` function.

- **Tests**
- Updated test scripts to reflect changes in datasource interaction and
query creation.
- Added new assertions and modified existing ones to accommodate the
updated test flows.

- **Chores**
- Cleaned up import statements and removed unused code across multiple
test files.
- Commented out unused properties and methods to improve code
maintainability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2023-12-05 11:20:36 +05:30

132 lines
4.3 KiB
TypeScript

import { all, put, select, takeEvery } from "redux-saga/effects";
import type {
ApplicationPayload,
ReduxAction,
} from "@appsmith/constants/ReduxActionConstants";
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
import history from "utils/history";
import {
getGenerateCRUDEnabledPluginMap,
getPlugin,
} from "@appsmith/selectors/entitiesSelector";
import type { Action } from "entities/Action";
import { PluginType } from "entities/Action";
import type { GenerateCRUDEnabledPluginMap, Plugin } from "api/PluginApi";
import {
generateTemplateFormURL,
saasEditorApiIdURL,
saasEditorDatasourceIdURL,
} from "@appsmith/RouteBuilder";
import { getCurrentPageId } from "selectors/editorSelectors";
import type { CreateDatasourceSuccessAction } from "actions/datasourceActions";
import { getQueryParams } from "utils/URLUtils";
import { getIsGeneratePageInitiator } from "utils/GenerateCrudUtil";
import { DATASOURCE_SAAS_FORM } from "@appsmith/constants/forms";
import { initialize } from "redux-form";
import { omit } from "lodash";
import {
getApplicationByIdFromWorkspaces,
getCurrentApplicationIdForCreateNewApp,
} from "@appsmith/selectors/applicationSelectors";
import { TEMP_DATASOURCE_ID } from "constants/Datasource";
function* handleDatasourceCreatedSaga(
actionPayload: CreateDatasourceSuccessAction,
) {
const { isDBCreated, payload } = actionPayload;
const plugin: Plugin | undefined = yield select(getPlugin, payload.pluginId);
// Only look at SAAS plugins
if (!plugin) return;
if (plugin.type !== PluginType.SAAS) return;
const currentApplicationIdForCreateNewApp: string | undefined = yield select(
getCurrentApplicationIdForCreateNewApp,
);
const application: ApplicationPayload | undefined = yield select(
getApplicationByIdFromWorkspaces,
currentApplicationIdForCreateNewApp || "",
);
const pageId: string = !!currentApplicationIdForCreateNewApp
? application?.defaultPageId
: yield select(getCurrentPageId);
yield put(initialize(DATASOURCE_SAAS_FORM, omit(payload, "name")));
const queryParams = getQueryParams();
const updatedDatasource = payload;
const isGeneratePageInitiator = getIsGeneratePageInitiator(
queryParams.isGeneratePageMode,
);
const generateCRUDSupportedPlugin: GenerateCRUDEnabledPluginMap =
yield select(getGenerateCRUDEnabledPluginMap);
// isGeneratePageInitiator ensures that datasource is being created from generate page with data
// then we check if the current plugin is supported for generate page with data functionality
// and finally isDBCreated ensures that datasource is not in temporary state and
// user has explicitly saved the datasource, before redirecting back to generate page
if (
isGeneratePageInitiator &&
updatedDatasource.pluginId &&
generateCRUDSupportedPlugin[updatedDatasource.pluginId] &&
isDBCreated
) {
history.push(
generateTemplateFormURL({
pageId,
params: {
datasourceId: updatedDatasource.id,
},
}),
);
} else if (
!currentApplicationIdForCreateNewApp ||
(!!currentApplicationIdForCreateNewApp && payload.id !== TEMP_DATASOURCE_ID)
) {
history.push(
saasEditorDatasourceIdURL({
pageId,
pluginPackageName: plugin.packageName,
datasourceId: payload.id,
params: {
from: "datasources",
pluginId: plugin?.id,
viewMode: "false",
},
}),
);
}
}
function* handleActionCreatedSaga(actionPayload: ReduxAction<Action>) {
const { id, pageId, pluginId } = actionPayload.payload;
const plugin: Plugin | undefined = yield select(getPlugin, pluginId);
if (!plugin) return;
if (plugin.type !== "SAAS") return;
history.push(
saasEditorApiIdURL({
pageId,
pluginPackageName: plugin.packageName,
apiId: id,
params: {
editName: "true",
from: "datasources",
},
}),
);
}
// since we are re-using the query editor form names for SAAS actions, all formValueChanges will be handled in the QuerypaneSagas.
export default function* root() {
yield all([
takeEvery(
ReduxActionTypes.CREATE_DATASOURCE_SUCCESS,
handleDatasourceCreatedSaga,
),
takeEvery(ReduxActionTypes.CREATE_ACTION_SUCCESS, handleActionCreatedSaga),
]);
}