diff --git a/src/components/Table/ColumnMenu/FieldSettings/DefaultValueInput.tsx b/src/components/Table/ColumnMenu/FieldSettings/DefaultValueInput.tsx index 730bad8e..c4275a42 100644 --- a/src/components/Table/ColumnMenu/FieldSettings/DefaultValueInput.tsx +++ b/src/components/Table/ColumnMenu/FieldSettings/DefaultValueInput.tsx @@ -15,6 +15,7 @@ import { WIKI_LINKS } from "@src/constants/externalLinks"; import { name } from "@root/package.json"; /* eslint-disable import/no-webpack-loader-syntax */ import defaultValueDefs from "!!raw-loader!./defaultValue.d.ts"; +import { useProjectContext } from "@src/contexts/ProjectContext"; const _CodeEditor = lazy( () => import("@src/components/CodeEditor" /* webpackChunkName: "CodeEditor" */) @@ -31,14 +32,19 @@ export interface IDefaultValueInputProps extends IMenuModalProps { } const CodeEditor = ({ type, config, handleChange }) => { + const { compatibleRowyRunVersion } = useProjectContext(); + const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" }); const returnType = getFieldProp("dataType", type) ?? "any"; - const defaultValueFn = config.defaultValue?.defaultValueFn - ? config.defaultValue?.defaultValueFn + + const dynamicValueFn = functionBodyOnly + ? config.defaultValue?.script + : config.defaultValue?.dynamicValueFn + ? config.defaultValue?.dynamicValueFn : config.defaultValue?.script - ? `const defaultValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{ + ? `const dynamicValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{ ${config.defaultValue.script} }` - : `const defaultValueFn: DefaultValue = async ({row,ref,db,storage,auth})=>{ + : `const dynamicValueFn : DefaultValue = async ({row,ref,db,storage,auth})=>{ // Write your default value code here // for example: // generate random hex color @@ -48,15 +54,17 @@ const CodeEditor = ({ type, config, handleChange }) => { }`; return ( <_CodeEditor - value={defaultValueFn} - diagnosticsOptions={diagnosticsOptions} + value={dynamicValueFn} + diagnosticsOptions={functionBodyOnly ? undefined : diagnosticsOptions} extraLibs={[ defaultValueDefs.replace( `"PLACEHOLDER_OUTPUT_TYPE"`, `${returnType} | Promise<${returnType}>` ), ]} - onChange={handleChange("defaultValue.defaultValueFn")} + onChange={handleChange( + functionBodyOnly ? "defaultValue.script" : "defaultValue.dynamicValueFn" + )} /> ); }; diff --git a/src/components/fields/Action/Settings.tsx b/src/components/fields/Action/Settings.tsx index a776efff..d3356c4e 100644 --- a/src/components/fields/Action/Settings.tsx +++ b/src/components/fields/Action/Settings.tsx @@ -34,6 +34,7 @@ import { WIKI_LINKS } from "@src/constants/externalLinks"; import { useAppContext } from "@src/contexts/AppContext"; /* eslint-disable import/no-webpack-loader-syntax */ import actionDefs from "!!raw-loader!./action.d.ts"; +import { RUN_ACTION_TEMPLATE, UNDO_ACTION_TEMPLATE } from "./templates"; const diagnosticsOptions = { noSemanticValidation: false, @@ -47,11 +48,12 @@ const CodeEditor = lazy( ); const Settings = ({ config, onChange }) => { - const { tableState, roles } = useProjectContext(); + const { tableState, roles, compatibleRowyRunVersion } = useProjectContext(); const { projectId } = useAppContext(); const [activeStep, setActiveStep] = useState< "requirements" | "friction" | "action" | "undo" | "customization" >("requirements"); + const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" }); const steps = config.isActionScript && _get(config, "undo.enabled") ? ["requirements", "friction", "action", "undo", "customization"] @@ -95,72 +97,25 @@ const Settings = ({ config, onChange }) => { typeof config.confirmation === "string" && config.confirmation !== ""); - const runFn = config.runFn + const runFn = functionBodyOnly + ? config?.script + : config.runFn ? config.derivativeFn : config?.script ? `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => { ${config.script.replace(/utilFns.getSecret/g, "rowy.secrets.getSecret")} }` - : `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => { - // Write your action code here - // for example: - // const authToken = await rowy.secrets.getSecret("service") - // try { - // const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{ - // method: 'PUT', - // headers: { - // 'Content-Type': 'application/json', - // 'Authorization': authToken - // }, - // body: JSON.stringify(row) - // }) - // - // return { - // success: true, - // message: 'User updated successfully on example service', - // status: "upto date" - // } - // } catch (error) { - // return { - // success: false, - // message: 'User update failed on example service', - // } - // } - // checkout the documentation for more info: https://docs.rowy.io/field-types/action#script - }`; + : RUN_ACTION_TEMPLATE; - const undoFn = config.undoFn + const undoFn = functionBodyOnly + ? _get(config, "undo.script") + : config.undoFn ? config.undoFn : _get(config, "undo.script") - ? `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => { + ? `const action : Action = async ({row,ref,db,storage,auth,actionParams,user}) => { ${_get(config, "undo.script")} }` - : `const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => { - // Write your undo code here - // for example: - // const authToken = await rowy.secrets.getSecret("service") - // try { - // const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{ - // method: 'DELETE', - // headers: { - // 'Content-Type': 'application/json', - // 'Authorization': authToken - // }, - // body: JSON.stringify(row) - // }) - // - // return { - // success: true, - // message: 'User deleted successfully on example service', - // status: null - // } - // } catch (error) { - // return { - // success: false, - // message: 'User delete failed on example service', - // } - // } - }`; + : UNDO_ACTION_TEMPLATE; return ( { { }> { + // Write your action code here + // for example: + // const authToken = await rowy.secrets.getSecret("service") + // try { + // const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{ + // method: 'PUT', + // headers: { + // 'Content-Type': 'application/json', + // 'Authorization': authToken + // }, + // body: JSON.stringify(row) + // }) + // + // return { + // success: true, + // message: 'User updated successfully on example service', + // status: "upto date" + // } + // } catch (error) { + // return { + // success: false, + // message: 'User update failed on example service', + // } + // } + // checkout the documentation for more info: https://docs.rowy.io/field-types/action#script + }`; + +export const UNDO_ACTION_TEMPLATE = `const action : Action = async ({row,ref,db,storage,auth,actionParams,user}) => { + // Write your undo code here + // for example: + // const authToken = await rowy.secrets.getSecret("service") + // try { + // const resp = await fetch('https://example.com/api/v1/users/'+ref.id,{ + // method: 'DELETE', + // headers: { + // 'Content-Type': 'application/json', + // 'Authorization': authToken + // }, + // body: JSON.stringify(row) + // }) + // + // return { + // success: true, + // message: 'User deleted successfully on example service', + // status: null + // } + // } catch (error) { + // return { + // success: false, + // message: 'User delete failed on example service', + // } + // } + }`; diff --git a/src/components/fields/Derivative/Settings.tsx b/src/components/fields/Derivative/Settings.tsx index 0385ed49..436c3d8e 100644 --- a/src/components/fields/Derivative/Settings.tsx +++ b/src/components/fields/Derivative/Settings.tsx @@ -33,16 +33,20 @@ export default function Settings({ onBlur, errors, }: ISettingsProps) { - const { tableState } = useProjectContext(); + const { tableState, compatibleRowyRunVersion } = useProjectContext(); + if (!tableState?.columns) return <>; const columns = Object.values(tableState.columns); const returnType = getFieldProp("dataType", config.renderFieldType) ?? "any"; - console.log({ returnType, r: config.renderFieldType }); const columnOptions = columns .filter((column) => column.fieldName !== fieldName) .filter((column) => column.type !== FieldType.subTable) .map((c) => ({ label: c.name, value: c.key })); - const code = config.derivativeFn + + const functionBodyOnly = compatibleRowyRunVersion!({ maxVersion: "1.4.0" }); + const code = functionBodyOnly + ? config?.script + : config.derivativeFn ? config.derivativeFn : config?.script ? `const derivative:Derivative = async ({row,ref,db,storage,auth})=>{ @@ -118,7 +122,9 @@ export default function Settings({ }> ` ), ]} - onChange={onChange("code")} + onChange={onChange(functionBodyOnly ? "script" : "code")} /> diff --git a/src/utils/rowyRun.ts b/src/utils/rowyRun.ts index ec08ee2f..22cf7520 100644 --- a/src/utils/rowyRun.ts +++ b/src/utils/rowyRun.ts @@ -17,7 +17,7 @@ export const rowyRun = async ({ route, body, params, - localhost = true, + localhost = false, json = true, signal, }: IRowyRunRequestProps) => {