PromucFlow_constructor/app/client/src/components/formControls/utils.ts

281 lines
7.6 KiB
TypeScript
Raw Normal View History

import { isBoolean, get, set } from "lodash";
import { HiddenType } from "./BaseControl";
2021-04-05 10:11:04 +00:00
export const evaluateCondtionWithType = (
conditions: Array<boolean> | undefined,
type: string | undefined,
) => {
if (conditions) {
let flag;
//this is where each conditions gets evaluated
if (conditions.length > 1) {
if (type === "AND") {
flag = conditions.reduce((acc: any, item: boolean) => {
return acc && item;
}, conditions[0]);
} else if (type === "OR") {
flag = conditions.reduce((acc: any, item: boolean) => {
return acc || item;
}, undefined);
}
} else {
flag = conditions[0];
}
return flag;
}
};
export const isHiddenConditionsEvaluation = (
values: any,
hidden?: HiddenType,
): any => {
if (!!hidden && !isBoolean(hidden)) {
//if nested condtions are there recursively from bottom to top call this function on each condtion
let conditionType, conditions;
if ("conditionType" in hidden) {
conditionType = hidden.conditionType;
}
if ("conditions" in hidden) {
conditions = hidden.conditions;
}
if (Array.isArray(conditions)) {
conditions = conditions.map((rule: any) => {
return isHiddenConditionsEvaluation(values, rule);
});
} else {
return caculateIsHidden(values, hidden);
}
return evaluateCondtionWithType(conditions, conditionType);
}
};
export const caculateIsHidden = (values: any, hiddenConfig?: HiddenType) => {
if (!!hiddenConfig && !isBoolean(hiddenConfig)) {
2021-04-05 10:11:04 +00:00
let valueAtPath;
let value, comparison;
if ("path" in hiddenConfig) {
valueAtPath = get(values, hiddenConfig.path);
}
if ("value" in hiddenConfig) {
value = hiddenConfig.value;
}
if ("comparison" in hiddenConfig) {
comparison = hiddenConfig.comparison;
}
2021-04-05 10:11:04 +00:00
switch (comparison) {
case "EQUALS":
return valueAtPath === value;
case "NOT_EQUALS":
return valueAtPath !== value;
case "GREATER":
return valueAtPath > value;
case "LESSER":
return valueAtPath < value;
case "IN":
return Array.isArray(value) && value.includes(valueAtPath);
case "NOT_IN":
return Array.isArray(value) && !value.includes(valueAtPath);
default:
return true;
}
}
2021-04-05 10:11:04 +00:00
};
2021-04-05 10:11:04 +00:00
export const isHidden = (values: any, hiddenConfig?: HiddenType) => {
if (!!hiddenConfig && !isBoolean(hiddenConfig)) {
if ("conditionType" in hiddenConfig) {
//check if nested conditions exist
return isHiddenConditionsEvaluation(values, hiddenConfig);
} else {
return caculateIsHidden(values, hiddenConfig);
}
}
return !!hiddenConfig;
};
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
export enum ViewTypes {
JSON = "json",
COMPONENT = "component",
}
export const alternateViewTypeInputConfig = {
label: "",
isValid: true,
controlType: "QUERY_DYNAMIC_INPUT_TEXT",
evaluationSubstitutionType: "TEMPLATE",
inputType: "JSON",
};
export const getViewType = (values: any, configProperty: string) => {
if (
configProperty.startsWith("actionConfiguration.formData") &&
configProperty.endsWith(".data")
) {
const pathForViewType = configProperty.replace(".data", ".viewType");
return get(values, pathForViewType, ViewTypes.COMPONENT);
} else {
return ViewTypes.COMPONENT;
}
};
export const switchViewType = (
values: any,
configProperty: string,
viewType: string,
formName: string,
changeFormValue: (formName: string, path: string, value: any) => void,
) => {
const newViewType =
viewType === ViewTypes.JSON ? ViewTypes.COMPONENT : ViewTypes.JSON;
const pathForJsonData = configProperty.replace(".data", ".jsonData");
const pathForComponentData = configProperty.replace(
".data",
".componentData",
);
const jsonData = get(values, pathForJsonData);
const componentData = get(values, pathForComponentData);
const currentData = get(values, configProperty);
if (newViewType === ViewTypes.JSON) {
changeFormValue(formName, pathForComponentData, currentData);
if (!!jsonData) {
changeFormValue(formName, configProperty, jsonData);
}
} else {
changeFormValue(formName, pathForJsonData, currentData);
if (!!componentData) {
changeFormValue(formName, configProperty, componentData);
}
}
changeFormValue(
formName,
configProperty.replace(".data", ".viewType"),
newViewType,
);
};
// Function that extracts the initial value from the JSON configs
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
export const getConfigInitialValues = (
config: Record<string, any>[],
multipleViewTypesSupported = false,
) => {
const configInitialValues: Record<string, any> = {};
// We expect the JSON configs to be an array of objects
if (!Array.isArray(config)) return configInitialValues;
// Function to loop through the configs and extract the initial values
const parseConfig = (section: any): any => {
if ("initialValue" in section) {
if (section.controlType === "KEYVALUE_ARRAY") {
section.initialValue.forEach(
(initialValue: string | number, index: number) => {
const configProperty = section.configProperty.replace("*", index);
set(configInitialValues, configProperty, initialValue);
},
);
} else {
set(configInitialValues, section.configProperty, section.initialValue);
}
} else if (section.controlType === "WHERE_CLAUSE") {
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
let logicalTypes = [];
if ("logicalTypes" in section && section.logicalTypes.length > 0) {
logicalTypes = section.logicalTypes;
} else {
logicalTypes = [
{
label: "OR",
value: "OR",
},
{
label: "AND",
value: "AND",
},
];
}
set(
configInitialValues,
`${section.configProperty}.condition`,
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
logicalTypes[0].value,
);
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
if (
multipleViewTypesSupported &&
section.configProperty.includes(".data")
) {
set(
configInitialValues,
section.configProperty.replace(".data", ".viewType"),
"component",
);
set(
configInitialValues,
section.configProperty.replace(".data", ".componentData.condition"),
logicalTypes[0].value,
);
}
}
if ("children" in section) {
section.children.forEach((section: any) => {
parseConfig(section);
});
feat: Switch to JSON view functionality for form components (#11786) * Update: Added flag check for evals - Only running init form evals if the form is of new UQI config * Create: Separated Form config logic - Created a new file to render the form configs of any form component * Update: Enum and getter for view types - Added enum for view types - Added a function to extract the view type of current component * Update: Handling initial values of form - Added checks and updated the default data tree for the components of UQI forms - This is to allow to switch these components to the new data type * Update: updated type of control data - Added viewTypes to the type of form component's control data * Create: Function to change viewType - Added function that will check the current data and switch to the new view type * Update FormControl.tsx - Split the logic of form config to a new file - Updated the memo component to a function so custom config can be passed in it - Added conditional check for the final component * Create: wrapper component to switch view - Component added to change between GUI and JSON view types * Update: Variable name for alternate view types - Changed variable name for alternate view types from viewTypes to alternateViewTypes * Update: Added checks to init section - Added a check for the required values in where clause component * Update: Usage of useMemo hook - Moved the useMemo hook to the top so it is not called conditionally * Update ToggleComponentToJson.tsx - Moved toggled input text view as a constant variable * Update utils.ts - Moved toggled input text view as a constant variable * Update actionProperties.ts - Added switched view type input text to binding path list Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-03-21 05:51:50 +00:00
} else if (
"configProperty" in section &&
multipleViewTypesSupported &&
section.configProperty.includes(".data")
) {
set(
configInitialValues,
section.configProperty.replace(".data", ".viewType"),
"component",
);
if (section.configProperty in configInitialValues) {
set(
configInitialValues,
section.configProperty.replace(".data", ".componentData"),
configInitialValues[section.configProperty],
);
}
}
};
config.forEach((section: any) => {
parseConfig(section);
});
return configInitialValues;
};
Introducing Google Sheets Plugin (#3517) * cherry pick -make new * revert to enable fix from release * attempt to hook into existing datasource editor * gSheets plugin skeleton from Rest API * Changes for database migration * fix for auth code * separate it out * action page loads! * add to explorer * create action from datasource * Editor JSON WIP * working query form * Editor JSON WIP * import to * fix toast message * redirect from datasource and editor pages * fix onboarding * fix imports and constants * refactor form out * refactor queryForm * Merge branch 'release' into feature/google-sheets * Merge branch 'release' into feature/google-sheets * initial values from settings, editor and form * Check * remove dangling code around lightTheme * Safety net * remove class * try mouseover solve * force click * changes from review * fix action form name on import * Merge branch 'release' into feature/google-sheets * minor cleanup * Merge branch 'release' into feature/google-sheets * WIP * Google sheets changes * Merge conflicts * Merging and fixes, needs refactoring * Check * Merge branch 'release' into feature/google-sheets * Fixed tests * Add cloud services env variable * Clean up saga * Clean up * Refactoring * Deleted svg file * Minor fixes * Modified design to allow behaviour in google sheets methods (#3486) * Modified design to allow behaviour in google sheets methods * Review changes * Removed sysout * Added handling of edge cases with table data * Merge branch 'release' into feature/google-sheets * Fixes * Fixes * Added validations * Improved tests * Removed extraneous injected bean * Review changes * Fixed bug with method * Changes to Google sheets plugin's request and response structures (#3692) * Method changes * Removed logging * Renaming options * Reverting pom version * Modified type of collection variables, fixed errors * Converted row offset field to one that supports dynamic bindings * Review changes * List SAAS plugin type actions under lightning menu apis (#3820) * list saas plugin type actions under lightning menu apis * combine saas plugin type actions in the other sub menu of lightning menu Co-authored-by: Hetu Nandu <hetunandu@gmail.com> * Fix merge issues * Prettified query editor and a few fixes w/ ux * Test fixes * Reformatting request * code for REST added (#3876) Co-authored-by: hetunandu <hetu@appsmith.com> * Renamed body to row object * Renamed placeholder for range * Renamed range heading * Modifications to handle range semantics * Use spreadsheet Url instead of id * Ordering of methods * Removed logging * Add tests for Dynamic text controls * Add tests for url helpers * Fix coverage config * Nevermind * Interface changes * There is no body here * Yay to hints * Delete row field is separately handled as row index * placeholder support (#4001) * Fixed tests, typos and creating new sheets with random rows * Switched to using 'rowIndex' throughout * binding path added for query input field (#4016) * - Fixed QA bugs (#4032) - Split delete sheet into two - Removed dynamic query input types from hidden keys * Proper exceptions * Removed extra logging * Throw exception if update method does not match any of the columns * Same for bulk update * Zero-indexed delete row * I'm a space bound rocket ship * Logic to register installations with cs (#4062) * Logic to register installations with cs * Clean up * Casting to string * Checking to see if this makes the test pass * Added an extra null check Co-authored-by: Piyush <piyush@codeitout.com> Co-authored-by: hetunandu <hetu@appsmith.com> Co-authored-by: Hetu Nandu <hetunandu@gmail.com> Co-authored-by: Apeksha Bhosale <7846888+ApekshaBhosale@users.noreply.github.com>
2021-04-22 03:30:09 +00:00
export const actionPathFromName = (
actionName: string,
name: string,
): string => {
const ActionConfigStarts = "actionConfiguration.";
let path = name;
if (path.startsWith(ActionConfigStarts)) {
path = "config." + path.slice(ActionConfigStarts.length);
Introducing Google Sheets Plugin (#3517) * cherry pick -make new * revert to enable fix from release * attempt to hook into existing datasource editor * gSheets plugin skeleton from Rest API * Changes for database migration * fix for auth code * separate it out * action page loads! * add to explorer * create action from datasource * Editor JSON WIP * working query form * Editor JSON WIP * import to * fix toast message * redirect from datasource and editor pages * fix onboarding * fix imports and constants * refactor form out * refactor queryForm * Merge branch 'release' into feature/google-sheets * Merge branch 'release' into feature/google-sheets * initial values from settings, editor and form * Check * remove dangling code around lightTheme * Safety net * remove class * try mouseover solve * force click * changes from review * fix action form name on import * Merge branch 'release' into feature/google-sheets * minor cleanup * Merge branch 'release' into feature/google-sheets * WIP * Google sheets changes * Merge conflicts * Merging and fixes, needs refactoring * Check * Merge branch 'release' into feature/google-sheets * Fixed tests * Add cloud services env variable * Clean up saga * Clean up * Refactoring * Deleted svg file * Minor fixes * Modified design to allow behaviour in google sheets methods (#3486) * Modified design to allow behaviour in google sheets methods * Review changes * Removed sysout * Added handling of edge cases with table data * Merge branch 'release' into feature/google-sheets * Fixes * Fixes * Added validations * Improved tests * Removed extraneous injected bean * Review changes * Fixed bug with method * Changes to Google sheets plugin's request and response structures (#3692) * Method changes * Removed logging * Renaming options * Reverting pom version * Modified type of collection variables, fixed errors * Converted row offset field to one that supports dynamic bindings * Review changes * List SAAS plugin type actions under lightning menu apis (#3820) * list saas plugin type actions under lightning menu apis * combine saas plugin type actions in the other sub menu of lightning menu Co-authored-by: Hetu Nandu <hetunandu@gmail.com> * Fix merge issues * Prettified query editor and a few fixes w/ ux * Test fixes * Reformatting request * code for REST added (#3876) Co-authored-by: hetunandu <hetu@appsmith.com> * Renamed body to row object * Renamed placeholder for range * Renamed range heading * Modifications to handle range semantics * Use spreadsheet Url instead of id * Ordering of methods * Removed logging * Add tests for Dynamic text controls * Add tests for url helpers * Fix coverage config * Nevermind * Interface changes * There is no body here * Yay to hints * Delete row field is separately handled as row index * placeholder support (#4001) * Fixed tests, typos and creating new sheets with random rows * Switched to using 'rowIndex' throughout * binding path added for query input field (#4016) * - Fixed QA bugs (#4032) - Split delete sheet into two - Removed dynamic query input types from hidden keys * Proper exceptions * Removed extra logging * Throw exception if update method does not match any of the columns * Same for bulk update * Zero-indexed delete row * I'm a space bound rocket ship * Logic to register installations with cs (#4062) * Logic to register installations with cs * Clean up * Casting to string * Checking to see if this makes the test pass * Added an extra null check Co-authored-by: Piyush <piyush@codeitout.com> Co-authored-by: hetunandu <hetu@appsmith.com> Co-authored-by: Hetu Nandu <hetunandu@gmail.com> Co-authored-by: Apeksha Bhosale <7846888+ApekshaBhosale@users.noreply.github.com>
2021-04-22 03:30:09 +00:00
}
return `${actionName}.${path}`;
};
export enum PaginationSubComponent {
Limit = "limit",
Offset = "offset",
}
export enum SortingSubComponent {
Column = "column",
Order = "order",
}
export enum WhereClauseSubComponent {
Condition = "condition",
Children = "children",
Key = "key",
Value = "value",
}
export const allowedControlTypes = ["DROP_DOWN", "QUERY_DYNAMIC_INPUT_TEXT"];