PromucFlow_constructor/app/client/src/components/formControls/KeyValueArrayControl.tsx

294 lines
8.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useCallback } from "react";
import {
Field,
FieldArray,
WrappedFieldArrayProps,
WrappedFieldMetaProps,
WrappedFieldInputProps,
} from "redux-form";
import styled from "styled-components";
import BaseControl, { ControlProps, ControlData } from "./BaseControl";
import { ControlType } from "constants/PropertyControlConstants";
import DynamicTextField from "components/editorComponents/form/fields/DynamicTextField";
import { Colors } from "constants/Colors";
import {
Case,
Classes,
Icon,
IconSize,
Text,
TextInput,
TextInputProps,
TextType,
feat: Renamed design system package (#19854) ## Description This PR includes changes for renaming design system package. Since we are building new package for the refactored design system components, the old package is renaming to design-system-old. Fixes #19536 ## Type of change - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) ## How Has This Been Tested? - Manual - Jest - 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 - [x] My code follows the style guidelines of this project - [x] 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 - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag ### QA activity: - [ ] Test plan has been approved by relevant developers - [ ] Test plan has been peer reviewed by QA - [ ] Cypress test cases have been added and approved by either SDET or manual QA - [ ] Organized project review call with relevant stakeholders after Round 1/2 of QA - [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-01-23 03:50:47 +00:00
} from "design-system-old";
fix: ds discard popup issue fixes (#19114) This PR includes following changes: - With latest datasource autosave improvements, we do not save the datasource immediately but save it on explicit click of save button, so in case user has not saved any changes and tries to leave the page, we have added a popup to inform users that they have unsaved changes and whether they would like to save them or not. - The issue was in case of postgres and authenticated API datasource, this popup was getting seen even when the user has not made any changes in the datasource configuration. This PR solves that issue. - The unsaved changes popup needs to be shown only when user has made any new changes in the datasource form TL;DR - We have used redux form's isDirty method to check if user has made any new updates to the form or not. This isDirty compares initial value of datasource form with current form value and if current form value is different it shows the unsaved changes popup - The issue occurred because in case of postgres and authenticated API datasource, we initialise 1 default pair of host address and port (postgres), and default pairs of headers and query parameters (Authenticated API). These initial default changes made the form dirty and so user used to see the popup even when they have not explicitly made any changes. - This PR fixes the issue by setting initial form value with these defaults so they do not make the form dirty. Fixes #18962 , #18998 https://user-images.githubusercontent.com/30018882/208931098-b570e3c4-10bc-4b76-bd54-531ccf869436.mov Co-authored-by: “sneha122” <“sneha@appsmith.com”> Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-12-30 10:23:24 +00:00
import { setDefaultKeyValPairFlag } from "actions/datasourceActions";
import { useDispatch } from "react-redux";
export interface KeyValueArrayControlProps extends ControlProps {
name: string;
label: string;
maxLen?: number;
description?: string;
actionConfig?: any;
extraData?: ControlData[];
isRequired?: boolean;
}
const FormRowWithLabel = styled.div`
display: flex;
flex: 1;
flex-direction: row;
& svg {
cursor: pointer;
}
`;
const StyledTextInput = styled(TextInput)`
min-width: 66px;
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0px;
}
`;
const CenteredIcon = styled(Icon)`
align-self: center;
margin-left: 15px;
`;
const AddMoreAction = styled.div`
width: fit-content;
cursor: pointer;
display: flex;
margin-top: 16px;
margin-left: 12px;
.${Classes.TEXT} {
margin-left: 8px;
color: ${Colors.GRAY};
}
svg {
fill: ${Colors.GRAY};
path {
fill: unset;
}
}
&:hover {
.${Classes.TEXT} {
color: ${Colors.CHARCOAL};
}
svg {
fill: ${Colors.CHARCOAL};
}
}
`;
function KeyValueRow(
props: KeyValueArrayControlProps & WrappedFieldArrayProps,
) {
const { extraData = [] } = props;
const keyName = getFieldName(extraData[0]?.configProperty);
const valueName = getFieldName(extraData[1]?.configProperty);
const keyFieldProps = extraData[0];
fix: ds discard popup issue fixes (#19114) This PR includes following changes: - With latest datasource autosave improvements, we do not save the datasource immediately but save it on explicit click of save button, so in case user has not saved any changes and tries to leave the page, we have added a popup to inform users that they have unsaved changes and whether they would like to save them or not. - The issue was in case of postgres and authenticated API datasource, this popup was getting seen even when the user has not made any changes in the datasource configuration. This PR solves that issue. - The unsaved changes popup needs to be shown only when user has made any new changes in the datasource form TL;DR - We have used redux form's isDirty method to check if user has made any new updates to the form or not. This isDirty compares initial value of datasource form with current form value and if current form value is different it shows the unsaved changes popup - The issue occurred because in case of postgres and authenticated API datasource, we initialise 1 default pair of host address and port (postgres), and default pairs of headers and query parameters (Authenticated API). These initial default changes made the form dirty and so user used to see the popup even when they have not explicitly made any changes. - This PR fixes the issue by setting initial form value with these defaults so they do not make the form dirty. Fixes #18962 , #18998 https://user-images.githubusercontent.com/30018882/208931098-b570e3c4-10bc-4b76-bd54-531ccf869436.mov Co-authored-by: “sneha122” <“sneha@appsmith.com”> Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-12-30 10:23:24 +00:00
const dispatch = useDispatch();
const addRow = useCallback(() => {
if (keyName && valueName) {
props.fields.push({ [keyName[1]]: "", [valueName[1]]: "" });
} else {
props.fields.push({ key: "", value: "" });
}
}, [keyName, valueName]);
useEffect(() => {
// Always maintain 1 row
if (props.fields.length < 1) {
for (let i = props.fields.length; i < 1; i += 1) {
addRow();
fix: ds discard popup issue fixes (#19114) This PR includes following changes: - With latest datasource autosave improvements, we do not save the datasource immediately but save it on explicit click of save button, so in case user has not saved any changes and tries to leave the page, we have added a popup to inform users that they have unsaved changes and whether they would like to save them or not. - The issue was in case of postgres and authenticated API datasource, this popup was getting seen even when the user has not made any changes in the datasource configuration. This PR solves that issue. - The unsaved changes popup needs to be shown only when user has made any new changes in the datasource form TL;DR - We have used redux form's isDirty method to check if user has made any new updates to the form or not. This isDirty compares initial value of datasource form with current form value and if current form value is different it shows the unsaved changes popup - The issue occurred because in case of postgres and authenticated API datasource, we initialise 1 default pair of host address and port (postgres), and default pairs of headers and query parameters (Authenticated API). These initial default changes made the form dirty and so user used to see the popup even when they have not explicitly made any changes. - This PR fixes the issue by setting initial form value with these defaults so they do not make the form dirty. Fixes #18962 , #18998 https://user-images.githubusercontent.com/30018882/208931098-b570e3c4-10bc-4b76-bd54-531ccf869436.mov Co-authored-by: “sneha122” <“sneha@appsmith.com”> Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-12-30 10:23:24 +00:00
// Since we are initializing one default key value pair, it needs to stored in redux store
// so that it can be used to initilize datasource config form as well
dispatch(setDefaultKeyValPairFlag(props.configProperty));
}
}
2020-06-04 13:49:22 +00:00
}, [props.fields, keyName, valueName]);
useEffect(() => {
if (typeof props.fields.getAll() === "string") {
const fieldsValue: any[] = JSON.parse(`${props.fields.getAll()}`);
props.fields.removeAll();
fieldsValue.forEach((value, index) => {
props.fields.insert(index, value);
});
}
}, [props.fields]);
const keyFieldValidate = useCallback(
(value: string) => {
if (value && keyFieldProps?.validationRegex) {
const regex = new RegExp(keyFieldProps?.validationRegex);
return regex.test(value)
? { isValid: true }
: { isValid: false, message: keyFieldProps.validationMessage };
}
return undefined;
},
[keyFieldProps?.validationRegex, keyFieldProps?.validationMessage],
);
return typeof props.fields.getAll() === "object" ? (
<>
{props.fields.map((field: any, index: number) => {
let keyTextFieldName = `${field}.key`;
let valueTextFieldName = `${field}.value`;
if (keyName && Array.isArray(keyName) && keyName?.length)
keyTextFieldName = `${field}.${keyName[1]}`;
if (valueName && Array.isArray(valueName) && valueName?.length)
valueTextFieldName = `${field}.${valueName[1]}`;
return (
<FormRowWithLabel
key={index}
style={{ marginTop: index > 0 ? "16px" : "0px" }}
>
2021-12-07 09:45:18 +00:00
<div
data-replay-id={btoa(keyTextFieldName)}
style={{ width: "20vw" }}
2021-12-07 09:45:18 +00:00
>
<Field
component={renderTextInput}
name={keyTextFieldName}
props={{
dataType: getType(extraData[0]?.dataType),
defaultValue: extraData[0]?.initialValue,
keyFieldValidate,
placeholder: props.extraData
? props.extraData[1]?.placeholderText
: "",
isRequired: extraData[0]?.isRequired,
name: keyTextFieldName,
}}
/>
</div>
{!props.actionConfig && (
<div style={{ marginLeft: "16px", width: "20vw" }}>
<div
feat: adding slug names in urls (#10957) * Init commit clean urls * Changes to builder route * Refactored URLs * Remove default params from url builder functions. * Fixed more urls * Changed selector name * Minor url correction * Type fixes * Jest fixes * Fixed routing for old published apps * Fixed url slug replace * page-1 -> page1 as default page slug name * Remove application id from init editor calls * Use default page slug * Added comments and placeholder values for slug names * variable rename * Removed redirection and added back the old routes * Prevent page slug name recompute * Fixed home page load in view mode * Added null checks * Fixed jest test * Fixed jest test * Update URL slugs when app/page name changes * Added unit tests and updates types * Removed unused code * * Removed duplication fetch page call. * Fixes #11354 * Fixed sign up flow * Refactored initializeEditorSaga * Fixed warnings * Fixed integrations screen URL bugs * Cypress fixes * Fixed slug names in copy/move operations and pages screen * Minor refactor * Fixed page highlight bug in published apps * Added new url factory and middleware to store url params * Changed store to default export and fix unit tests * Fixed slugs unit test * Minor fixes * Fixes #11379 * Fixed set as home page feature * Updated types * app id adjustments for cypress * Fixed bad merge * Refactored routes to functional component * * Fixed EE active entity highlight. * Remove unused code in editor router. * jest fix * Mock history to prevent security errors * constant rename * Removed console logs * Fixed page id regex * Do not check for /pages in url * Fixed missing pageId on quick edit/deploy clicks * Missed files from previous commit * Fixed warnings * Fixed jest test * New api integration * feat: Add applicationVersion property to Application (#11626) Added a new property to Application object - applicationVersion. This property can be used to identity when there is a breaking change and can not be solved with migration. FE will use this property to detect such conditions. Another API is added to migrate the applicationVersion to latest version when user migrates the breaking changes. * Added manual upgrade modal. * Test fix * Fixed jest test * function rename * Fix deploy error * Added null check * Changes to persist URL search params when redirecting * Added updates tooltip * More unit test cases * Fixed git url redirection * Fix warning * Fixed evaluation on upgrade * Fixed warnings * File rename * Added cypress for clean urls * Fixed import/export/fork cypress * Cypress api server fixes * Fixed mongo spec * Fixed replay spec * Fixed comments spec * More cypress fixes * Fixed tooltip in update btn * Text size changes * Minor fixes * Jest test fix * Fixed type error * Fixed warnings * Fixed todo comments * Moved description to constants file * Fixed cypress CI run crash * Fixes git cypress failures * Import/Export cypress test fixes * Import export fork cypress fixes * Explorer test fix * Switch branch test fix * Added applicationVersion in export app json * Calls plugin forms in parallel * Fixed warnings * Fixed warning * Import export CI fixes * Reverts previous changes * Fixes import export * Fixed import export cypress URL verification * Pass applicationVersion when duplicating application * Cypress fix * Dummy commit Co-authored-by: Nayan <nayan@appsmith.com>
2022-03-25 10:43:26 +00:00
data-replay-id={btoa(valueTextFieldName)}
style={{ display: "flex", flexDirection: "row" }}
>
<Field
component={renderTextInput}
name={valueTextFieldName}
props={{
dataType: getType(extraData[1]?.dataType),
defaultValue: extraData[1]?.initialValue,
placeholder: props.extraData
? props.extraData[1]?.placeholderText
: "",
name: valueTextFieldName,
isRequired: extraData[1]?.isRequired,
}}
/>
<CenteredIcon
className="t--delete-field"
name="delete"
onClick={() => props.fields.remove(index)}
size={IconSize.LARGE}
/>
</div>
</div>
)}
{props.actionConfig && (
<DynamicTextField
name={`${field}.value`}
placeholder={
props.actionConfig[index].mandatory &&
props.actionConfig[index].type
? `Value (Type: ${props.actionConfig[index].type})`
: `Value (optional)`
}
/>
)}
</FormRowWithLabel>
);
})}
<AddMoreAction className="t--add-field" onClick={addRow}>
<Icon className="t--addApiHeader" name="add-more" size={IconSize.XXL} />
<Text case={Case.UPPERCASE} type={TextType.H5}>
Add more
</Text>
</AddMoreAction>
</>
) : null;
}
class KeyValueArrayControl extends BaseControl<KeyValueArrayControlProps> {
render() {
const name = getFieldName(this.props.configProperty);
return (
<FieldArray
component={KeyValueRow}
rerenderOnEveryChange={false}
{...this.props}
name={name ? name[0] : ""}
/>
);
}
getControlType(): ControlType {
return "KEYVALUE_ARRAY";
}
}
const getFieldName = (configProperty: string): string[] | undefined => {
if (configProperty) return configProperty.split("[*].");
};
const getType = (dataType: string | undefined) => {
switch (dataType) {
case "PASSWORD":
return "password";
case "NUMBER":
return "number";
default:
return "text";
}
};
function renderTextInput(
props: TextInputProps & {
dataType?: "text" | "number" | "password";
placeholder?: string;
defaultValue: string | number;
isRequired: boolean;
keyFieldValidate?: (value: string) => { isValid: boolean; message: string };
errorMsg?: string;
helperText?: string;
} & {
meta: Partial<WrappedFieldMetaProps>;
input: Partial<WrappedFieldInputProps>;
},
): JSX.Element {
return (
<StyledTextInput
dataType={props.dataType}
defaultValue={props.defaultValue}
errorMsg={props.errorMsg}
helperText={props.helperText}
name={props.input?.name}
onChange={props.input.onChange}
placeholder={props.placeholder}
validator={props.keyFieldValidate}
value={props.input.value}
width="100%"
/>
);
}
export default KeyValueArrayControl;