diff --git a/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/UQIEditorForm.tsx b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/UQIEditorForm.tsx index fdee4e73717f..44750c91be8a 100644 --- a/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/UQIEditorForm.tsx +++ b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/UQIEditorForm.tsx @@ -2,36 +2,33 @@ import React from "react"; import FormRender from "./FormRender"; import { usePluginActionContext } from "../../../../PluginActionContext"; import { QUERY_EDITOR_FORM_NAME } from "ee/constants/forms"; -import { getFormValues, reduxForm } from "redux-form"; -import type { QueryAction, SaaSAction } from "entities/Action"; -import { useSelector } from "react-redux"; -import { getFormEvaluationState } from "selectors/formSelectors"; +import { reduxForm } from "redux-form"; import { Flex } from "@appsmith/ads"; +import { useGoogleSheetsSetDefaultProperty } from "./hooks/useGoogleSheetsSetDefaultProperty"; +import { useFormData } from "./hooks/useFormData"; +import { useInitFormEvaluation } from "./hooks/useInitFormEvaluation"; const UQIEditorForm = () => { - const { editorConfig, plugin } = usePluginActionContext(); + const { + editorConfig, + plugin: { uiComponent }, + } = usePluginActionContext(); - const formData = useSelector(getFormValues(QUERY_EDITOR_FORM_NAME)) as - | QueryAction - | SaaSAction; + useInitFormEvaluation(); - const formEvaluation = useSelector(getFormEvaluationState); + // Set default values for Google Sheets + useGoogleSheetsSetDefaultProperty(); - let formEvaluationState = {}; - - // Fetching evaluations state only once the formData is populated - if (!!formData) { - formEvaluationState = formEvaluation[formData.id]; - } + const { data, evaluationState } = useFormData(); return ( ); diff --git a/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useFormData.ts b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useFormData.ts new file mode 100644 index 000000000000..61f798cc093e --- /dev/null +++ b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useFormData.ts @@ -0,0 +1,22 @@ +import { useSelector } from "react-redux"; +import { getFormValues } from "redux-form"; +import { QUERY_EDITOR_FORM_NAME } from "ee/constants/forms"; +import type { QueryAction, SaaSAction } from "entities/Action"; +import { getFormEvaluationState } from "selectors/formSelectors"; + +export const useFormData = () => { + const data = useSelector(getFormValues(QUERY_EDITOR_FORM_NAME)) as + | QueryAction + | SaaSAction; + + const formEvaluation = useSelector(getFormEvaluationState); + + let evaluationState = {}; + + // Fetching evaluations state only once the formData is populated + if (!!data) { + evaluationState = formEvaluation[data.id]; + } + + return { data, evaluationState }; +}; diff --git a/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useGoogleSheetsSetDefaultProperty.ts b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useGoogleSheetsSetDefaultProperty.ts new file mode 100644 index 000000000000..b888239e3aaf --- /dev/null +++ b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useGoogleSheetsSetDefaultProperty.ts @@ -0,0 +1,60 @@ +import { useEffect } from "react"; +import { PluginPackageName } from "entities/Action"; +import { merge } from "lodash"; +import { getConfigInitialValues } from "components/formControls/utils"; +import { diff, type Diff } from "deep-diff"; +import { getPathAndValueFromActionDiffObject } from "utils/getPathAndValueFromActionDiffObject"; +import { setActionProperty } from "actions/pluginActionActions"; +import { usePluginActionContext } from "../../../../../PluginActionContext"; +import { useDispatch } from "react-redux"; + +export const useGoogleSheetsSetDefaultProperty = () => { + const { + action, + editorConfig, + plugin: { packageName }, + settingsConfig, + } = usePluginActionContext(); + + const dispatch = useDispatch(); + + useEffect( + function setDefaultValuesForGoogleSheets() { + if (packageName === PluginPackageName.GOOGLE_SHEETS) { + const initialValues = {}; + + merge( + initialValues, + getConfigInitialValues(editorConfig as Record[]), + ); + + merge( + initialValues, + getConfigInitialValues(settingsConfig as Record[]), + ); + + // initialValues contains merge of action, editorConfig, settingsConfig and will be passed to redux form + merge(initialValues, action); + + // @ts-expect-error: Types are not available + const actionObjectDiff: undefined | Diff[] = + diff(action, initialValues); + + const { path = "", value = "" } = { + ...getPathAndValueFromActionDiffObject(actionObjectDiff), + }; + + if (value && path) { + dispatch( + setActionProperty({ + actionId: action.id, + propertyName: path, + value, + }), + ); + } + } + }, + [action, dispatch, editorConfig, packageName, settingsConfig], + ); +}; diff --git a/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useInitFormEvaluation.ts b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useInitFormEvaluation.ts new file mode 100644 index 000000000000..5c52cdfa89d2 --- /dev/null +++ b/app/client/src/PluginActionEditor/components/PluginActionForm/components/UQIEditor/hooks/useInitFormEvaluation.ts @@ -0,0 +1,21 @@ +import { useEffect } from "react"; +import { initFormEvaluations } from "actions/evaluationActions"; +import { useDispatch } from "react-redux"; +import { usePluginActionContext } from "../../../../../PluginActionContext"; + +export const useInitFormEvaluation = () => { + const dispatch = useDispatch(); + + const { + action: { baseId }, + editorConfig, + settingsConfig, + } = usePluginActionContext(); + + useEffect( + function formEvaluationInit() { + dispatch(initFormEvaluations(editorConfig, settingsConfig, baseId)); + }, + [baseId, dispatch, editorConfig, settingsConfig], + ); +};