15067: Retrieve property values by its key id.
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-value-field.tsx
index ce4f5a52591e6b106049edc9719d29def8a259d5..5043bca7d24c62b2130438bff71e506f28cddca6 100644 (file)
@@ -3,13 +3,14 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { WrappedFieldProps, Field, formValues } from 'redux-form';
+import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps, Field, formValues } from 'redux-form';
 import { compose } from 'redux';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary } from '~/models/vocabulary';
-import { PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field';
-import { VocabularyProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common';
+import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
+import { VocabularyProp, connectVocabulary, buildProps, PropFieldSuggestion } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
+import { COLLECTION_TAG_FORM_NAME } from '~/store/collection-panel/collection-panel-action';
 
 interface PropertyKeyProp {
     propertyKey: string;
@@ -18,22 +19,31 @@ interface PropertyKeyProp {
 export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
 
 export const PROPERTY_VALUE_FIELD_NAME = 'value';
+export const PROPERTY_VALUE_FIELD_ID = 'valueID';
 
 export const PropertyValueField = compose(
     connectVocabulary,
-    formValues({ propertyKey: PROPERTY_KEY_FIELD_NAME })
+    formValues({ propertyKey: PROPERTY_KEY_FIELD_ID })
 )(
     (props: PropertyValueFieldProps) =>
-        <Field
-            name={PROPERTY_VALUE_FIELD_NAME}
-            component={PropertyValueInput}
-            validate={getValidation(props)}
-            {...props} />);
+        <div>
+            <Field
+                name={PROPERTY_VALUE_FIELD_NAME}
+                component={PropertyValueInput}
+                validate={getValidation(props)}
+                {...props} />
+            <Field
+                name={PROPERTY_VALUE_FIELD_ID}
+                type='hidden'
+                component='input' />
+        </div>
+);
 
 export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
     <Autocomplete
         label='Value'
         suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
+        onSelect={handleSelect(props.input, props.meta)}
         {...buildProps(props)}
     />;
 
@@ -44,7 +54,7 @@ const getValidation = (props: PropertyValueFieldProps) =>
 
 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
     (value: string) =>
-        getTagValues(propertyKey, vocabulary).find(v => v.id.includes(value))
+        getTagValues(propertyKey, vocabulary).find(v => v.label === value)
             ? undefined
             : 'Incorrect value';
 
@@ -66,3 +76,12 @@ const getTagValues = (tagKey: string, vocabulary: Vocabulary) => {
         : [];
     return ret;
 };
+
+const handleSelect = (
+    { onChange }: WrappedFieldInputProps,
+    { dispatch }: WrappedFieldMetaProps) => {
+        return (item:PropFieldSuggestion) => {
+            onChange(item.label);
+            dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, item.id));
+    };
+};