17266: Avoids adding a property value without a key.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 25 Jan 2021 20:33:55 +0000 (17:33 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Wed, 27 Jan 2021 12:04:45 +0000 (09:04 -0300)
Also resets the form whenever a property key changes, this avoids a previously
validated property value to be taken as valid with a different property key.

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/components/autocomplete/autocomplete.tsx
src/views-components/resource-properties-form/property-field-common.tsx
src/views-components/resource-properties-form/property-key-field.tsx
src/views-components/resource-properties-form/property-value-field.tsx

index e01673b70e6d992bb608fbf958072510dfd4c654..67bc9fa184ffaf10673caa8156b8827e3b0fc356 100644 (file)
@@ -18,6 +18,7 @@ export interface AutocompleteProps<Item, Suggestion> {
     label?: string;
     value: string;
     items: Item[];
+    disabled?: boolean;
     suggestions?: Suggestion[];
     error?: boolean;
     helperText?: string;
@@ -67,6 +68,7 @@ export class Autocomplete<Value, Suggestion> extends React.Component<Autocomplet
 
     renderInput() {
         return <Input
+            disabled={this.props.disabled}
             autoFocus={this.props.autofocus}
             inputRef={this.inputRef}
             value={this.props.value}
index e802ad5ccbd8b355b4d362eab0b3065714cfaa7f..90a6a3011435622763cacde512d5083a938c1625 100644 (file)
@@ -2,11 +2,13 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
+import { ChangeEvent } from 'react';
 import { connect } from 'react-redux';
-import { change, WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps } from 'redux-form';
+import { change, WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps, reset } from 'redux-form';
 import { Vocabulary, PropFieldSuggestion } from '~/models/vocabulary';
 import { RootState } from '~/store/store';
 import { getVocabulary } from '~/store/vocabulary/vocabulary-selectors';
+import { PROPERTY_KEY_FIELD_ID } from './property-key-field';
 
 export interface VocabularyProp {
     vocabulary: Vocabulary;
@@ -36,7 +38,6 @@ export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
 export const buildProps = ({ input, meta }: WrappedFieldProps) => {
     return {
         value: input.value,
-        onChange: input.onChange,
         items: ITEMS_PLACEHOLDER,
         renderSuggestion: (item: PropFieldSuggestion) => item.label,
         error: hasError(meta),
@@ -69,3 +70,19 @@ export const handleSelect = (
             dispatch(change(formName, fieldName, item.id));
         }
     };
+
+export const handleChange = (
+    fieldName: string,
+    formName: string,
+    { onChange }: WrappedFieldInputProps,
+    { dispatch }: WrappedFieldMetaProps) =>
+    (value: ChangeEvent<HTMLInputElement>) => {
+        if (fieldName === PROPERTY_KEY_FIELD_ID) {
+            // Properties' values are dependant on the keys, if any value is
+            // pre-existant, a change on the property key should mean that the
+            // previous value is invalid.
+            dispatch(reset(formName));
+        }
+        onChange(value);
+        dispatch(change(formName, fieldName, value));
+    };
\ No newline at end of file
index d17f50d46530484b07a6682314eb8527b2e10d02..0c3a49bea217ca2276152fe02007ecac2639f604 100644 (file)
@@ -7,9 +7,10 @@ import { WrappedFieldProps, Field, FormName } from 'redux-form';
 import { memoize } from 'lodash';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary, getTags, getTagKeyID } from '~/models/vocabulary';
-import { handleSelect, handleBlur, connectVocabulary, VocabularyProp, ValidationProp, buildProps } from '~/views-components/resource-properties-form/property-field-common';
+import { handleSelect, handleBlur, connectVocabulary, VocabularyProp, ValidationProp, buildProps, handleChange } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_KEY_VALIDATION } from '~/validators/validators';
 import { escapeRegExp } from '~/common/regexp.ts';
+import { ChangeEvent } from 'react';
 
 export const PROPERTY_KEY_FIELD_NAME = 'key';
 export const PROPERTY_KEY_FIELD_ID = 'keyID';
@@ -32,6 +33,9 @@ const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & Vocabula
             suggestions={getSuggestions(props.input.value, vocabulary)}
             onSelect={handleSelect(PROPERTY_KEY_FIELD_ID, data.form, props.input, props.meta)}
             onBlur={handleBlur(PROPERTY_KEY_FIELD_ID, data.form, props.meta, props.input, getTagKeyID(props.input.value, vocabulary))}
+            onChange={(value: ChangeEvent<HTMLInputElement>) => {
+                handleChange(PROPERTY_KEY_FIELD_ID, data.form, props.input, props.meta)(value);
+            }}
             {...buildProps(props)}
         />
     )} />;
index c5a5071fb8aa44a8a8d6a08f79d9ae97f48ecb86..9ce9d52163bf5783a7d15491025f743ea87d860e 100644 (file)
@@ -7,23 +7,31 @@ import { WrappedFieldProps, Field, formValues, FormName } from 'redux-form';
 import { compose } from 'redux';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary, isStrictTag, getTagValues, getTagValueID } from '~/models/vocabulary';
-import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
-import { handleSelect, handleBlur, VocabularyProp, ValidationProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common';
+import { PROPERTY_KEY_FIELD_ID, PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field';
+import { handleSelect, handleBlur, VocabularyProp, ValidationProp, connectVocabulary, buildProps, handleChange } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
 import { escapeRegExp } from '~/common/regexp.ts';
 
 interface PropertyKeyProp {
-    propertyKey: string;
+    propertyKeyId: string;
+    propertyKeyName: string;
 }
 
-type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp & ValidationProp;
+interface PropertyValueInputProp {
+    disabled: boolean;
+}
+
+type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp & ValidationProp & PropertyValueInputProp;
 
 export const PROPERTY_VALUE_FIELD_NAME = 'value';
 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
 
 const connectVocabularyAndPropertyKey = compose(
     connectVocabulary,
-    formValues({ propertyKey: PROPERTY_KEY_FIELD_ID }),
+    formValues({
+        propertyKeyId: PROPERTY_KEY_FIELD_ID,
+        propertyKeyName: PROPERTY_KEY_FIELD_NAME,
+    }),
 );
 
 export const PropertyValueField = connectVocabularyAndPropertyKey(
@@ -33,29 +41,31 @@ export const PropertyValueField = connectVocabularyAndPropertyKey(
             name={PROPERTY_VALUE_FIELD_NAME}
             component={PropertyValueInput}
             validate={skipValidation ? undefined : getValidation(props)}
-            {...props} />
+            {...{...props, disabled: !props.propertyKeyName}} />
         </span>
 );
 
-const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
+const PropertyValueInput = ({ vocabulary, propertyKeyId, propertyKeyName, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
     <FormName children={data => (
         <Autocomplete
             label='Value'
-            suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
+            disabled={props.disabled}
+            suggestions={getSuggestions(props.input.value, propertyKeyId, vocabulary)}
             onSelect={handleSelect(PROPERTY_VALUE_FIELD_ID, data.form, props.input, props.meta)}
-            onBlur={handleBlur(PROPERTY_VALUE_FIELD_ID, data.form, props.meta, props.input, getTagValueID(propertyKey, props.input.value, vocabulary))}
+            onBlur={handleBlur(PROPERTY_VALUE_FIELD_ID, data.form, props.meta, props.input, getTagValueID(propertyKeyId, props.input.value, vocabulary))}
+            onChange={handleChange(PROPERTY_VALUE_FIELD_ID, data.form, props.input, props.meta)}
             {...buildProps(props)}
         />
     )} />;
 
 const getValidation = (props: PropertyValueFieldProps) =>
-    isStrictTag(props.propertyKey, props.vocabulary)
+    isStrictTag(props.propertyKeyId, props.vocabulary)
         ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
         : TAG_VALUE_VALIDATION;
 
-const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
+const matchTagValues = ({ vocabulary, propertyKeyId }: PropertyValueFieldProps) =>
     (value: string) =>
-        getTagValues(propertyKey, vocabulary).find(v => v.label === value)
+        getTagValues(propertyKeyId, vocabulary).find(v => v.label === value)
             ? undefined
             : 'Incorrect value';