Skip to content

Commit

Permalink
review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hetunandu committed Sep 25, 2024
1 parent e6ded1b commit 2b05378
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import { API_EDITOR_FORM_NAME } from "ee/constants/forms";
import { getCurrentEnvironmentId } from "ee/selectors/environmentSelectors";
import get from "lodash/get";
import { useSelector } from "react-redux";
import type { Action, ApiAction, Property } from "entities/Action";
import {
type Action,
type ApiAction,
isAPIAction,
type Property,
} from "entities/Action";
import { getDatasources } from "ee/selectors/entitiesSelector";

function useGetFormActionValues() {
const formValues = useSelector(
getFormValues(API_EDITOR_FORM_NAME),
) as ApiAction;
const formValues = useSelector(getFormValues(API_EDITOR_FORM_NAME)) as Action;
const datasources = useSelector(getDatasources);
const currentEnvironment = useSelector(getCurrentEnvironmentId);

// In an unlikely scenario where form is not initialised,
// return empty values to avoid form ui issues
if (!isAPIAction(formValues)) {
return {
actionHeaders: [],
actionParams: [],
autoGeneratedHeaders: [],
datasourceParams: [],
datasourceHeaders: [],
};
}

const actionHeaders = get(
formValues,
Expand All @@ -26,21 +43,17 @@ function useGetFormActionValues() {
[],
) as Property[];

const datasources = useSelector(getDatasources);

let datasourceFromAction: Action["datasource"] | undefined = get(
formValues,
"datasource",
);

if (datasourceFromAction && datasourceFromAction.hasOwnProperty("id")) {
if (datasourceFromAction && Object.hasOwn(datasourceFromAction, "id")) {
datasourceFromAction = datasources.find(
(d) => d.id === datasourceFromAction?.id,
);
}

const currentEnvironment = useSelector(getCurrentEnvironmentId);

const datasourceHeaders = get(
datasourceFromAction,
`datasourceStorages.${currentEnvironment}.datasourceConfiguration.headers`,
Expand Down

0 comments on commit 2b05378

Please sign in to comment.